Game Development Community

Design Principle Question: Objects in Torque and TorqueScript

by Ingo Seidel · in Torque Game Engine · 06/01/2006 (9:18 am) · 7 replies

Hello!

I have a general design question concerning the implementation of objects with Torque. I want to implement a (non trivial) object hierarchy and originally planned to use Torque Script to do this task. Now as I took a closer look at TScript I realized that object declaration is somehow unhandy in TScript. (You can only use the SimObject to declare own types, where the types have to be specified through variables. OOP features are not build in. Also I am uncertain if you can instantiate these objects later in your program.).

Then I saw that the second possibility is to code my objects in the game engine itself as C++ Code. Then I can write wrapper methods for TScript and instantiate my objects in TScript. This again has the disadvantages that you always have to recompile the source code and that I am not sure weather this is the right place for such code.

My question is: If I want to declare my own objects for Torque is it better to do it in TScript or by extending the game engine code?

thanks

#1
06/01/2006 (10:43 am)
What do you call a "non trivial object hierarchy" ?

Give us some examples of what exactly you want and we'll be able to help you further.
#2
06/01/2006 (11:45 am)
I just meant an object hierarchy where you have things like abstract classes and inheritance. For example I will have to implement a hierarchy of message objects, where you have an abstract message AM at the top then some spezialiced abstract messages AM1,...,AMN that inherit from AM and lots of concrete message objects CM1,...,CMK that inherit from AM1,..,AMN.
#3
06/01/2006 (2:48 pm)
Quote:
You can only use the SimObject to declare own types, where the types have to be specified through variables. OOP features are not build in.
I really don't understand what you mean. Could you please elaborate?

BTW, according to the GPGT book, the right tool to declare script classes is by deriving from class ScriptObject, not SimObject.
#4
06/02/2006 (8:25 am)
Maybe the term object was not appropriate in my prior posts, it should rather mean class. Now to declare your own classes you have to write something like:

%instance = new ScriptObject(MyObjectName)
{
    class  = MyObjectClass;
    data = "something";
};

function MyObjectClass::doSomething(%this)
{
   echo(%this.data);
}

i.e. you have to use ScriptObject to declare your class and the type of your class has to be specified through a member variable. If you want inheritance, I found a OOP resource which lets you subclass other classes. Then you can write something like

link(MyClass, null, ScriptObject);

function MyClass::create(%objectName)
{
   %this = new ScriptObject(%objectName)
   {
        class = MyClass;
   }
   MyClass::protInit(%this);
   return %this;
}

function MyClass::protInit(%this)
{
    %this.var1 = "parentclass";
}

link(MySubClass, MyClass, ScriptObject);

function MySubClass::create(%objectName)
{
   %this = new ScriptObject(%objectName)
   {
       class = MySubClass;
   }
   MySubClass::protInit(%this);
   return %this;
}

function MySubClass::protInit(%this) 
{
   Parent::protInit(%this);
   %this.var2 = "subclass";
}

In my oppinion the above code fragements do not look very nice: you always have to pass a reference to your object in a class method (you cant write %instance.doSomething() but must write MyClass::doSomething(%instance)). You must specify your class type through member variables. The inheritance is just attached and not built in.

Now I thought that if I use Torque Script to code my class hierarchies this code will become very cumbersome and hard to maintain. This is why I thought about implementing my class hierarchy in C++.

In general I just want to know if you rather use Torque Script to implement class hierarchies or if C++ would be a better choice? Suppose you have three class hierarchies with approximately 40 classes which have 4 levels of inheritance, would you implement this in Torque Script or in C++? (you want to access some of these classes from script code).
#5
06/02/2006 (8:35 am)
Personally, I would go with your second implementation direction--TGE is designed to allow for basic "new class" creation in TorqueScript, but the primary technique for complex hierarchies with full OOP is simply to implement your classes in C++ (derived from the appropriate place in the stock hierarchy--SimObject at a minimum must be in your inheritence chain), and use console methods, callbacks, and the other available interaction techniques to allow script to manipulate your objects.

It's also the primary tactic I teach in all of the Torque Boot Camps (except for TGB, which is designed slightly differently from core Torque in this aspect).
#6
06/02/2006 (8:53 am)
Actually I never tried it, but according to The Book, ScriptObject has a "superClass" field that let you link with the superclass namespace withouth the link/protInit code you posted. That code really looks like a hack to me, making the code unreadable. I wouldn't use it if I were you. What you get with the superClass field is only a two-layer inheritance, not true inheritance, but that's enough for a script language. Deeper hierarchies are best done in C++.
#7
06/02/2006 (9:07 pm)
Hubertus, I definately second Stephen's sentiments. If you actually need inheritance features- and message trees are a great example of that- then C++ is really the only way to go. It's just so much easier to manage. It is really easy, as Stephen said, to derive new objects in Torque, just be sure to start at an appropriate level- SimObject at the very least. Check the code documentation for SimObject, SimDatablock and NetObject for some very good, quick tutorials on how to do it, and there's also a few resources floating around, as well as entries in the TDN- I suggest looking at all of them. For the most part, it's possible to treat console/script useable classes like any other at the C++ level, so if you are comfortable in C++ (and you sound like you are) by all means do it there!

And in any case, the more of the engine source you read the easier scripting gets because you see how it all falls together.