Game Development Community

Object Oriented Programming

by Ted "darkhitman" Nubel · in Torque Game Builder · 03/14/2005 (5:30 pm) · 8 replies

From searching on this forum, I've noticed a few topics on OOP, but I didn't really see a post explaining exactly how to implement data structures like Classes into TorqueScript.

So, I decided to ask here for what you people think is the best way to implement classes into TorqueScript. From searching, I saw some stuff that looked to me to be some form of a 'fake' (so to speak) class system, and I was wondering if it would be possible to hard code the classes into the actual C++ source of Torque 2D for integration into the TorqueScript language? Or, if it is possible, would it be easier to use the TS form of classes?

The reason I ask is because I need to have a large number of similar objects in the same scene, and I needed an efficient way of handling them. I was planning on using a single ENTITY class with a nextobj* pointer to the next object in the list and having an update loop cycle through the entities (with an inheritance tree, as well), but I don't know if I can still do that through TorqueScript.

Any ideas on how I could accomplish this?


Edit: Sorry for the triple-post. The forum was lagging a lot.

#1
03/14/2005 (5:38 pm)
Uh.. check out some of the early posts in this forum. There was a good one on the subject.
#2
03/14/2005 (6:00 pm)
T2D has the ScriptObject and ScriptGroup, which both implement object oriented programming. Look up the documenation for them, and you will be able to do whatever you want with them. Just as an added note, if you want destructors and constructors, just use onAdd and onRemove. What the heck, here is a quick example of a ScriptObject:
function MyClass::onAdd(%this)
{
   echo("Constructor is being called!");
}

function MyClass::onRemove(%this)
{
   echo("Destructor is being called!");
}

function MyClass::echoPhrase(%this)
{
   echo(%this.fancyPhrase);   
}
Then in a function somewhere try this:
%myObject = new ScriptObject(MyClass)
{
   fancyPhrase = "T2D is for me!";
};

%myObject.echoPhrase();

%myObject.delete();
If you look at the above code, MyClass can be whatever you wish to call your class. If you look at the code where the object is created, you will notice the code block under it. This is just a fancy way to pass parameters to an object on creation. If you run that code you will get output in the console something like the following:
Constructor is being called!
T2D is for me!
Destructor is being called!
You can add parameters to any function define, but the first parameter will always be a reference to the class itself. If you have ever used python, Torque Script works exactly the same in that respect.

Now if you want to get really fancy you can use script groups. They are essentially dynamic arrays, that you can ever subclass if you want. I won't go into too much detail with them, but will give you enough information about them to get started. Here is a simple example of using a ScriptGroup:
%myObject1 = new ScriptObject(MyClass)
{
   fancyPhrase = "T2D is my master!";
};

%myObject2 = new ScriptObject(MyClass)
{
   fancyPhrase = "His (or her) call i must obey!";
};

%myGroup = new ScriptGroup();
%myGroup.add(%myObject1);
%myGroup.add(%myObject2);

for(%c = 0;c% < %myGroup.getCount();c%++)
{
   %myGroup.getObject(c).echoPhrase();
}

%myGroup.delete();

Its really just that easy. On thing to keep in mind with script groups is that an object can only be contained in one script group at a time. If you add the object to another script group, it will remove it from that script group it was previously in. The really cool thing about script groups, is that when you delete the group, it automatically deletes all of the objects in that group. I hope this gets you started.
#3
03/14/2005 (7:09 pm)
Thanks, I think those functions will suffice.
#4
03/15/2005 (3:43 am)
Also take a look at the following resource by Bryan Edds Object-Oriented Programming in T2D 1.0.0

It's very easy to follow, and once you've made the changes the zip contains a number of example templates that illustrate how to use oo in torque script.
#5
03/17/2005 (5:34 pm)
Hm... can you add ScriptGroups to ScriptGroups? I was unable to locate the documentation on the functions, and I was wondering because I would need a heirarchy of groups if at all possible.
#6
03/18/2005 (11:11 pm)
It should work, although I never tried it. ScriptGroups inherit from ScriptObjects and SimGroups.
#7
03/25/2005 (10:53 pm)
Can you add datablocks to scriptgroups?
#8
03/26/2005 (9:05 am)
@Joe: As far as I understand it, datablocks are not really objects, but more like templates that can set the properties of objects. So I think you can only reference them as globals when you create an engine object, rather than being able to contain them inside an object.