Game Development Community

Help Dynamically adding components

by Will O-Reagan · in Torque X 2D · 07/13/2007 (6:29 pm) · 2 replies

I'm trying to create a scene object dynamically but my code doesnt seem to be working. I want to in code create a scene object, and then add components.. but the components and object don't seem to be working properly, heres my code.

T2DSceneObject _newObj = new T2DSceneObject();
Haywire _haywire = new Haywire();
_newObj.Components.AddComponent(_haywire);
TorqueObjectDatabase.Instance.Register(_newObj);

Am I missing something?

About the author

I have a degree in dramatic art, and literature, from UC Santa Barbara. I've worked for a few studios, also at Animax Ent in 2008, and some smaller studios. Currently studying Computer Science at CSU Channel Islands.


#1
07/14/2007 (7:35 am)
What do you mean not working 'properly'? I'll just take a wild guess and think your component isn't firing the ProcessTick()/InterpolateTick() methods?

If that's the case, try to make sure that in the OnRegister() method in your component you have something like:

protected override bool _OnRegister(TorqueObject owner)
        {
            if (!base._OnRegister(owner) || !(owner is T2DSceneObject))
                return false;
        
            ProcessList.Instance.AddTickCallback(owner, this);
            return true;
        }

ProcessList.Instance.AddTickCallback() allows your component to fire the ProcessTick/InterpolateTick methods.
#2
07/14/2007 (1:43 pm)
In a nutshell, this what I'm seeing. My goal of the test sample was to have a working process tick, register, and unregister... however, this is what I expereinced using the code provided above.

1: I had the new "sceneObject" delete a remote object, on it's "unregister"

2: It found the remote object as null

3: I changed the delete command to a physics command, and it found the remote object and changed its physics(proving that it could in fact find the remote object).


everything else seemed fine

@Alex, as #3 suggests, its not a problem with process tick..