Devine order of instantiation
by Dan Pascal · in Torque X 2D · 12/05/2007 (9:36 am) · 3 replies
Hello,
let's say I make some objects in txb: objects A,B,C and D,
Then I add a different component to each object.
The component on object A looks for the instantiated object B, the component on object B looks for the instantiated object C (or D), etc...
When the txb scene file is loaded (deserialized), does it matter in what order?
i.e. if it loads object A first, will it immediately run the A component code that looks for an object B that is not instantiated yet?
I often get the "null reference" error, and wondered if sometimes it has to do with the order in which the components are be deserialzed and instantiated.
Or does it matter at all?
let's say I make some objects in txb: objects A,B,C and D,
Then I add a different component to each object.
The component on object A looks for the instantiated object B, the component on object B looks for the instantiated object C (or D), etc...
When the txb scene file is loaded (deserialized), does it matter in what order?
i.e. if it loads object A first, will it immediately run the A component code that looks for an object B that is not instantiated yet?
I often get the "null reference" error, and wondered if sometimes it has to do with the order in which the components are be deserialzed and instantiated.
Or does it matter at all?
About the author
#2
I did't trace through the source, but I'm guessing all components are created first then _OnRegister() gets called for each component afterwards.
The only problem with this is that some things that are initialized when _OnRegister() is called aren't initialized yet when the constructor is called. if you just need a reference to an object in another object's component, then this works great.
12/05/2007 (4:42 pm)
This may work depending on what you are trying to accomplish. You just create object with 'new' in the component's constructor. That's what constructors are for, technically. I did't trace through the source, but I'm guessing all components are created first then _OnRegister() gets called for each component afterwards.
The only problem with this is that some things that are initialized when _OnRegister() is called aren't initialized yet when the constructor is called. if you just need a reference to an object in another object's component, then this works great.
Torque Owner Joshua A. Thomas
If you 'new' some object owned by B in one of its components _OnRegister() and try to access it from A, it will be null if A appears before B in the level XML file. I think theres a way around this. I'll get back with you if someone else doesn't.