Author Topic: [INFORMATION] ScriptObjects  (Read 3301 times)

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:

Code: [Select]
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:
Code: [Select]
$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:

Code: [Select]
foo.data = "bar";or
Code: [Select]
$myFoo.data = "bar";or
Code: [Select]
1337.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:

Code: [Select]
$myFoos[0] = new ScriptObject(foo);
$myFoos[1] = new ScriptObject(foo);
Now if you write
Code: [Select]
$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:
Code: [Select]
$myObject = new ScriptObject(foo)
{
class = myClass;
};
or
Code: [Select]
$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:

Code: [Select]
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:
Code: [Select]
$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:

Code: [Select]
$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:

Code: [Select]
$myPet.eat("Tonce”);
$myPet.purr();
$myPet.solveRubiksCube( new ScriptObject(cube1) { class = RubiksCube; } );
This will output:
Code: [Select]
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:
Code: [Select]
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.
« Last Edit: October 25, 2015, 07:37:34 AM by Quartz »

Reserved... maybe?
« Last Edit: October 23, 2015, 06:32:32 PM by Quartz »

Did you actually test out the bill::solverubix function?

Edit: Did you test out any of this? Because this isn't how you use simObjects lol.
« Last Edit: October 23, 2015, 06:26:44 PM by elm »

Did you actually test out the bill::solverubix function?
Oh stuff. All the quote symbols are somehow... different, hang on.

Reserved... maybe? BY THE WAY SOMEBODY TELL ME THE DIFFERENCE BETWEEN SIMOBJECTS AND SCRIPTOBJECTS AAAAAAA! (Other than ScriptObjects inherit from SimObjects)

ScriptObjects implement the class and superClass attributes. SimObjects do not.

Me and Trinick were talking about this a while ago, it seems that simObjects and simSets won't allow you to associate functions with them. The weird part is that doing echo(isFunction(simObject,funcName)); returns true, however when you try calling them, it doesn't work.

On the other hand, you can do everything you posted in your original post, with Script Objects/Script Groups.

ScriptObjects implement the class and superClass attributes. SimObjects do not.
Oh god, thank you! Time to edit.

Me and Trinick were talking about this a while ago, it seems that simObjects and simSets won't allow you to associate functions with them. The weird part is that doing echo(isFunction(simObject,funcName)); returns true, however when you try calling them, it doesn't work.
That is really odd.

On the other hand, you can do everything you posted in your original post, with Script Objects/Script Groups.
Let's just pretend I was talking about that all along. I should have gone to bed earlier.
« Last Edit: October 23, 2015, 06:34:01 PM by Quartz »

It's cool, you should probably test things out before you hit the post button.

I'll add this to the resource topic too once you fix it.

It's cool, you should probably test things out before you hit the post button.

I'll add this to the resource topic too once you fix it.
I'll pm you, once I've gone through it.

In one of the example functions, why are you returning the function before the echo?

Please check your code before you give an example of it.

In one of the example functions, why are you returning the function before the echo?

Please check your code before you give an example of it.
Yes, ty.

Code: [Select]
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
this will echo "$myFoos[0].data $myFoos[1].data"

this will echo "$myFoos[0].data $myFoos[1].data"
Oops, what the hell was I thinking. Hahaha.