Creating a new sprite and imagemap without XML
by Jason Cahill · in Torque X 2D · 12/11/2006 (11:59 pm) · 6 replies
It would be great if someone could help create a small sample... maybe based on the StarterGame solution that shows in Game.cs how to create a material (the t2dImageMapDataBlock equivalent), a T2DStaticSprite, and how to register it (properly) into the scene.
I noticed that commenting out: SceneLoader.Load(@"data\levels\levelData.txscene");
Caused an exception to be thrown.
I'd love to know how to build a soup-to-nuts "mini scene" in code (instead of XML). I know that we should probably use XML all the time to be data driven, but it's a bit overwhelming at first and I'd like to understand how the individual pieces work.
Thanks for the assistance!
I noticed that commenting out: SceneLoader.Load(@"data\levels\levelData.txscene");
Caused an exception to be thrown.
I'd love to know how to build a soup-to-nuts "mini scene" in code (instead of XML). I know that we should probably use XML all the time to be data driven, but it's a bit overwhelming at first and I'd like to understand how the individual pieces work.
Thanks for the assistance!
About the author
#2
I'll definitely look at the pieces you mentioned. Thanks!
12/12/2006 (12:56 pm)
Interesting. Thanks Dave. As I mentioned, I'm trying to crawl, walk, run. And for me, crawl means start with 100% code, get a solid sense for that, before moving to a more data driven approach. I see a lot of benefits to the XML methodology, it's just harder for me to get my head around at the moment.I'll definitely look at the pieces you mentioned. Thanks!
#3
12/12/2006 (1:56 pm)
You can definitely create all your stuff in code and not use XML. Our early demos did this before the XML loader was ready. It can be a little quicker than hand-editing XML, but not quicker than using the editor to create objects (especially once you get familiar with how the editor works). Aside from no editor support, the downside of not using XML is that your game data tends to get scattered around in your game, making it harder to tweak data independently of code. Also, the XML format is a bit easier for artists to work with, because they don't have to hunt through a lot of C# code trying to find parameters to tweak. But in the end, you can do it whatever way you want, just be prepared to live with the consequences ;)
#4
T2DSceneGraph SceneGraph = new T2DSceneGraph();
SceneGraph.Name = "DefaultSceneGraph";
SceneGraph.IsPersistent = true;
T2DSceneContainer SceneContainer = new T2DSceneContainer();
SceneContainer.SceneGraph = SceneGraph;
SceneContainer.BinCount = 256;
SceneContainer.BinSize = 20;
SceneGraph.Container = SceneContainer;
TorqueObjectDatabase.Instance.Register(SceneGraph);
But when I run it, I get an error that no scene graph is installed. If someone could explain what my problem is, I would appreciate it.
Also, when I then add this code:
T2DSceneCamera SceneCamera = new T2DSceneCamera();
SceneCamera.CenterPosition = new Microsoft.Xna.Framework.Vector2(0);
SceneCamera.Extent = new Microsoft.Xna.Framework.Vector2(100, 75);
SceneCamera.ResizeToDisplayAspectRatioWithFixedWidth = true;
TorqueObjectDatabase.Instance.Register(SceneCamera);
It says that SceneCamera is null. Why does the constructor not instantiate the object?
03/22/2007 (5:28 pm)
Since the non-XML code seems to have been replaced (at least in the samples I have) I was wondering if someone could explain provide a bit more information. I'm working from the Start Game template, and have the following code in BeginRun():T2DSceneGraph SceneGraph = new T2DSceneGraph();
SceneGraph.Name = "DefaultSceneGraph";
SceneGraph.IsPersistent = true;
T2DSceneContainer SceneContainer = new T2DSceneContainer();
SceneContainer.SceneGraph = SceneGraph;
SceneContainer.BinCount = 256;
SceneContainer.BinSize = 20;
SceneGraph.Container = SceneContainer;
TorqueObjectDatabase.Instance.Register(SceneGraph);
But when I run it, I get an error that no scene graph is installed. If someone could explain what my problem is, I would appreciate it.
Also, when I then add this code:
T2DSceneCamera SceneCamera = new T2DSceneCamera();
SceneCamera.CenterPosition = new Microsoft.Xna.Framework.Vector2(0);
SceneCamera.Extent = new Microsoft.Xna.Framework.Vector2(100, 75);
SceneCamera.ResizeToDisplayAspectRatioWithFixedWidth = true;
TorqueObjectDatabase.Instance.Register(SceneCamera);
It says that SceneCamera is null. Why does the constructor not instantiate the object?
#5
If you look at the T2DSceneGraph constructor you'll note there are several different versions. The default constructor will not set the BaseSceneGraph singleton to itself. And in fact should only be used when deserializing from XML. It doesn't set the BaseSceneGraph singleton to dissuade you from using it because it should only be used for XML deserialization.
To fix this use one of the other constructors, such as:
The camera issue was complaining about a null scenegraph, not a null camera. Doing the above fixes both your scenegraph and camera.
03/23/2007 (1:24 pm)
Currently the SceneGraph is implemented as a singleton. There is a BaseSceneGraph which exposes a static property allowing you to access the scenegraph instance. T2DSceneGraph inherits from this class and also exposes a static property to access the scenegraph instance.If you look at the T2DSceneGraph constructor you'll note there are several different versions. The default constructor will not set the BaseSceneGraph singleton to itself. And in fact should only be used when deserializing from XML. It doesn't set the BaseSceneGraph singleton to dissuade you from using it because it should only be used for XML deserialization.
To fix this use one of the other constructors, such as:
// creates the container and adds to the BaseSceneGraph singleton automatically T2DSceneGraph scenegraph = new T2DSceneGraph(true); scenegraph.Name = "DefaultSceneGraph"; scenegraph.IsPersistent = true; scenegraph.Container.BinCount = 256; scenegraph.Container.BinSize = 20; TorqueObjectDatabase.Instance.Register(scenegraph);
The camera issue was complaining about a null scenegraph, not a null camera. Doing the above fixes both your scenegraph and camera.
#6
03/23/2007 (3:44 pm)
That worked, thanks, I should have been able to figure that out. However, according to Visual Studio, the constructor that takes in one argument is used to create a SceneContainer and the one with two arguments creates a SceneContainer and has the option to install in BaseSceneGraph. Is this because the one argument constructor uses true for the second argument, so it installs in the BaseSceneGraph, and if I called new T2DSceneGraph(false) it would still work if I specified my own SceneContainer?
Associate David Wyand
Gnometech Inc.
Jason, I went through the same thing when writing the Torque Combat starter kit. I started off with defining all sprites and materials in C# and have been slowly moving over to XML. I too found it helped me to understand the engine's flow rather than treating it as a black box.
If you'd like to see how I did it, take a look in Torque Combat's GameData.cs at the GetPlayer1Winner() method. That shows how to set up a material and sprite template all at once. When it comes time to use the sprite you Clone() it to a new T2DSpriteObject (or your object of choice) place it and register it. For that last part see Levels/SumoLevel.cs in UpdateLevel() where I build _winnerText. Hopefully that'll get you started.
Ironically just this morning I've started to move all of this into XML so you won't be able to find the above in the next TorqueX release. One advantage of going with XML is you can preload your materials -- this happens automatically in TorqueX. If you don't do this then the textures are loaded when they're first needed which can cause a noticeable pause in your game.
An example of this is when you win at Torque Combat's Sumo match. The winner text is currently loaded when it is displayed which doesn't appear to cause a problem on my laptop. But on the Xbox there is a definite pause before the text is displayed. Moving over to the XML file has fixed it.
- LightWave Dave