Game Development Community

Newbie problem with calling a function

by Matthew W · in Torque Game Engine · 04/09/2008 (12:09 am) · 2 replies

Hi

There is a file named commander.cs and in this file there is a function defined like:
function Commander::onLeftMouseDown(%this) {
//...
}
and i want to call this function from inside another torque script file named default.bind.cs by trying:
function button0Clicked(%val) {
  
    Commander.onLeftMouseDown();
}

And i get this error,
Unable to find object: 'Commander' attempting to call function 'onLeftMouseDown'



Could you tell me what to do to make this work?

#1
04/09/2008 (7:11 am)
So, what is Commander?

Is this an ActionMap? A PlayerObject? A GUI?

The syntax function Object::methodName(%vars) results in creation of a member function for the "Object" class or script object.

Edit: Fixed bad formatting...To the person who brought it to my attention: "Shut it Mr. irnogsights"
#2
04/09/2008 (10:04 am)
The '.' operator requires the left param to be an object.
Commander.onLeftMouseDown() makes no sense if 'Commander' is not an object.

By defining this function:
function Commander::onLeftMouseDown() {}

You have not created a commander "Object", you have only created a function in the Commander namespace. To call that function ( on an object ) with the '.' operator, you must create an object that belongs to the Commander namespace ( eg. it is named Commander, its class is Commander, or its super-class is Commander ).

If you DID want to call it like a global function and not on a particular object, call it like:
Commander::onLeftMouseDown();