Game Development Community

Declaring class in the function

by Matt Clayton · in Torque Game Builder · 06/09/2010 (4:11 am) · 4 replies

Hi,

I am trying to understand more about function in Torquescript.

Question - when do I need to add the class to the function declaration - ie:

function objectClass::functionName()
{}

And when is it ok to leave the class out - ie:

function functionName()
{}

Currently I try both in my script files and look for "cannot find function functionName" in the console.

Any help will be much appreciated.

Matt.

#1
06/09/2010 (5:02 am)
(ignoring behaviors)
You can leave out the class reference for functions that will be used by multiple classes. Or another way to think of it: A function belongs within a class when its a method specific to that class.

Example:
function Alien::ceaseFire(%this)
{
   if (%this.missiles.getCount()>0){
      while(%this.missiles.getCount()){
         %fire=%this.missiles.getObject(0); 
         %fire.Delete(); 
      }
   }
}

Implementaion: %alienObject.ceaseFire();

I want my aliens to cease fire, so this belongs in my alien class. I could have done this outside of the class and then passed in the alien object:
function ceaseFire(%obj)
{
   if (%obj.missiles.getCount()>0){
      while(%obj.missiles.getCount()){
         %fire=%obj.missiles.getObject(0); 
         %fire.Delete(); 
      }
   }
}

Implementation: ceaseFire(%alienObject);

I prefer to use classes (i.e. namespaces) because the code is cleaner and easier to manage in the long run. Also, its nice to have a separate cs file for each class.
#2
06/09/2010 (7:17 am)
Kevin - thanks for your help - for clarification:

Does it matter where non-class specific functions are stored? Will the engine find them wherever I put them?

Sorry if this is basic stuff, but namespaces seem to work a bit differently in Torquescript.

Matt.
#3
06/09/2010 (7:25 am)
It does not matter where you store non-class specific functions. As long as you exec the file the engine will pick it up.
#4
06/09/2010 (7:27 am)
They should work anywhere. Personally, I keep all of my non-class functions in their own cs file.