Game Development Community

Is there a way to determine the type/class of a %this variable

by Juan Aramburu · in Torque Game Engine · 05/11/2006 (2:46 am) · 17 replies

Is there a way to determine the type/class of a %this variable or any other object?

%this is a horrible variable name to use for multiple functions when they mean different things.

#1
05/11/2006 (3:03 am)
GameConnection::Bla(%this)

%this = a GameConnection, I usually type it as %client though. Ie, it is a ID for one of the clients.

%this is just a common name when you're calling the object which uses the function.
It can really be anything, a player, a client, a GUI element.

It's just a "lazy" name. Doh, wish I could explain better.
#2
05/11/2006 (3:35 am)
I think this might help you
#3
05/11/2006 (4:10 am)
It's not a "lazy" name, it's a mirror of the "this" pointer that is standard in C++. It's simply a syntax usage so that you have a common reference--%this (as long as it is the first parameter in a function) is always the object handle of the object calling that namespace function.
#4
05/11/2006 (4:35 am)
--
%this (as long as it is the first parameter in a function) is always the object handle of the object calling that namespace function.
--

OK. The thing is that it's a parameter is what's confusing.

Still, is there a way to determine the object type/class/datablock for a certain variable? It would help in determing what parameters to certain functions are.

Martin Hansen, I obviously know about 'that'. Try searching for '%this' or even 'this' and tell me what you get...
#5
05/11/2006 (4:58 am)
DUH, the link got mingled, and now can't find the relevant post.

What I ment to link to was a description on how to use getClassName() and className to differentiate diffenrent object types, and not the Getting Started forum ;)
#6
05/11/2006 (5:13 am)
Just tried a %this.getClassName() and it returned ItemData, so looks like that works, thanks.

Wasn't sure if you were trying to be sarcastic in response to a probably simple question...
#7
05/11/2006 (5:17 am)
Sorry, my mistake.

Note to self: Try links in another window before posting!!!
#8
05/11/2006 (6:00 am)
Quote:
It's not a "lazy" name, it's a mirror of the "this" pointer that is standard in C++. It's simply a syntax usage so that you have a common reference--%this (as long as it is the first parameter in a function) is always the object handle of the object calling that namespace function.

But it's not used everywhere in the TGE scripts, which is why I don't use it myself.
It might make sense in C++, but TS is not C++ and the fact that you can name it anything (like %client, in my example) just makes it confusing.

For instance,
Quote:
function GameConnection::createPlayer(%this, %spawnPoint)

Could read
Quote:
function GameConnection::createPlayer(%client, %spawnPoint)

Without any difference in functionallity.
I see how it can work in C++, but not in TS.
#9
05/16/2006 (2:12 pm)
ItemData::onPickup(%this,%obj,%user,%amount)

%this and %obj seem to refer to the same thing. Is there a difference between them?
#10
05/16/2006 (2:24 pm)
%this is probably referring to the object accessing the function and the %obj is probably referring to the object being picked up
#11
05/16/2006 (2:35 pm)
I was under the impression from posts above that %this referred to the object (an ammobox, for example). %user refers to the player (as it is picking up the ammobox). But %obj seems to be equal to %this; am I correct?
#12
05/16/2006 (3:00 pm)
A note about %this and Torsion. When it sees %this it assumes that the type matches the function namespace:

function GameConnection::createPlayer(%this, %spawnPoint)
{
   %this. // in the dot completion list i assume %this is a GameConnection class.
}

Currently if you use %client, %obj, or any of the others i've seen in the demo scripts i do not do this check and you will not get dot completion.

So use %this ... although it's just a variable name it does provide meaningful information to the reader and to the editor.
#13
05/16/2006 (3:23 pm)
The variable %this refers to a SimObject, but note that datablocks are also SimObjects. So, in some functions "%this" will actually refer to a datablock, not an honest-to-goodness object.

for instance, in
function ItemData::onPickup(%this,%obj,%user,%amount)
%this refers to the DATABLOCK of the object being picked up (HealthKit, Crossbow, etc.) and %obj refers to the actual item in-game. You can tell this because the namespace is ItemData, which is a datablock.

In contrast for the following code
function Player::kill(%this, %damageType)
the variable "%this" is actually a player object. A datablock method would be
// only an example, not actual working code!
function PlayerData::kill(%this, %obj, %damageType)
in this case the method is bound to a datablock so %this would be the player datablock and %obj would be the actual player object (assuming this was an actual Torque function, WHICH IT IS NOT).

So Juan, in your case %this <> %obj. %this is the datablock of the item that you are picking up (HealthKit or perhaps HealthPatch), and %obj is referring to the actual in-game object.
#14
05/18/2006 (2:08 am)
So the way I understand it, if you have a function with a namespace (function namespace::function()), the first argument always refers to the namespace & can be called anything you want?

Gary,
Why would you even care about the datablock if you have the %obj? Can't you just do %obj.dataBlock to obtain the datablock? And if an object was created with a datablock, doesn't the functions get carried down to the object?

So if there was:

- datablock ItemData(MyCustomItemDataBlock)

- function MyCustomItemDataBlock::myDBFunction()

- new Item(MyCustomItemObject) {dataBlock = MyCustomItemDataBlock}

Couldn't you just do MyCustomItemObject.myDBFunction()?

I don't get why you would need the datablock reference if you already have the object. The datablock can't do anything on it's own without an object.
#15
05/18/2006 (6:53 am)
Some Info
Quote:
@note Unlike in C++, the "this" variable is not implicit in a script's method declaration. Instead, the first parameter to a method is always the object whose method is being called. It is traditional but not required to call this parameter %this; a common variant is %db, for methods of datablocks.
#16
05/18/2006 (9:07 am)
Quote:
So the way I understand it, if you have a function with a namespace (function namespace::function()), the first argument always refers to the namespace & can be called anything you want?

Gary,
Why would you even care about the datablock if you have the %obj? Can't you just do %obj.dataBlock to obtain the datablock? And if an object was created with a datablock, doesn't the functions get carried down to the object?

So if there was:

- datablock ItemData(MyCustomItemDataBlock)

- function MyCustomItemDataBlock::myDBFunction()

- new Item(MyCustomItemObject) {dataBlock = MyCustomItemDataBlock}

Couldn't you just do MyCustomItemObject.myDBFunction()?

No you can't do that. Two things have gone wrong with that example.

1. You're trying to call MyCustomItemObject.myDBFunction() but you don't have a function called myDBFunction in the namespace for MyCustomItemObject.

2. Lets say you did MyCustomItemObject::myFunction() instead. Also lets assume your new object is stored in %myObject. It still won't work. When you try to make the call MyCustomItemObject.myDBFunction() this is just syntactic sugar. What TS actually translates this to is the call MyCustomItemObject::myFunction(%myObject). Then you can see your function is not going to get called because the signature of your method (no arguments) does not match the signature of the method you're trying to call (one argument).

Scoping methods to the namespaces of objects and datablocks is just a layer of syntactic sugar designed to give the appearance of object orientedness. Since the engine is actually running the script with a function call that adds the calling "object" as the first argument, you have to have that argument available. Using %this as the arg signature is just a convention to further the illusion of OO in a non-OO environment.
#17
05/22/2006 (12:53 am)
So if
[namespace::][function]([correct number of arguments])

doesn't exist the function will never get called?