Author Topic: Scriptgroups/Scriptobjects (eli5 please)  (Read 2798 times)

i really don't get the purpose of them at all. i haven't needed to use one but they sound extremely useful.
what are they and what do they do?

Scriptobjects are essentially just perfectly generic objects. You create them, assign values to them, create methods for them... They have no unique properties, you make all the properties for them.

Code: [Select]
$world = new ScriptObject(WorldObject)
{
   name = "Earth";
   chanceOfNukes = 10;
};

function worldObject::getName(%this)
{
   return %this.name;
}

function worldObject::makeNukes(%this, %val)
{
   %this.chanceOfNukes += %val;
}

$world.makeNukes(1);

echo($world.getName() SPC "has a" SPC $world.chanceOfNukes @ "% chance of obliterating itself.");
//Prints "Earth has a 11% chance of obliterating itself."

for an example of how powerful scriptobjects can be, minigames are actually scriptobjects of the MiniGameSO class

also, scriptobjects and scriptgroups can have three defined namespaces associated with them, which is super-useful at times (name, class, superClass)

Scriptgroups allow you to add objects to them using the .add() method. You can iterate through all the object a scriptgroup using the .getCount() and .getObject() methods.
For example, if you ever want to perform an action against all client objects, you would iterate through the ClientGroup object
(Except that ClientGroup is probably a SimGroup instead of a ScriptGroup, the difference between the two being that an object can only belong to one SimGroup [which can be retrieving using .getGroup()] but can belong to multiple ScriptGroups)

(Except that ClientGroup is probably a SimGroup instead of a ScriptGroup, the difference between the two being that an object can only belong to one SimGroup [which can be retrieving using .getGroup()] but can belong to multiple ScriptGroups)

The ScriptGroup class inherits from the SimGroup and ScriptObject classes, and as such the same applies to it; it's not just SimGroup objects.

The ScriptGroup class inherits from the SimGroup and ScriptObject classes, and as such the same applies to it; it's not just SimGroup objects.
Ok, it was SimSets I was thinking of in which objects can belong to more than one
So then is there any functional difference between scriptgroups and simgroups?

Scriptobjects are very useful for holding data, and can be used in an efficient databasing system (as it is the fastest)

An example of lets say 2D vectors:

Code: [Select]
function vector2(%x, %y)
{
    %vec = new ScriptObject()
    {
        x = %x;
        y = %y;
    };
        
    return %vec;
}

...

$myVec = vector2(2,5);

...

echo($myVec.x);
« Last Edit: May 25, 2014, 01:15:05 AM by Wrapperup »

Ok, it was SimSets I was thinking of in which objects can belong to more than one
So then is there any functional difference between scriptgroups and simgroups?

You cannot define methods on a SimGroup, while you can on a ScriptGroup.


The overhead of creating an object isn't worth it for 2D vectors at all. In TorqueScript, using a string is better (as long as you don't call getWord over and over; only do it once for each dimension).

You cannot define methods on a SimGroup, while you can on a ScriptGroup.

I think you mean that you can't use class and superClass on them. Everything else works just fine.

The overhead of creating an object isn't worth it for 2D vectors at all. In TorqueScript, using a string is better (as long as you don't call getWord over and over; only do it once for each dimension).

Its just an example showing what you could do with scriptobjects

Script objects allow you to use object-oriented programming in Torque. Objects and classes make things a lot easier and organized. Although ScriptObjects are not true objects made from true classes, they still make your life easier.