Game Development Community

Creating classes in torque script

by WDDG · in Torque Game Builder · 02/13/2006 (2:51 pm) · 8 replies

I've tried using the search function to no avail. I'm just curious if anyone can point me in the right direction on how to create classes (or something class like) in torque script.

#1
02/14/2006 (8:27 am)
Getting my head around that just now myself. try looking at scriptobjects and there namespaces. If that doesnt help, look for the resources submitted by Bryan Edds.
#2
02/14/2006 (8:49 am)
new scriptObject(ed){ class = edClass; };

than you can use such functions as 

function edClass::onAdd(%this) {}
function edClass::onRemove(%this) {}
or any other custom method
function edClass::DanceForMe(%this) {}
ed.DanceForMe();

hope that helps
#3
02/14/2006 (11:11 am)
How about passing vars down.

Here's what I have:
function createEnemyClass()
{
   new scriptObject(Enemy)
   {
      health = 1;
      fixAim = 400;
      speed = 20;
      player = $player;
      sizeDevider = 4;
   };
}
function createBlueDiamondClass()
{
   new scriptObject(blueDiamond)
   {
     class = Enemy;
     name = "blueDiamondEntity";
   };
   echo(blueDiamond.name);
}

function createBlueDiamond(%name)
{
   new ScriptObject(%name)
   {
      class = blueDiamond;
      superClass = Enemy;
      imageMap = "blueDiamondImageMap";
      size = 6;
      speed = 15;
      minRotationFixVar = 1000;
      maxRotationFixVar = 250;
   };
}

basically i wanna create a blue dimond and echo "player" from within the diamond and hope it to reference the var created when i created the Enemy class.
#4
02/16/2006 (12:33 am)
What about if you need to store variables?
i'm very c++ oriented, and this whole thing is tripping me up.

for instance i want to make a class called bubble
and i want to have variables like xPos, yPos, xVel, yVel

how do i implement this?
can someone post an example class with functions and variables?

thanks
#5
02/16/2006 (8:23 am)
The method i eventually used is posted here:

http://www.garagegames.com/mg/forums/result.thread.php?qt=40141

It may be of use to some of you
#6
02/16/2006 (9:42 am)
Two points to comment on in James' code--mostly because I'm seeing the trend in several threads:

1) You should keep in mind that the "new" operator always returns the objectID of the newly created object. This is important information, and you shouldn't ever ignore it. More on this in a second.

2) The format in which objects are saved is not the best example for copying directly into code, because when a saved file is read in there are several things going on by the underlying code that will not be done by your pure script. The biggest example of which are some hidden SimSet/SimGroup.add() operations going on that are implied by creating one object within a new object hierarchically.

Here is how I would have written James' functions:

function createEnemyClass()
{
   %newEnemyClass = new scriptObject(EnemyClass)
   {
      health = 1;
      fixAim = 400;
      speed = 20;
//      player = $player; honestly not sure the purpose of this, but it absolutely implies only ever having single player
      sizeDevider = 4;
   };
   return %newEnemyClass;
}

function createBlueDiamondClass()
{
   %newBlueDiamondClass = new scriptObject(blueDiamondClass)
   {
     class = EnemyClass;
     name = "blueDiamondEntity";
   };
   %newBlueDiamondClass.imageFile = "blueDiamondImageMap";
   %newBlueDiamondClass.classSize = 6;
   %newBlueDiamondClass.classSpeed = 15;
   %newBlueDiamondClass.classMinRotationFixVar = 1000;
   %newBlueDiamondClass.classMaxRotationFixVar = 250;

   echo(blueDiamondClass.name);
  return %newBlueDiamondClass;
}

function createEnemy(%name, %class, %superClass)
{
   %newEnemy = new ScriptObject(%name)
   {
      class = %class;
      superClass = %superClass;
      imageMap = %class.imageFile;
      size = %class.classSize;
      speed = %class.classSpeed;
      minRotationFixVar = %class.classMinRotationFixVar;
      maxRotationFixVar = %class.classMaxRotationFixVar;
   };
   return %newEnemy;
}

You'll note three primary things here:

1) When I am creating a "class", I name it as such. This is to differentiate between the naming convention for enemies themselves, and the various enemy classes. Purely a matter of choice.

2) I always return an objectID handle to whatever it is that I am creating. This is possibly redundant for creating classes, since we have an implied assumption that our scripter always knows the names of classes he is going to be creating, but it's consistent with where it's important: when we create enemies of those classes.

3) I've removed some hard coded values from my createEnemy function, and placed them in the creation of the class itself. The primary reason for this is the fact that I can now do this:

function createBlackDiamondClass()
{
   %newBlackDiamondClass = new scriptObject(blackDiamondClass)
   {
     class = EnemyClass;
     name = "blackDiamondEntity";
   };
   %newBlackDiamondClass.imageFile = "blackDiamondImageMap";
   %newBlackDiamondClass.classSize = 12;
   %newBlackDiamondClass.classSpeed = 33;
   %newBlackDiamondClass.classMinRotationFixVar = 200;
   %newBlackDiamondClass.classMaxRotationFixVar = 100;

   echo(blackDiamondClass.name);
  return %newBlackDiamondClass;
}

And the important advantage to this technique: Now I can add in new classes, as many as I like, and use them with exactly the same createEnemy script, unchanged.

Like so:

$myEasyEnemy = createEnemy("EasyEnemy", blueDiamondClass, EnemyClass);
$myHardEnemy = createEnemy("HardEnemy", blackDiamondClass, EnemyClass);


To "USC-IMD student 3": Keep in mind that TorqueScript is "oop-like"...in many ways it will act as c++, but it does not give you the entire suite of oop-ishness that c++ does. In your particular example, you don't actually have to create those variables--T2D Game Builder already has all of the coordinate systems for position and velocity built into the underlying classes such as fxSceneObject2D, and you simply use those via the accessor methods provided.

Now, one of the benefits of T2D is that if you really need a new variable that isn't covered by the underlying classes, you simply use it, and it's created for you for future use as well. In that particular example, you can add on to the variables that we create in the bottom part of the class creation functions with your new variables, and use them like I did in the createEnemy function.
#7
02/16/2006 (8:33 pm)
I have a question.

Why am we creating classes in a function?

e.g.
function createEnemyClass()
{
   %newEnemyClass = new scriptObject(EnemyClass)
   {
      health = 1;
      fixAim = 400;
      speed = 20;
//      player = $player; honestly not sure the purpose of this, but it absolutely implies only ever having single player
      sizeDevider = 4;
   };
   return %newEnemyClass;
}

The only reason I actually did that in my example was because, that's how I had seen it done in the RTS tutorial.

Is there any benefit to that?

Also I had to implicitly call

createEnemyClass();
createBlueDiamondClass();

before I used createEnemy(%name,%class,%superClass);
#8
02/16/2006 (9:54 pm)
Well, to be honest there isn't a reason that you have to do it that way--I simply kept things in line with your original code.

Some people don't like to have "loose" code that isn't happening in a particular function's scope, and/or like to abstract out things into a particular function and then call that function repeatedly. If you wind up changing things later, it can be easier to change it all in one place.

Simply a matter of style however.