Need help with TorqueObjectDatabase
by Aivan Monceller · in Torque X 2D · 10/17/2008 (7:50 am) · 28 replies
I currently have this code. I am receiving a runtime error on the last line of this code. The exact error is, "NullReferenceException was unhandled" use new keyword to create an object instance. I have the same problem with other TorqueObjectDatabase.Instance.Register lines on my code.
T2DSceneCamera sceneCamera = new T2DSceneCamera();
Vector2 cPosition = new Vector2(0,0);
Vector2 eXtent = new Vector2(100,75);
sceneCamera.CenterPosition = cPosition;
sceneCamera.Extent = eXtent;
sceneCamera.UseCameraWorldLimits = false;
TorqueObjectDatabase.Instance.Register(sceneCamera);
#2
10/17/2008 (9:06 am)
SceneCamera is not a keyword but an object made from T2DSceneCamera.
#3
10/17/2008 (10:28 am)
Cool, I wasn't sure if it was having trouble making the object because sceneCamera was used as a keyword by torque. I'll take a look when I get home as I've created a camera with a similar block of code so I can find the differences. :) Hopefully it's something simple.
#4
Hopefully, I'll be writing a txscene to C# converter if I would get a concrete grasp on how the XML deserialization works.
10/17/2008 (10:29 am)
No luck with variable name changing. I've been trying to rewrite the whole levelData.txscene to C# code. Please help me with my efforts as I am experiencing the problem above during runtime on every line that contains the TorqueObjectDatabase.Instance.Register. Please spare some seconds to look at my code. This is the C# version of the txscene (written in XML) which compiles neatly but the error pops out during runtime. //<SceneData>
TorqueObjectType TileType;
TorqueObjectType t2dAnimatedSpriteType;
TorqueObjectType t2dScrollerType;
TorqueObjectType t2dSpriteType;
TorqueObjectType t2dParticleEffectType;
TorqueObjectType t2dTileLayerType;
T2DSceneGraph sceneData = new T2DSceneGraph();
sceneData.Name = "DefaultSceneGraph";
T2DSceneContainer sceneContainer = new T2DSceneContainer(sceneData);
sceneContainer.BinSize = 20;
sceneContainer.BinCount = 256;
T2DSceneCamera sceneCamera = new T2DSceneCamera();
Vector2 cPosition = new Vector2(0,0);
Vector2 eXtent = new Vector2(100,75);
sceneCamera.CenterPosition = cPosition;
sceneCamera.Extent = eXtent;
sceneCamera.UseCameraWorldLimits = false;
TorqueObjectDatabase.Instance.Register(sceneCamera);
//</SceneData>
//<Material>
T2DCollisionMaterial DefaultCollisionMaterial = new T2DCollisionMaterial(1F,0.3F,0F);
SimpleMaterial material = new SimpleMaterial();
material.TextureFilename = @"data\images\ketchup.jpg";
material.IsTranslucent = true;
material.Name = "ketchupMaterial";
//</Material>
//<Objects>
// <StaticSprite>
T2DStaticSprite ketchupLogo = new T2DStaticSprite();
ketchupLogo.CreateWithCollision = false;
ketchupLogo.CreateWithPhysics = false;
ketchupLogo.Name = "ketchupLogo";
ketchupLogo.Pool = true;
ketchupLogo.PoolWithComponents = false;
ketchupLogo.IsTemplate = false;
ketchupLogo.IsPersistent = false;
ketchupLogo.Layer = 0;
Vector2 position = new Vector2(-1.573F,-2.189F);
ketchupLogo.Position = position;
ketchupLogo.Rotation = 0;
ketchupLogo.FlipX = false;
ketchupLogo.FlipY = false;
Vector2 sortpoint = new Vector2(0,0);
ketchupLogo.SortPoint = sortpoint;
ketchupLogo.MaterialRegionIndex = 0;
ketchupLogo.Visible = true;
ketchupLogo.VisibilityLevel = 1;
ketchupLogo.CollisionsEnabled = true;
ketchupLogo.Material = (RenderMaterial)TorqueObjectDatabase.Instance.FindObject("ketchupMaterial");
ketchupLogo.Components.AddComponent(new T2DPhysicsComponent());
ketchupLogo.Components.AddComponent(new T2DCollisionComponent());
ketchupLogo.Components.AddComponent(new KetchupAI.Movement.MovementComponent());
TorqueObjectDatabase.Instance.Register(ketchupLogo);
T2DSceneObject kLogoTemplate = TorqueObjectDatabase.Instance.FindObject<T2DSceneObject>("ketchupLogo");
_kLogo = (T2DSceneObject)kLogoTemplate.Clone();
_kLogo.Position = position;
// <Components>
_kLogo.Components.AddComponent(new T2DPhysicsComponent());
_kLogo.Components.AddComponent(new T2DCollisionComponent());
_kLogo.Components.AddComponent(new MovementComponent());
// </Components>
TorqueObjectDatabase.Instance.Register(_kLogo);
// </StaticSprite>
//</Objects>Hopefully, I'll be writing a txscene to C# converter if I would get a concrete grasp on how the XML deserialization works.
#5
John K.
10/17/2008 (3:31 pm)
What's the name of the source file and the method name that contains the above code? Also, TorqueX comes with an XML deserializer that instantiates C# objects from XML - are you sure that you're not re-inventing the wheel.John K.
#6
10/17/2008 (6:14 pm)
I am aware that there is an existing xml deserializer built in torque x. The codes above are located under SceneLoader of Game.cs. I want to render a sprite with material on screen with the movementcomponent.cs without the use of the txscene (as if I don't have a TGBX). I would use this for my project presentation just to prove that developing games in Torque X is not dependent to its builder software ( I just need the Garagames.TorqueX.Framework.dll). I would appreciate If you would give me an example. I am just trying to accomplish the comment above the SceneLoader in the Game.cs source.Quote:
load our scene objects from XML. Torque X is designed to load game data from XML, but this is not strictly required; anything in an XML file can also be created manually in C# code. The SceneLoader is provided by TorqueGame and can be used to load and unload XML files.
#7
Honestly I'm a bit new to this so I'm not sure how everything fits together but.
One diffrence I,m noticing is my .txscene file starts like this
1.0
and ends like
perhapes you need the tag for torque to find it?
Honestly I don't know just something I noticed.
my Scene data looks like this
true
ground
player
sysDummy
hitChecker
player1
player2
player3
player4
t2dScrollerType
t2dSpriteType
t2dAnimatedSpriteType
t2dParticleEffectType
t2dTileLayerType
TileType
20
256
0
0
1280.000
720.000
false
10/17/2008 (6:35 pm)
Sorry I didn't know that you where trying to get things running without the builder.Honestly I'm a bit new to this so I'm not sure how everything fits together but.
One diffrence I,m noticing is my .txscene file starts like this
and ends like
perhapes you need the tag for torque to find it?
Honestly I don't know just something I noticed.
my Scene data looks like this
#8
PS: Use CODE tags mate.
10/17/2008 (7:22 pm)
I believe the structure of my SceneData is correct. But, I am not sure if I followed the heirarchy of T2DSceneContainer under T2DSceneGraph.PS: Use CODE tags mate.
#9
10/17/2008 (9:41 pm)
Ah had to look that up, Didn't know how LOL.
#10
10/18/2008 (2:35 am)
I hope my goal was clearly stated above, looking forward for the help of experts in this forum.
#11
T2DSceneGraph sceneData = new T2DSceneGraph();
To this:
T2DSceneGraph sceneData = new T2DSceneGraph(true);
And then Add this:
sceneCamera.SceneGraph = sceneData;
Before you register the camera.
This will properly initialize the scenegraph and create the rendering container and then attach the camera to the scenegraph so that the camera knows what to render.
John K.
10/18/2008 (8:46 am)
Okay, I think I understand you now. If you want to avoid using the .txscene file and have commented out the SceneLoader.Load(@"data\levels\levelData.txscene");, and want to create everything manually, that's fine. The problem with your code is that the scene graph is not properly initialized and attached to the camera. Change this:T2DSceneGraph sceneData = new T2DSceneGraph();
To this:
T2DSceneGraph sceneData = new T2DSceneGraph(true);
And then Add this:
sceneCamera.SceneGraph = sceneData;
Before you register the camera.
This will properly initialize the scenegraph and create the rendering container and then attach the camera to the scenegraph so that the camera knows what to render.
John K.
#12
10/18/2008 (9:42 pm)
Hi John, I've done what you've said and it turned out well. But I have another problem, I don't see an object on screen is something missing with my code. I only see a black screen, I am expecting an object with a material on screen with the movement component based on my code above. I am not getting errors right now but I just can't see a thing.
#13
Oh, wait. I don't think you registered the material ...
10/18/2008 (9:53 pm)
Try checking that the material is being found (just in case):ketchupLogo.Material = (RenderMaterial)TorqueObjectDatabase.Instance.FindObject("ketchupMaterial");
//add null check
if (ketchupLogo.Material == null)
{
//handle material not found
}Oh, wait. I don't think you registered the material ...
material.Name = "ketchupMaterial"; //don't forget to register it ... TorqueObjectDatabase.Instance.Register(material);
#14
10/18/2008 (9:58 pm)
I get this error when I try to register the material.Argument '1': cannot convert from 'GarageGames.Torque.Materials.SimpleMaterial' to 'GarageGames.Torque.Core.TorqueObject'
#15
So how about replacing
ketchupLogo.Material = (RenderMaterial)TorqueObjectDatabase.Instance.FindObject("ketchupMaterial");
with
ketchupLogo.Material = (RenderMaterial)material;
10/18/2008 (10:04 pm)
Right, SimpleMaterial isn't a TorqueObject.So how about replacing
ketchupLogo.Material = (RenderMaterial)TorqueObjectDatabase.Instance.FindObject("ketchupMaterial");
with
ketchupLogo.Material = (RenderMaterial)material;
#16
10/18/2008 (10:11 pm)
I tried that and it compiled without errors but still can't see a thing.
#17
10/18/2008 (10:27 pm)
Can you send me the current version of your code (it will be easier to figure out with the debugger than visual inspection ...)? Send your Game.cs file (I'm assuming that's where this code is) to sdgoodwin@cogeco.ca. I'll have some time to look into it more carefully tomorrow.
#18
10/18/2008 (10:36 pm)
Now that's help. Thank you for your time, I'll be sending it now.
#19
add
ketchupLogo.Size = new Vector2(32,32); // otherwise the size is (0,0)!
Here's a minimal version that displays the GGLogo without using the .txscene:
10/18/2008 (11:25 pm)
One more thing to try:add
ketchupLogo.Size = new Vector2(32,32); // otherwise the size is (0,0)!
Here's a minimal version that displays the GGLogo without using the .txscene:
protected override void BeginRun()
{
base.BeginRun();
// load our scene objects from XML. Torque X is designed to load game data from XML, but this is not strictly required;
// anything in an XML file can also be created manually in C# code. The SceneLoader is provided by TorqueGame and can be
// used to load and unload XML files.
//SceneLoader.Load(@"data\levels\levelData.txscene");
T2DSceneGraph sceneGraph = new T2DSceneGraph(true);
sceneGraph.Name = "DefaultSceneGraph";
T2DSceneCamera camera = new T2DSceneCamera();
camera.Name = "Camera";
camera.CenterPosition = new Vector2(0, 0);
camera.Extent = new Vector2(1024, 768);
camera.SceneGraph = sceneGraph;
SimpleMaterial material = new SimpleMaterial();
material.TextureFilename = @"data\images\GGLogo.png";
T2DStaticSprite logo = new T2DStaticSprite();
logo.Material = material;
logo.Position = new Vector2(0, 0);
logo.Size = new Vector2(32, 32);
TorqueObjectDatabase.Instance.Register(logo);
}
#20
ketchupLogo.Components.AddComponent(new MovementComponent());
10/18/2008 (11:29 pm)
Now I could see the image but couldn't make it move using this line of code.ketchupLogo.Components.AddComponent(new MovementComponent());
Torque Owner Matthew Hoesterey
First thing I would try is changeing "sceneCamera" to something new like _mainCamera and see if you still get the error.