Copy ScriptObjects?
by kupo · in Torque Game Builder · 02/09/2012 (10:04 am) · 2 replies
Here is what I am trying to do:
I have three simsets.
1 - Fruits
2 - Tree
3 - Basket
In the fruits simset, I have some ScriptObjects. These scriptObjects are to be used as templates. For instance: Apple, Pear, Plum, Orange, etc.
What I hope to do is pick a random fruit type, and copy about 5-6 Apples into the Tree simset. Then, I can pick apples from the tree and place them in my basket.
I have everything else working, but It seems very tedious. I haven't touched trying to copy the class/etc yet. Here is some code:
I have three simsets.
1 - Fruits
2 - Tree
3 - Basket
In the fruits simset, I have some ScriptObjects. These scriptObjects are to be used as templates. For instance: Apple, Pear, Plum, Orange, etc.
What I hope to do is pick a random fruit type, and copy about 5-6 Apples into the Tree simset. Then, I can pick apples from the tree and place them in my basket.
I have everything else working, but It seems very tedious. I haven't touched trying to copy the class/etc yet. Here is some code:
//create the simsets
%fruits = new SimSet(fruits);
%tree = new simset(tree);
%bag = new simset(bag);
%a = new scriptobject(apple){};
%p = new scriptobject(pear){};
%o = new scriptobject(orange){};
%pl = new scriptobject(plumb){};
//add the fruits to the Fruits list
%fruits.add(%a);
%fruits.add(%p);
%fruits.add(%o);
%fruits.add(%pl);
//I get a random fruit
%randomType = %fruits.getRandomObject();
//I create the new fruit on the tree
%fruit.copyObject(%tree, %randomType);
function SimSet::copyObject(%this, %target, %object){
%c = %object.getDynamicFieldCount();
%name = %object.getName();
%n = new ScriptObject(%name);
for(%i=0; %i<%c; %i++){
%dfn = %object.getDynamicField(%i);
%eval = "%dfv = " @ %object @ "." @ %dfn @ ";";
eval(%eval);
%eval2 = %n @ "." @ %dfn @ " = " @ %dfv @ ";";
eval(%eval2);
}//end for loop
%target.add(%n);
}
function SimSet::getRandomObject(%this){
%count = %this.getCount();
%count = %count - 1;
%rand = getRandom(0, %count);
%object = %this.getObject(%rand);
return %object;
}//end SimSet getRandomObject()About the author
#2
02/10/2012 (12:25 pm)
The downside to using a t2dSceneObject is that you will get all the overhead that it comes with, such as ticking, physics, etc. However, if your fruit objects are supposed to be game objects then you should most definitely use t2dSceneObject. In fact, you should take those templates and convert them from a ScriptObject to a t2dSceneObjectDatablock.
Torque Owner kupo