Game Development Community

Why isn't %this always used?

by Sean West · in Torque Game Engine · 08/13/2005 (3:00 pm) · 6 replies

Here are two functions (taken from 3d game prog all in one):

function ItemData::OnPickup(%this,%obj,%user,%amount)
{
....
return true;
}

function ItemData::Create(%data)
{
%obj = new Item() {
dataBlock = %data;
static = true;
rotate = true;
};
return %obj;
}

Now I thought that %this was always the first parameter, even if you changed the name. In other words I thought that the parameter in the second function, %data, is really the same thing as %this in the function above. Am I wrong?

#1
08/13/2005 (3:25 pm)
Hey Sean!

no you're not wrong, the %this variable in the first function is the same thing as the %data parameter in the second function. the first parameter in a namespace function is always a handle to the object calling it, regardless of what you name it. these are both datablock functions, so the first parameter will be the handle of the datablock itself.
#2
08/13/2005 (3:46 pm)
Like Sean H. said,

%this isn't implied like in normal C++. As it is implemented in TorqueScript right now, %this is just the first argument given in any of the namespace functions.

Once you get used to this rather odd behavior it actually is fairly flexible.
Here are a couple of different ways to call a function belonging to a particular
namespace.
%test = new ScriptObject() {
   class = ATest;
};

%test.helloWorld();
ATest::helloWorld(%test);

function ATest::helloWorld(%this)
{
   echo("Hello World");
}

Both accomplish the same thing. As you can see %this is just the object.
I actually prefere the first method over the second, but they both have
their uses depending on what it is you are trying to do.
#3
08/13/2005 (3:54 pm)
Thanks for the quick help guys! Ok, but one last clarification... the two following methods are equivalent right?

1)

(%data is a previously defined handle to a datablock)

%myitem = new Item() {
datablock=%data;
static=true;
rotate= true;
}

2)

(%data is a previously defined handle to a datablock)
(2nd function in original post is defined)

%myitem = %data.create();
#4
08/13/2005 (5:05 pm)
@Sean W:

I would suggest you install the 'enhanced debugger' resource from Tom Spilman and grab either Torsion or TorqueDev IDE and take a look at the variables during runtime. It's a great way to really get your brain wrapped around what is going on.

B--

EDIT: Some resources here might help you out as well. torque.smdlabs.com
#5
08/13/2005 (6:09 pm)
"Thanks for the quick help guys! Ok, but one last clarification... the two following methods are equivalent right?

1)

(%data is a previously defined handle to a datablock)

%myitem = new Item() {
datablock=%data;
static=true;
rotate= true;
}

2)

(%data is a previously defined handle to a datablock)
(2nd function in original post is defined)

%myitem = %data.create();"

the first method is the way you should do it. the way you're trying to do it in the second situation is problematic for a number of reasons. the first is that you cannot previously define a datablock handle. the only way to obtain a datablock handle is through an object which is currently using that datablock. theres no global array or list of datablock handles for manipulation. when you declare a datablock, the definition doesnt return a handle. theres only two instances where you can access a datablock handle; from an object which is currently using the datablock by calling the getdatablock() function, or within a datablock function using the first parameter (%this) as explained earlier. if the function is a datablock function which resides in another datablock function, you can obtain the datablock handle using the first function parameter, but how would you obtain a handle to actually call it?

thinking about it further, it may be possible to do it this way, by obtaining a datablock handle thru conventional means, setting it to a global variable, and then calling the function from the global variable, but thats not the way its supposed to be done. in effect, what you're trying to do is create an object from a datablock. the normal way is to create an object and then assign an associated datablock.
#6
08/13/2005 (6:11 pm)
And oh yeah, doing it using the first method, you should pass the name of the datablock for "datablock=" you should be passing the name of the datablock.