Game Development Community

ScriptObject inside a ScriptObject

by Samaursa · in General Discussion · 07/22/2009 (9:34 am) · 1 replies

Hi, I am running into a problem with scriptobjects inside a script object.

Lets say I have

new ScriptObject(someObject)
{
variable1 = "Random" @ getRandom(1, 100);
variable2 = "Random" @ getRandom(1, 100);
};

now if I create two objects

$newObj1 = new ScriptObject(newObj1 : someObject)
{
class = TWSiteUtil;
};

$newObj2 = new ScriptObject(newObj2 : someObject)
{
class = TWSiteUtil;
};

Both variable1 and variable2 of each object (newObj1 and newObj2) will have a different value in them. That is, each new object has its on variable1 and is not shared.

Now lets say I create another scriptobject

new ScriptObject(complexScriptObject)
{
nestedObj = new ScriptObject(nestedObj : someObject)
{
class = TWSiteUtil;
};
};

which is creating the previous script object. Now if I create two objects that are complexScriptObject, both of them have the same instance of nestedObj.

Initially I thought the problem was that the name nestedObj (in this example) is common and so torque points to the same object, but that is not the case because I tried making a random name by using getRandom().

I hope I explained my problem clearly. If not, please let me know and I will clarify further.

Thank you

#1
07/22/2009 (10:47 am)
I have found a solution to this problem which is explained below in case somebody else runs into a similar problem.

To avoid pointing to the same scriptObject inside the parent scriptObject, create all the children scriptObjects in the onAdd(%this) function of the parent scriptObject. Also, make sure that the name of the scriptObject is unique everytime (getRandom() is one way to get a unique name - there is however a slight chance that your object may end up with the same random number. That can also be solved by getting unique random numbers everytime. GetID is also another way to do it)