Newbie Q - alias / pointer in TorqueScript???
by Dayton Williams · in Torque Game Builder · 10/15/2006 (4:01 pm) · 1 replies
I need to create an array of ... references / pointers / aliases to existing objects.
Short version: There exist > 1 object the player can manipulate. One is the "hot" object, being the object that is supposed to consume keyboard input. You can change the "hot" object using the equivalent of "next" and "prev".
I am attempting to implement this via an array of aliases, and an integer representing the index.
In C++ this would be something like
playercharacter *thePCs[numPCs]; // array of pointer-to-playercharacter
int currPC = 0;
and you would change currPC to change which PC was currently selected.
I have tried implementing this in script, to find it doesn't seem to work. My attempt appears to create new objects instead of aliasing the existing ones. I know this because I set it to print out a field I know is set, and it doesn't print out anything.
B0rked code follows
init is called and then the keyboard callbacks are set. The intent is to have a member variable changed on pc_one; this doesn't seem to be happening.
pc_one and pc_two were created in the Level Builder. (There are only two, but more will exist later. I must successfully walk before running :)
====
// Use arrow keys to change active characters (eventually mouse-clicks?)
function inputMgr_init()
{
// I need to declare an array x[$numPCs]. I think it just works?
$arr_PClist[0] = pc_one;
$arr_PClist[1] = pc_two;
$im_currPC = 0;
}
function battle_Up() { %dummy = 0; } // quash; only generate 1 movement per keypress
function battle_UpStop()
{
$arr_PClist[$im_currPC].tx--; // Change tile-x position of current PC
echo($arr_PClist[$im_currPC].name);
//pc_two.tx--;
//pc_two.updateScreen(pc_two);
}
function inputMgr_battle_bindKeys(%this, %scenegraph)
{
echo("Test1");
menuKeymap.pop(); // disable previously-active maps
battleKeymap.push(); // enable current map
battleKeymap.bindCmd(keyboard, "w", "battle_Up();", "battle_UpStop();");
}
Short version: There exist > 1 object the player can manipulate. One is the "hot" object, being the object that is supposed to consume keyboard input. You can change the "hot" object using the equivalent of "next" and "prev".
I am attempting to implement this via an array of aliases, and an integer representing the index.
In C++ this would be something like
playercharacter *thePCs[numPCs]; // array of pointer-to-playercharacter
int currPC = 0;
and you would change currPC to change which PC was currently selected.
I have tried implementing this in script, to find it doesn't seem to work. My attempt appears to create new objects instead of aliasing the existing ones. I know this because I set it to print out a field I know is set, and it doesn't print out anything.
B0rked code follows
init is called and then the keyboard callbacks are set. The intent is to have a member variable changed on pc_one; this doesn't seem to be happening.
pc_one and pc_two were created in the Level Builder. (There are only two, but more will exist later. I must successfully walk before running :)
====
// Use arrow keys to change active characters (eventually mouse-clicks?)
function inputMgr_init()
{
// I need to declare an array x[$numPCs]. I think it just works?
$arr_PClist[0] = pc_one;
$arr_PClist[1] = pc_two;
$im_currPC = 0;
}
function battle_Up() { %dummy = 0; } // quash; only generate 1 movement per keypress
function battle_UpStop()
{
$arr_PClist[$im_currPC].tx--; // Change tile-x position of current PC
echo($arr_PClist[$im_currPC].name);
//pc_two.tx--;
//pc_two.updateScreen(pc_two);
}
function inputMgr_battle_bindKeys(%this, %scenegraph)
{
echo("Test1");
menuKeymap.pop(); // disable previously-active maps
battleKeymap.push(); // enable current map
battleKeymap.bindCmd(keyboard, "w", "battle_Up();", "battle_UpStop();");
}
About the author
Torque 3D Owner Matthew Langley
Torque
One thing to keep in mind with TorqueScript and TGB is you really are only references to objects... for example:
If you give an object the name "testObject"
then do
testObject.setPosition(5, 5);
you are just referencing that name... now all TGB object have a unique ID number... you can get it via
testObject.getID();
that way you can just swap reference IDs (which will garantee a unique reference unlike names)
When you create objects via script the ID is returned... for ex:
so %obj isn't holding the object, just the ID number... so when you do
%obj.blah = "test";
its the same as using the id number
1523.blah = "test";
or the name if you pass a name
testObj.blah = "test";