Game Development Community

Finding Objects created with code

by Matthew Hoesterey · in Torque X 2D · 10/01/2008 (8:57 pm) · 2 replies

Hi, I'm having trouble finding objects that I create with code from outside of the objects components. for example I create player 2 with this code:

_spawn = TorqueObjectDatabase.Instance.FindObject("SpawnP2");
_player2 = (T2DSceneObject)playerTemplate.Clone();
_player2.Position = new Vector2(_spawn.Position.X, _spawn.Position.Y);
PlayerInputMap InputP2 = _player2.Components.FindComponent();
InputP2.PlayerNumber = 1;
TorqueObjectDatabase.Instance.Register(_player2);



The object creates and works as I would expect.




Now I want to find that objects position when player 1 attacks so I write this code:

T2DSceneObject Player2 = TorqueObjectDatabase.Instance.FindObject("_player2");



Sadly this line returns null.




Does a registered instance have a name that is different then the name of the variable you registered it as?

I can pass the _player2 variable from my component out to my .game class and then access it there but that seams hacky and I'm sure there is a better way.

Thanks for any info.

#1
10/01/2008 (10:02 pm)
Looks like the name of the object is not set (you're trying to find an object named "_player2").

Try adding _player2.Name = "_player2"; // or whatever name you want
Then try the find with the name you set.
#2
10/03/2008 (6:40 am)
I figured I had to set it's name somehow!. :) thanks.