SCRIPTOBJECTS
When an object is created, it is created with an identifier and a name. The identifier is unique, but that’s not always the case with the name.
To create a new ScriptObject write this:new ScriptObject(foo);
This creates a new ScriptObject called foo. It's ID is unknown, but the prior command will return the identifier. Therefore you can do this:$myFoo = new ScriptObject(foo);
$myFoo equals to the identifier of the ScriptObject called foo. E.g: "echo($myFoo);" => "1337"
These ScriptObjects can contain both data and functions, which are called "methods and fields", when we're dealing with objects. To assign a value to a given field, you can do these commands:foo.data = "bar";
or$myFoo.data = "bar";
or1337.data = "bar";
These 3 commands will do the same thing in this context. However, if you have more than 1 object called foo, the game won't know which one you want to reference. Therefore you could assign the wrong object's field called "data" to "bar". This is why you want to make sure that the names are unique OR that you keep track of your objects' identifier.
This works with arrays too:$myFoos[0] = new ScriptObject(foo);
$myFoos[1] = new ScriptObject(foo);
Now if you write$myFoos[0].data = "bar";
$myFoos[1].data = "bahh";
echo($myFoos[0].data SPC $myFoos[1].data);
It will return "bar bahh" in console. There are other ways to be sure you "talk" about the right object too. Anyway, let's move on to something very important: Classes.
With classes, you can create objects that start out with functionality that you have defined. To create an object with a class, you do either:
$myObject = new ScriptObject(foo)
{
class = myClass;
};
or$myObject = new ScriptObject(foo);
$myObject.class = myClass;
Hooray! We've created our first class. As we mentioned earlier, an object can contain
functions. These are called methods. To create a method for the class "myClass”, you can do this:function myClass::myMethod(%this)
{
echo("The object with the identifier” SPC %this SPC "called myMethod”);
return "yay”;
}
NOTE: When an object’s method is called, the first parameter equals to the identifier of the object. Therefore we can set data or call other methods for this object.
Now since the object we created earlier has the class "myClass”, we can run the method by writing:$myObject.myMethod();
NOTE: Be careful to not mistake fields for methods. $myObject.myMethod() returns "yay”, but $myObject.myMethod
does nothing, since without the brackets the game will consider $myObject.myMethod as a FIELD!
When an object contains methods, it is because it has inherited it. An object can inherit methods from 3 places:
• A class.
• A superclass.
• The object’s name.
To concretize this, let’s imagine we have a cat called Bill. Animals can eat things, therefore Bill can eat things. But Bill is a cat too, therefore he can purr! Bill is very special cat as well. That’s because he can do a cool trick, where he solves a Rubik’s cube. If Bill was a ScriptObject, he would look like this:$myPet = new ScriptObject(Bill)
{
superclass = Animal;
class = Cat;
};
function Animal::eat(%this, %target)
{
//insert eat code
%this.hunger = 0;
echo(%this.getName() SPC "has eaten” SPC %target);
}
function Cat::purr(%this)
{
//insert purr code
echo(%this.getName() SPC "won’t stop purring!”);
}
function Bill::solveRubiksCube(%this, %cube)
{
//insert your simple solving code
echo(”Wow!” SPC %this.getName() SPC "actually solved" SPC %cube SPC ", a Rubik’s Cube. Does Mensa accept cats?”);
}
Now we’ve created 3 methods with 3 different namespaces. But since Bill’s class is "Cat”, his superclass "Animal” and his name is "Bill” – he can use ALL the methods within these namespaces! That’s great for Bill.
This means these three commands will work:$myPet.eat("Tonce”);
$myPet.purr();
$myPet.solveRubiksCube( new ScriptObject(cube1) { class = RubiksCube; } );
This will output:Bill has eaten Tonce
Bill won’t stop purring!
Wow! Bill actually solved (objectIdentifierHere), a Rubik’s Cube. Does Mensa accept cats?
ScriptObjects are very useful because, it introduces Object-oriented programming into TorqueScript.
Some helpful commands:obj.dump(); - Dumps all methods and fields for a specified object.
tree(); - Opens a GUI where you can inspect objects in their hierarchy.
Sorry for the semi-stuffty layout. If something is off (either content or layout), please inform me.