Game Development Community

Object References in Variables

by Johannes Pauw · in Torque Game Builder · 02/14/2008 (2:24 pm) · 1 replies

I'm having a strange problem, and I can't figure out why it's happening or really what's going on.
I created a simobject as a quick wrapper for an array holding the units for one side in my game:

$RedUnits = new SimObject();

I then create a static image (the hull of a tank), and then mount various parts onto it (the turret, a particle effect of dust trailing the wheels, and a health bar), and put them in an array contained in the simobject:

$RedUnits.contents[0] = createunit(RedTankHull, "RedTank", "Red", 4, 4, 0, 9, 5, 3, 2, 40, 3, 5, 4);
$RedUnits.contents[0].turret = mountItem(RedTankTurret, "RedTankTurret", "Red", $RedUnits.contents[0]);
$RedUnits.contents[0].dust1 = adddust($redUnits.contents[0], 0.7, 0.45);
$RedUnits.contents[0].dust2 = adddust($redUnits.contents[0], 0.7, -0.45);
$RedUnits.contents[0].HealthIndicator = addHealthIndicator($redUnits.contents[0]);

Each function simply makes the object, sets its values, and returns it.
Now, everything works fine and dandy, I can reference say the turret through echo($RedUnits.contents[0].turret), it'll give me back the object's number. I can also echo back variables contained in the turret staticimage. It seems to be totally fine. However, after passing the original staticimage (the hull that is in $RedUnits.contents[0]) through a series of functions to process things, it seems that it loses the reference to the turret, the dust, and the indicator.


I pass the red and blue unit array wrappers ($RedUnits and $BlueUnits) into this function:
function ProcessShooting(%UnitArray1, %UnitArray2)

Then after doing some processing, I pass a specific pair of units into another function:

shot(%UnitArray1.contents[%a], %UnitArray2.contents[%b]);

function shot(%unit1, %unit2)
{
fireAnimation(%unit1, %unit2);
}

function fireAnimation(%unit1, %unit2)
{
%unit1.turret.setRotation(angleBetween(%unit1.getPosition(), %unit2.getPosition()));
%unit1.turret.flash.playEffect();
}

Now, I can still echo the name of the %Unit1 and %Unit2 variables, at every stage along this path, but even by the time I call the 'shot' function, it seems that the reference to the turret, the dust, and the indicator have disappeared. %unit1.turret.flash is a particle effect that was created earlier and mounted onto the turret itself. It can't be seen either... Regardless, if I try and echo %unit1.turret, I get nothing, and there's constantly a series of runtime errors that say:

game/gameScripts/shooting.cs (45): Unable to find object: '' attempting to call function 'getPosition'
game/gameScripts/shooting.cs (45): Unable to find object: '' attempting to call function 'setRotation'
game/gameScripts/shooting.cs (46): Unable to find object: '' attempting to call function 'playEffect'

I'm really completely lost... For some reason, the references to the turret, the dust, and the health indicator exist when I try and access them immediately after I create them, in my startup function, but by the time the game starts running and it reaches the firing functions, they're gone.

Am I missing something simple? Or is there some torquescript subtlety here in the way it's managing variable names? Any help would be very appreciated.

#1
02/14/2008 (3:47 pm)
Firstly, you should use a SimSet, like so:

$RedUnits = new SimSet();

%redTank = createunit(RedTankHull, "RedTank", "Red", 4, 4, 0, 9, 5, 3, 2, 40, 3, 5, 4);
%redTank.turret = mountItem(RedTankTurret, "RedTankTurret", "Red", $RedUnits.contents[0]);
%redTank.dust1 = adddust($redUnits.contents[0], 0.7, 0.45);
%redTank.dust2 = adddust($redUnits.contents[0], 0.7, -0.45);
%redTank.HealthIndicator = addHealthIndicator($redUnits.contents[0]);

$RedUnits.add(%redTank);

SimSets store objects, similar to your array, except it is easier to manage your objects. To get the id's of the objects inside the set, you just use "$RedUnits.getObject(%i);" (where %i is the index of the object).

In terms of your specific problem, have you ensured that the object you create has the field "CanSaveDynamicFields" equal to true? It sounds to me as if it cannot.