Game Development Community

Reference for object oriented scriptobjects and such?

by Jacob Wagner · in Torque Game Builder · 11/24/2006 (1:59 am) · 2 replies

I used to have a copy of some text that matthew langley wrote about scriptobjects and how to use them,
however a search for 'scriptobject' and 'matthew langley' together didn't find it. Basically, the text described how you create scriptobjects, how you inherit values, and what you could do with them.

Is there a new reference to similar material somewhere? I perusal of TDN and the torquescript overview and reference docs didn't seem to turn up anything.

#1
11/24/2006 (8:21 am)
The syntax is the same as for other TS objects. I'm not sure if the colon operator works like the copy constructor of datablocks but a quick test should solve that for you. It would be worth you trying it out and posting it back here for posterity.

$myObj = new ScriptObject(ANameForIt : ScriptObjectToInherit)
{
   myVar = "1.01";
   config = "myConfigDatablock";
};

The " : ScriptObjectToInherit" is what I'm not sure will work. Create another script object called that and see.
#2
11/28/2006 (1:36 pm)
Here is the block I used to post about it (dug it up)


Script objects also have three levels of inheritence... say we created a base class like this
and add some starting values to it

function createEntityClass()
{
   new ScriptObject(Entity){
      health = 100;
      mana = 250;
      strength = 10;
   };
}


now lets make our own onAdd function attached to this "Entity" object which will get called everytime an entity is created...

function Entity::onAdd(%this)
{
   echo("New Entity added -" SPC %this.getName());
}

then we can create a child class of it, like this

function createOrcClass()
{
   new ScriptObject(Orc) {
      class = Entity;   
   };
   Orc.health = Orc.class.health + 50;
   Orc.strength = Orc.class.strength + 5;
}

(I did the health and strength that way so it would build off of its class)

run the createEntityClass(); function in the console and you should see this
Quote:
New Entity added - Entity

then you should see this in the console when createOrc(); function is run

Quote:
New Entity added - Orc

now you can create a function like this

function createOrc(%name)
{
   new ScriptObject(%name){
      class = Orc;
      superClass = Entity;
   };
}

then just call it like this

createOrc(Bob);
and if you want to overide the entity onAdd you can do so by adding one to Orc

function Orc::onAdd(%this)
{
   echo("New Orc of added -" SPC %this.getName());
}

and it will fire when you create an orc

now to test this add

function Entity::yell(%this)
{
   echo("This yelled -" SPC %this.getName());
}

then whatever orcs you created with "createOrc" call ".yell();" on them and you should see the console react properly