Game Development Community

Scriptobject handle

by John Coyne · in Torque Game Builder · 02/16/2006 (5:39 am) · 11 replies

Hi
I'm trying to come up with a way of creating scriptobjects when i need them, and storing them all in relevant simsets. Trouble is, once the scriptobject is created it seems to dissapear into the cosmos. I need to know how to keep a hold of the scriptobjects i'm creating. Heres what i have so far:

//infant Script

new ScriptObject(Infant)
{
postion = "0 0";
size = 10;
speed = 5;
name = "NULL";
type = "infant";
state = "idle";
};

function infant::onAdd()
{
echo("Infant created");
}

function createInfant(%obj)
{
new ScriptObject(%obj)
{
class = Infant;
name = %obj;
};
%obj.dump(); //this doesnt work here
}


------------------------


//onstartup.cs

//create a simset to put our infants in
$playerInfants = new simset();

createInfant(%newInfant);

%newInfant.dump();

//create a single infant object
$playerInfants.add(%newInfant);


----------------------


none of the dump calls work, giving an "unable to find object" error. Adding to the simset gives an "object not found" error. So. is there a way to keep a variable pointing to my scriptobjects long enough to put them into the simset?

#1
02/16/2006 (5:43 am)
Have you tried...

new ScriptObject(infant);

$simS.add(infant);


or....

$simS.add(%handle.getId());
#2
02/16/2006 (5:46 am)
I need lots of infants though, so i need some way of doing this inside a createInfant function.
#3
02/16/2006 (5:48 am)
First you have to get it working.

function createInfant(%obj)
{
   %obj = new ScriptObject(infant)
   {
      class = Infant;
   };
   infant.getID().dump(); // Does this work?
   %obj.getID().dump(); // Does this work?

   // if either of those ^ works, then this will work:
   $simS.add(%obj.getID());
   // or this:
   $simS.add(infant.getID());
}
#4
02/16/2006 (5:52 am)
Thanks, i think i have it working now but i'm gonna log off for a bit before i really test it propery. Got super bad eye strain working on this.
#5
02/16/2006 (6:59 am)
Welcome to the life of a coder :P

Could you please post the version that worked, in case anyone stumbles across this thread with the same problem.
#6
02/16/2006 (7:01 am)
No probs, I'm looking to add a small state machine into this too (its a sim type game), so maybe i can submit it as a resource once i have it all going.
#7
02/16/2006 (7:51 am)
Looks ok now so heres the code:

infant.cs
.............

//infant Script

new ScriptObject(Infant)
{
postion = "0 0";
size = 10;
speed = 5;
name = "NULL";
type = "infant";
state = "idle";
};

function infant::onAdd(%this)
{
echo("Infant " @ %this.name @ " created");
}

function createInfant(%obj)
{
%objhandle = new ScriptObject(%obj)
{
class = Infant;
name = %obj;
};
return %objHandle;
}

------------------------------------------------------

onStartup.cs
....................

//onstartup.cs

//create a simset to put our infants in
$playerInfants = new simset();

//create some infant object
for(%unitCnt=0;%unitCnt<=5;%unitCnt++)
{
%newInfant = "Infant" @ %unitCnt;
$playerInfants.add(createInfant(%newInfant));
}

//list all the infants in the simset to check its working
$playerInfants.listObjects();


---------------------------------------


The guts of this came a post Matthew "King Tut" Langley made here:

http://www.garagegames.com/mg/forums/result.thread.php?qt=35475

All i've done is modified it a little to my own needs. I think this is probably a bit simpler as it doesnt bring inheritence into the equation, so, hopefully it might be usefull to someone.
#8
02/16/2006 (7:57 am)
I thought you said that this didn't work:

$playerInfants.add(createInfant(%newInfant));

and please encase your code blocks in tags like this: [.code]code here[./code] (remove the .'s)


I reread your code... Ya, your method won't work because youre trying to use the assigned name when adding the object to the simSet. You eneded to be using the object handle.
#9
02/16/2006 (8:13 am)
It didnt work earlier when i used the assigned name to try adding into the simset, it appears to be working now though, I get this in the console window:

the 1st block is from the onAdd menthod, the 2nd block is a list of objects in the simset.


Infant Infant0 created
Infant Infant1 created
Infant Infant2 created
Infant Infant3 created
Infant Infant4 created
Infant Infant5 created
1756,"Infant0": ScriptObject
1757,"Infant1": ScriptObject
1758,"Infant2": ScriptObject
1759,"Infant3": ScriptObject
1760,"Infant4": ScriptObject
1761,"Infant5": ScriptObject

I was under the impression that the text that goes here

new scriptobject(IN_HERE);

is just a name for the scriptobject, and that the handle gets returned from that call. My reasoning was to store the object handle, and return it from the create infant function and into an add to simset call. Like this:


$playerInfants.add(createInfant(%newInfant));   //create an infant and add it into the simset



function createInfant(%obj)
{
   %objhandle = new ScriptObject(%obj)  //create the object and get the handle
   {
      class = Infant;   //we want an infant object
      name = %obj;      //copy the name into our name field
   };
   return %objHandle;   //return the handle so we dont loose the object
}

Thought i had it going, have i screwed up again? :S
#10
02/16/2006 (8:21 am)
No man, that looks perfect :)
#11
02/16/2006 (8:22 am)
Cool, I think the last line of your previous post confused me. :)