Game Development Community

Torquescript classes: what and how?

by Fredrik Aron Riktor · in Torque 3D Professional · 08/29/2011 (6:06 am) · 3 replies

The documentation says that you can create classes using either C plus plus or Torquescript. It does not seem to contain any further details on the subject of Torquescript classes.

I'm investigating the possibility of writing a pregame client/server interaction by inheriting from either GameConnection or NetConnection, preferably keeping all the changes within Torquescript.

Existing code suggests that classes are instances of a class definition-class, and that there might not be a separate class definition syntax.

How would the definition of a class called Preconnection look into Torquescript?

#1
08/29/2011 (7:34 am)
Torque Script can't create "real" classes, like you can in C++, so if you want to create a subclass of GameConnection, you will have to do so within the C++/engine side of things and expose it to script (much like GameConnection itself does).

With Torque Script, you can create namespaces, but those are not classes, strictly-speaking (so making a bunch of functions like "sample::blah()", "sample::blah2()", etc, does not create a class named sample). If you take a look at the engine, you'll see that all objects that you can create and use in Torque are classes in the engine exposed to script.

Hope that helps.
#2
08/29/2011 (8:11 am)
It adds some clarity, which might help me pick an approach. Perhaps I don't require an actual class. I shall fiddle about and see what comes of it. Thanks.
#3
08/29/2011 (8:15 am)
You can use ScriptObjects as pseudo-classes but, as Ted said, trying to create a subclass of a C++ class in TScript isn't going to work.

Some basic code to give you the gist of ScriptObjects:

// Define a "base" class that holds some misc. variables
new ScriptObject(BaseClass)
{
   aVal = 1;
   aValToo = 0.5132;
   aString = "I'm a string!";
};

function BaseClass::printVars(%this)
{
   echo(%this.aVal);
   echo(%this.aValToo);
   echo(%this.aString);
}

// Define a child class that inherits methods (and ONLY methods) from the parent class (or superClass)
new ScriptObject(ChildClass)
{
   superClass = BaseClass;

   class = ChildClasses;

   aVal = 2; // different than parent class to show functionality
};

function ChildClasses::printParentVars(%this)
{
   %this.superClass.printVars();
}

// Define another  child class that inherits methods AND variables from the parent class (or superClass)
new ScriptObject(ChildClass2 : BaseClass)
{
   superClass = BaseClass;

   class = ChildClasses;

   aVal = 3; // different than parent class to show functionality
};
echo("Printing BaseClass vars...");
BaseClass.printVars();

echo("Printing ChildClass vars...");
ChildClass.printVars();
echo("Printing ChildClass superClass vars...");
ChildClass.printParentVars();

echo("Printing ChildClass2 vars...");
ChildClass2.printVars();
echo("Printing ChildClass2 superClass vars...");
ChildClass2.printParentVars();