Game Development Community

What is %this?

by NoobGank · in Torque Game Builder · 06/23/2006 (8:54 am) · 4 replies

I'm used to scripting languages. So I'm used this seeing 'this'. Bascially its used to pass a reference to the calling object to the called function (method). In the case of GameBuilder in the onLevelLoaded, what is the %this that is passed in? Its a reference to what? Did I miss seeing documentation on this? It seems any object with a onLevelLoaded function is called when the level is loaded, but where is this documented - it seems implicit.

Thanks

#1
06/23/2006 (9:11 am)
For class methods, %this refers to the object that invoked the method. When defining class methods, the first parameter (usually named %this) must be explicitly defined, but its implicit when calling the method on an object. The first parameter does not have to be named %this, you could call it %bob or %myobject, but it always refers to the object that invoked the method.

defining the method for a class:
MyClass::MyMethod(%this, %x, %someOtherParameter)
{
    //method code goes here
}

calling the method on an object
%myObject.MyMethod(%pX, %pSomeOtherParameter);
#2
06/23/2006 (9:41 am)
Check out the Documenation Overview ... the first two tutorials Fish Demo and Fish Game describe the command in detail :)
#3
06/23/2006 (10:01 am)
MMm I started in the wrong place with Basic Tutorial. I'll line up Fish next. Thanks guys. Good answer Ian.
#4
06/23/2006 (12:25 pm)
Also, %anything means it's a local variable, while $whatever means it's a global variable. And you can also have functions that aren't passed references to objects:

function printHello()
{
    echo("Hello world!");
}

called with a simple:

printHello();