Game Development Community

How can I get int data out of an array

by ALFRED GATT · in Torque Game Builder · 05/31/2005 (6:26 pm) · 1 replies

I just can't seem to get data out of an array when I echo the data to the console it shows a blank line.

I would like to represent 100 levels with 100 ints per level. (see below 10 ints level 0)

Please help


function loadData()
{
%gameData = new ScriptObject();
//create the second level which will be an array for "levels"
%gameData.level[0] = new ScriptObject();
//create the third level which will be arrays for object arrays
%gameData.level[0].objects[0] = new ScriptObject();
%gameData.level[0].objects[0] = 0;
%gameData.level[0].objects[1] = 1;
%gameData.level[0].objects[2] = 2;
%gameData.level[0].objects[3] = 3;
%gameData.level[0].objects[4] = 4;
%gameData.level[0].objects[5] = 5;
%gameData.level[0].objects[6] = 6;
%gameData.level[0].objects[7] = 7;
%gameData.level[0].objects[8] = 8;
%gameData.level[0].objects[9] = 9;
}

function ResetLevel()
{

%mIdx = 0;
%posX1 = -18;
%posY1 = -21.5;
%random = 0;

for (%mIdx = 0; %mIdx < 10; %mIdx++)
{
echo("Level Position");
echo(%mIdx);
echo(%posX1);
echo(%posy1);
//%random = GetRandom(0,12);
%obj = %gameData.level[0].objects[%mIdx];
echo(%gameData.level[0].objects[%mIdx]);
switch(%obj)
{
// Default Single Projectile.
case 1:
CreateHook(%posX1,%posY1);
case 2:
CreateBoot(%posX1,%posY1);
case 3:
CreateAnchor(%posX1,%posY1);
case 4:
CreateOneup(%posX1,%posY1);
case 5:
CreateOneFish(%posX1,%posY1);
case 6:
CreateTwoFish(%posX1,%posY1);
case 7:
CreatePlus_25_Pnts(%posX1,%posY1);
case 8:
CreatePlus_50_Pnts(%posX1,%posY1);
case 9:
CreatePlus_75_Pnts(%posX1,%posY1);
case 10:
CreatePlus_100_Pnts(%posX1,%posY1);
case 11:
CreateHole(%posX1,%posY1);
case 12:
CreateOld_Man_Bonus(%posX1,%posY1);
}

%posX1 += 4;
if(%posX1 > 18)
{
%posX1 = -18;
%posY1 += 4;
}
}
}

#1
06/01/2005 (3:01 am)
I think your problem here is that you're using a local variable '%gameData' in the 'loadData()' function and then trying to use it in a different function 'ResetLevel()'. The object reference assigned to '%gameData' will be lost when the 'loadData()' function is exited, although the data itself will remain but will be inaccessible. (In TorqueScript variables use the '%' prefix to indicate that they are local and the '$' prefix to indicate global variables.)

One possible way to get this to work is to give the ScriptObject a name in 'loadData()' as follows:
%gameData = new ScriptObject( theGameData );


(Note: You could use any name you want where I have chosen to use 'theGameData')

Then in 'ResetLevel()' change (all of) the references from '%gameData.' to 'theGameData.'
e.g.
%obj = theGameData.level[0].objects[%mIdx];

As with most programming, there are other ways of course but this should correct your problem.