The Complete Guide Guide to Torque X for XNA: Chapter 9 - Adding Game Functionality
by Randy Lutcavich · in Torque X 2D · 07/17/2009 (5:02 pm) · 14 replies
Hi All,
I've been working my way through John Kanalakis' book on TX and have run into a few issues as I try and get the hang of this new engine. I'm now on Chapter 9... for the most part I've just ended up reading through early tutorials because I had trouble with some of the code(Note that I'm also skipping all the 3D chapters). I'm very determined to put all the time needed into gaining the skills necessary to complete a game for Xbox 360: Indie Games and I know this book is going to be a huge part of my learning process.
I've searched the GG forums for help on random issues that I was having on parts of this book but didn't have much luck. I'm convinced I am not the only one with questions on some of the tutorials and I thought it would be a good idea to start organizing my questions in a new thread and seeing what kind of help I can get. After all, John K. himself seems to be active in the forums.
So without giving away too much of the book, because I really want people to buy it, I'm going to compile a list of issues I run into while working my way through each chapter... starting with 9 because that's where I am now.
I'll have my questions up soon.
Thanks,
Randy Lutcavich
RandysPortfolio.com
I've been working my way through John Kanalakis' book on TX and have run into a few issues as I try and get the hang of this new engine. I'm now on Chapter 9... for the most part I've just ended up reading through early tutorials because I had trouble with some of the code(Note that I'm also skipping all the 3D chapters). I'm very determined to put all the time needed into gaining the skills necessary to complete a game for Xbox 360: Indie Games and I know this book is going to be a huge part of my learning process.
I've searched the GG forums for help on random issues that I was having on parts of this book but didn't have much luck. I'm convinced I am not the only one with questions on some of the tutorials and I thought it would be a good idea to start organizing my questions in a new thread and seeing what kind of help I can get. After all, John K. himself seems to be active in the forums.
So without giving away too much of the book, because I really want people to buy it, I'm going to compile a list of issues I run into while working my way through each chapter... starting with 9 because that's where I am now.
I'll have my questions up soon.
Thanks,
Randy Lutcavich
RandysPortfolio.com
About the author
Recent Threads
#2
07/17/2009 (6:04 pm)
Often times slow frame-rate for no apparent reason is fixed by turning on Simulate Fences. See this thread for details.
#3
Done So Far:
•Named the player's chopper "player"
•Named the scene camera "SceneCamera"
•Added camera following code from chapter 3, page 167 to game.cs
•Attempted to Run the Game
Problem:
Debugging the game returns the following error:
"Object reference not set to an instance of an object."
I believe this is because the code from chapter 3 expects the player to be an animated sprite. I've tried changing the code so that instead of _player being a an animated sprite it is a static sprite and I even tried it as a scene object but for some reason I continually get the same error. I even tried making a animated rotor the player and using the chapter 3 code but that returns the same error.
Question:
What variable should _player be?
EDIT:
Fix:
There were actually two problems with this part of the tutorial.
The first problem can be solved by naming the scene camera: "Camera" and making sure to search for "Camera" in code instead of "SceneCamera". Thanks Julien and Scott!
T2DSceneObject _camera = TorqueObjectDatabase.Instance.FindObject<T2DSceneCamera>("Camera");
The second problem can be fixed by using a T2DStaticSprite for the _player object. There are three spots where the tutorial code uses T2DAnimatedSprite instead of T2DStaticSprite for the _player variable, be sure to change them all.
T2DStaticSprite _player = TorqueObjectDatabase.Instance.FindObject<T2DStaticSprite>("player");
T2DStaticSprite _player;
07/17/2009 (7:44 pm)
Issue 2 (Page 175): SOLVEDDone So Far:
•Named the player's chopper "player"
•Named the scene camera "SceneCamera"
•Added camera following code from chapter 3, page 167 to game.cs
•Attempted to Run the Game
Problem:
Debugging the game returns the following error:
"Object reference not set to an instance of an object."
I believe this is because the code from chapter 3 expects the player to be an animated sprite. I've tried changing the code so that instead of _player being a an animated sprite it is a static sprite and I even tried it as a scene object but for some reason I continually get the same error. I even tried making a animated rotor the player and using the chapter 3 code but that returns the same error.
Question:
What variable should _player be?
EDIT:
Fix:
There were actually two problems with this part of the tutorial.
The first problem can be solved by naming the scene camera: "Camera" and making sure to search for "Camera" in code instead of "SceneCamera". Thanks Julien and Scott!
T2DSceneObject _camera = TorqueObjectDatabase.Instance.FindObject<T2DSceneCamera>("Camera");
The second problem can be fixed by using a T2DStaticSprite for the _player object. There are three spots where the tutorial code uses T2DAnimatedSprite instead of T2DStaticSprite for the _player variable, be sure to change them all.
T2DStaticSprite _player = TorqueObjectDatabase.Instance.FindObject<T2DStaticSprite>("player");
T2DStaticSprite _player;
#4
Done So Far:
•Added the code for the Destructible Component
•Attempted to run the game
Problem:
I'm getting 6 debugs errors in DestructibleComponent.cs:
"Error 1 'StarterGame2D.DestructibleComponent' does not contain a definition for 'MaxHealthLevel' and no extension method 'MaxHealthLevel' accepting a first argument of type 'StarterGame2D.DestructibleComponent' could be found (are you missing a using directive or an assembly reference?) C:UsersRandyDocumentsVisual Studio 2008ProjectsXnaChopperStrike2XnaChopperStrike2GameDestructibleComponent.cs 54 18 Game
Error 2 The name 'MaxHealthLevel' does not exist in the current context C:UsersRandyDocumentsVisual Studio 2008ProjectsXnaChopperStrike2XnaChopperStrike2GameDestructibleComponent.cs 54 35 Game
Error 3 'StarterGame2D.DestructibleComponent' does not contain a definition for 'DamageHealthLevel' and no extension method 'DamageHealthLevel' accepting a first argument of type 'StarterGame2D.DestructibleComponent' could be found (are you missing a using directive or an assembly reference?) C:UsersRandyDocumentsVisual Studio 2008ProjectsXnaChopperStrike2XnaChopperStrike2GameDestructibleComponent.cs 55 18 Game
Error 4 The name 'DamageHealthLevel' does not exist in the current context C:UsersRandyDocumentsVisual Studio 2008ProjectsXnaChopperStrike2XnaChopperStrike2GameDestructibleComponent.cs 55 38 Game
Error 5 'StarterGame2D.DestructibleComponent' does not contain a definition for 'T2DParticleEffect' and no extension method 'T2DParticleEffect' accepting a first argument of type 'StarterGame2D.DestructibleComponent' could be found (are you missing a using directive or an assembly reference?) C:UsersRandyDocumentsVisual Studio 2008ProjectsXnaChopperStrike2XnaChopperStrike2GameDestructibleComponent.cs 56 18 Game
Error 6 'GarageGames.Torque.T2D.T2DParticleEffect' is a 'type' but is used like a 'variable' C:UsersRandyDocumentsVisual Studio 2008ProjectsXnaChopperStrike2XnaChopperStrike2GameDestructibleComponent.cs 56 38 Game"
When I comment out the new additions to the CopyTo method, I am able to run the game and use the new component.
Question:
Is the CopyTo method supposed to be function at this point in the tutorial? Or is it intended to be commented out? If not, what did I do wrong?
EDIT:
Fix:
This issue was solved by comparing the code between the actual book and the code found in the downloadable files for Chapter 9 - DestructibleComponent.cs www.TorqueXBook.com
The correct CopyTo should use these variables instead of the ones in the book...
public override void CopyTo(TorqueComponent obj)
{
base.CopyTo(obj);
DestructibleComponent obj2 = (DestructibleComponent)obj;
obj2.MaxHealth = MaxHealth;
obj2.Health = Health;
obj2.DestructionEffect = DestructionEffect;
}
07/17/2009 (8:30 pm)
Issue 3 (Page 175 - 177): SOLVEDDone So Far:
•Added the code for the Destructible Component
•Attempted to run the game
Problem:
I'm getting 6 debugs errors in DestructibleComponent.cs:
"Error 1 'StarterGame2D.DestructibleComponent' does not contain a definition for 'MaxHealthLevel' and no extension method 'MaxHealthLevel' accepting a first argument of type 'StarterGame2D.DestructibleComponent' could be found (are you missing a using directive or an assembly reference?) C:UsersRandyDocumentsVisual Studio 2008ProjectsXnaChopperStrike2XnaChopperStrike2GameDestructibleComponent.cs 54 18 Game
Error 2 The name 'MaxHealthLevel' does not exist in the current context C:UsersRandyDocumentsVisual Studio 2008ProjectsXnaChopperStrike2XnaChopperStrike2GameDestructibleComponent.cs 54 35 Game
Error 3 'StarterGame2D.DestructibleComponent' does not contain a definition for 'DamageHealthLevel' and no extension method 'DamageHealthLevel' accepting a first argument of type 'StarterGame2D.DestructibleComponent' could be found (are you missing a using directive or an assembly reference?) C:UsersRandyDocumentsVisual Studio 2008ProjectsXnaChopperStrike2XnaChopperStrike2GameDestructibleComponent.cs 55 18 Game
Error 4 The name 'DamageHealthLevel' does not exist in the current context C:UsersRandyDocumentsVisual Studio 2008ProjectsXnaChopperStrike2XnaChopperStrike2GameDestructibleComponent.cs 55 38 Game
Error 5 'StarterGame2D.DestructibleComponent' does not contain a definition for 'T2DParticleEffect' and no extension method 'T2DParticleEffect' accepting a first argument of type 'StarterGame2D.DestructibleComponent' could be found (are you missing a using directive or an assembly reference?) C:UsersRandyDocumentsVisual Studio 2008ProjectsXnaChopperStrike2XnaChopperStrike2GameDestructibleComponent.cs 56 18 Game
Error 6 'GarageGames.Torque.T2D.T2DParticleEffect' is a 'type' but is used like a 'variable' C:UsersRandyDocumentsVisual Studio 2008ProjectsXnaChopperStrike2XnaChopperStrike2GameDestructibleComponent.cs 56 38 Game"
When I comment out the new additions to the CopyTo method, I am able to run the game and use the new component.
Question:
Is the CopyTo method supposed to be function at this point in the tutorial? Or is it intended to be commented out? If not, what did I do wrong?
EDIT:
Fix:
This issue was solved by comparing the code between the actual book and the code found in the downloadable files for Chapter 9 - DestructibleComponent.cs www.TorqueXBook.com
The correct CopyTo should use these variables instead of the ones in the book...
public override void CopyTo(TorqueComponent obj)
{
base.CopyTo(obj);
DestructibleComponent obj2 = (DestructibleComponent)obj;
obj2.MaxHealth = MaxHealth;
obj2.Health = Health;
obj2.DestructionEffect = DestructionEffect;
}
#5
Done So Far:
•Created DestructibleComponent.cs (Commented out the CopyTo - See Issue 3)
•Rebuilt the project and restarted TX2D
•Added the DestructibleComponenet to the enemy helicopters
•Set the MaxHealth to 100
Problems:
There are two problems with this component. One, the book is telling me to set the DamageHealthLevel and the MinHealthLevel but I don't see these listed in TorqueBuilder... I only see Health and MaxHealth fields.
Two, the book wants me to change the T2DParticleEffect to the explosion particle effect but the dropdown field says none when I try to add the explosion effect. I tried reloading but both these issues still occur.
Questions:
Am I supposed to see more field than Health and MaxHealth in Torque Builder or does the book want me to change values in code?
How can I get the explosion to show up in the drop down?
EDIT:
Fix:
This was fixed by naming the particle effect object that is in the scene. The name should be: "explosion".
Ones the particle effect object is named, the drop down in the destructible component will list explosion.
As for the part about MinHealth and DamageHealth not having a field in this component... I don't think it is required. We have the Health and the MaxHealth and that seems to be all that is needed because the code for this component seems to use a value passed from the colliding object when dealing damage and the code also already recognizes when the object's health goes below zero. If this does turn out to be a problem later in the chapter I will create a new issue.
07/17/2009 (8:37 pm)
Issue 4 (Page 178): SOLVEDDone So Far:
•Created DestructibleComponent.cs (Commented out the CopyTo - See Issue 3)
•Rebuilt the project and restarted TX2D
•Added the DestructibleComponenet to the enemy helicopters
•Set the MaxHealth to 100
Problems:
There are two problems with this component. One, the book is telling me to set the DamageHealthLevel and the MinHealthLevel but I don't see these listed in TorqueBuilder... I only see Health and MaxHealth fields.
Two, the book wants me to change the T2DParticleEffect to the explosion particle effect but the dropdown field says none when I try to add the explosion effect. I tried reloading but both these issues still occur.
Questions:
Am I supposed to see more field than Health and MaxHealth in Torque Builder or does the book want me to change values in code?
How can I get the explosion to show up in the drop down?
EDIT:
Fix:
This was fixed by naming the particle effect object that is in the scene. The name should be: "explosion".
Ones the particle effect object is named, the drop down in the destructible component will list explosion.
As for the part about MinHealth and DamageHealth not having a field in this component... I don't think it is required. We have the Health and the MaxHealth and that seems to be all that is needed because the code for this component seems to use a value passed from the colliding object when dealing damage and the code also already recognizes when the object's health goes below zero. If this does turn out to be a problem later in the chapter I will create a new issue.
#6
07/18/2009 (10:00 am)
Issue 2: I had the same problem. For me it was that even if changed the name of the camera in Torque X, it didn't work. I had to change it in the level file here:<Camera2D name="SceneCamera">
#7
Issue number four--What number am I thinking of? Pat Buchanan! Wrong!!!
Sorry, for a second there I started channeling John McLaughlin. Love that show :)
Generally, to expose something to appear in TXB, you would write a Property with both Get and Set and whose type is a compatible type with the TXB boxes. Also, either the class itself, or else the specific Property need to be preceded immediately by the line:
07/18/2009 (10:16 am)
Yeah, I had found previously that changing the name of the standard camera in TXB and saving does not change it in the XML file. Though, I am not sure whether changing it manually will work long term, since I would think if you ever save again in TXB, its name will be reverted back to "Camera". I think you are better off just using that name instead.Issue number four--What number am I thinking of? Pat Buchanan! Wrong!!!
Sorry, for a second there I started channeling John McLaughlin. Love that show :)
Generally, to expose something to appear in TXB, you would write a Property with both Get and Set and whose type is a compatible type with the TXB boxes. Also, either the class itself, or else the specific Property need to be preceded immediately by the line:
[TorqueXmlSchemaType]When built, it will export the property to the txschema file to be imported by TXB.
#8
At this point all four issues have been solved. I've made it to the Creating Projectiles section on page 178.
I'll keep posting issues as they appear.
07/18/2009 (2:20 pm)
Thanks for the help Scott and Julien!At this point all four issues have been solved. I've made it to the Creating Projectiles section on page 178.
I'll keep posting issues as they appear.
#9
Done So Far:
•Made a new component item named WeaponComponent
•Added private fields and public properties using the code straight from the book
•Implemented the CopyTo() using the code straight from the book
•Created a public method: FireWeapon() using code straight from the book
•Attempted to rebuild as told to do so by the book
Problems:
There are compiler errors when attempting to rebuild due to this section of the CopyTo(): "obj2.FireWeaponEventName = FireWeaponEventName;
Questions:
I asked myself if there was some missing code.
Fix:
There is indeed some missing code. It appears that the final file for the WeaponComponent has more code for the FireWeaponEventName. I'm not sure when in the chapter that this code is added but if it is needed to compile and the book says to compile at this point, then I would expect it to be added. It appears the only extra code needed in WeaponComponent.cs to rebuild at this point in the chapter is:
Private Fields:
string _fireweaponEventName;
Public Property:
[TorqueXmlSchemaType(DefaultValue = "FireWeapon")]
public string FireWeaponEventName
{
get { return _fireweaponEventName; }
set { _fireweaponEventName = value; }
}
EDIT 2:
The missing code is added on Page 184 in the Responding to TorqueEvents section. I'm guessing I could have just commented out the references to _FireWeaponEventName until I got to this section. Either way, it shouldn't have been mentioned on pages 180 - 182.
07/18/2009 (3:42 pm)
Issue 5 (Page 180 - 182): SOLVEDDone So Far:
•Made a new component item named WeaponComponent
•Added private fields and public properties using the code straight from the book
•Implemented the CopyTo() using the code straight from the book
•Created a public method: FireWeapon() using code straight from the book
•Attempted to rebuild as told to do so by the book
Problems:
There are compiler errors when attempting to rebuild due to this section of the CopyTo(): "obj2.FireWeaponEventName = FireWeaponEventName;
Questions:
I asked myself if there was some missing code.
Fix:
There is indeed some missing code. It appears that the final file for the WeaponComponent has more code for the FireWeaponEventName. I'm not sure when in the chapter that this code is added but if it is needed to compile and the book says to compile at this point, then I would expect it to be added. It appears the only extra code needed in WeaponComponent.cs to rebuild at this point in the chapter is:
Private Fields:
string _fireweaponEventName;
Public Property:
[TorqueXmlSchemaType(DefaultValue = "FireWeapon")]
public string FireWeaponEventName
{
get { return _fireweaponEventName; }
set { _fireweaponEventName = value; }
}
EDIT 2:
The missing code is added on Page 184 in the Responding to TorqueEvents section. I'm guessing I could have just commented out the references to _FireWeaponEventName until I got to this section. Either way, it shouldn't have been mentioned on pages 180 - 182.
#10
This never became an issue because I spotted it right away but...
When creating the ApplyDamage method from page 183 change one of the references from ourObject to theirObject.
Instead of:
DestructibleComponent enemyDamage = ourObject.Components.FindComponent<DestructibleComponent>();
Use:
DestructibleComponent enemyDamage = theirComponents.Components.FindComponent<DestructibleComponent>();
Atleast that is what I am seeing in the final version of this file from www.TorqueXBook.com
07/18/2009 (4:09 pm)
FYI 1:This never became an issue because I spotted it right away but...
When creating the ApplyDamage method from page 183 change one of the references from ourObject to theirObject.
Instead of:
DestructibleComponent enemyDamage = ourObject.Components.FindComponent<DestructibleComponent>();
Use:
DestructibleComponent enemyDamage = theirComponents.Components.FindComponent<DestructibleComponent>();
Atleast that is what I am seeing in the final version of this file from www.TorqueXBook.com
#11
Page 185 meantions setting the AmmoRounds field to 500. No where in the chapter thus far has mentioned ammo and I don't see anything about it in the final .cs files.
I'm guessing we won't need it. I'll write up an issue if it turns out we need it.
07/18/2009 (4:24 pm)
FYI 2:Page 185 meantions setting the AmmoRounds field to 500. No where in the chapter thus far has mentioned ammo and I don't see anything about it in the final .cs files.
I'm guessing we won't need it. I'll write up an issue if it turns out we need it.
#12
Done So Far:
•Added all the code in the Creating an Inventory Item Component section
•Added all the code in the Creating the Inventory Manager Component
•Tried to run the game
Problems:
There are two identical compiler errors for the two lines: [System.Xml.Serilization.XmlIgnore]
Error 1 The type or namespace name 'Serialization' does not exist in the namespace 'System.Xml' (are you missing an assembly reference?) C:UsersRandyDocumentsVisual Studio 2008ProjectsXnaChopperStrike2XnaChopperStrike2GameInventoryManagerComponent.cs 35 21 Game
Questions:
Is it true that Serialization does not exist in the System.Xml namespace? How can I compile with this code?
EDIT:
Fix:
Followed Scott's directions to add the reference to System.Xml. Now I know :)
07/18/2009 (7:29 pm)
Issue 6 (Page 189): SOLVEDDone So Far:
•Added all the code in the Creating an Inventory Item Component section
•Added all the code in the Creating the Inventory Manager Component
•Tried to run the game
Problems:
There are two identical compiler errors for the two lines: [System.Xml.Serilization.XmlIgnore]
Error 1 The type or namespace name 'Serialization' does not exist in the namespace 'System.Xml' (are you missing an assembly reference?) C:UsersRandyDocumentsVisual Studio 2008ProjectsXnaChopperStrike2XnaChopperStrike2GameInventoryManagerComponent.cs 35 21 Game
Questions:
Is it true that Serialization does not exist in the System.Xml namespace? How can I compile with this code?
EDIT:
Fix:
Followed Scott's directions to add the reference to System.Xml. Now I know :)
#13
The namespace exists, but no namespace is officially available unless your project has a reference to its assembly, hence the part of the message
07/18/2009 (10:44 pm)
Issue #6:The namespace exists, but no namespace is officially available unless your project has a reference to its assembly, hence the part of the message
Quote:are you missing an assembly reference?Check the solution explorer for a reference to System.Xml in the references node under Game. Add it if not there by right-clicking References -> Add Reference... and finding it on the .NET tab.
#14
Done So Far:
•Added all the code in the Creating an Inventory Item Component section
•Added all the code in the Creating the Inventory Manager Component
•Tried to set the OnCollision field of an object in my scene to InventoryManagerComponent.AddItemToInventory as depicted on p.193 in the Turning Scene Objects into Pickup Items section.
Problem:
Only WeaponDamage is shown in the dropdown of the OnCollision field. I've successfuly built my project, reloaded the component schema and even restarted and still the AddItemToInventory selection does not appear.
Questions:
Shouldn't the code from page 188 - 193 in the Creating the Inventory Manager Component add this selection to the OnCollison dropdown?
07/19/2009 (4:16 pm)
Issue 7 (Page 193): SOLVEDDone So Far:
•Added all the code in the Creating an Inventory Item Component section
•Added all the code in the Creating the Inventory Manager Component
•Tried to set the OnCollision field of an object in my scene to InventoryManagerComponent.AddItemToInventory as depicted on p.193 in the Turning Scene Objects into Pickup Items section.
Problem:
Only WeaponDamage is shown in the dropdown of the OnCollision field. I've successfuly built my project, reloaded the component schema and even restarted and still the AddItemToInventory selection does not appear.
Questions:
Shouldn't the code from page 188 - 193 in the Creating the Inventory Manager Component add this selection to the OnCollison dropdown?
Torque Owner Randy Lutcavich
Done So Far:
•Added the background, three helicopters and their three animated rotors.
•Added the MovementComponent.
•Ran the game
Problem:
The frame rate is horrific. The game is very choppy and the player has trouble moving the helicopter across the screen as the rotors spin. Stopping the rotor animation fixes the problem.
Question:
I have a good amount of RAM, what else could be causing this problem?
EDIT:
Fix:
Setting SimulateFences to true in TorqueSettings.xml solves all framerate issues. Thanks Scott!