Game Development Community

What is the " %this"?

by Eddie Liu · in Torque Game Engine · 08/18/2006 (8:52 am) · 6 replies

In example\starter.fps\server\scripts\aiPlayer.cs ,there have follow function:

function AIManager::think(%this)
{
   if (!isObject(%this.player))
      %this.player = %this.spawn();
   %this.schedule(500,think);


Can somebody tell me what is the " %this", and why can it be "%this.player"

I searched all over the GG,and found this:
"the first parameter to a method is always the object whose method is being called. "
But "%this" still confuse me, and somebody help me?

#1
08/18/2006 (8:54 am)
In your example, you are calling the 'think' method of an AIManager object you created, I.E. the NPC/Bot/AI player you added to the game. %this refers to that specific instance of the AIManager object that you created.
#2
08/18/2006 (9:11 am)
%this is the instance of the AIManager class, which is always the first parameter when you define a function for that class, such as AIManager::think(%this)

Because your AIManager class manages an AI bot called player, and player is a member variable of the class, you refer to it as %this.player.

I'd strongly suggest reading the TorqueScript overview on TDN as a starting point.
#3
08/18/2006 (9:12 am)
Jonathon Stevens,thank you very much.
But in the same file "aiPlayer.cs "
function DemoPlayer::onReachDestination(%this,%obj)
{
   if (%obj.path !$= "") {
      if (%obj.currentNode == %obj.targetNode)
         %this.onEndOfPath(%obj,%obj.path);
      else
         %obj.moveToNextNode();
   }
what is the "%obj"? and what is the "%this"?
can I change "%this.onEndOfPath" to "%obj.onEndOfPath" ?
sorry for my so many questions.
#4
08/18/2006 (9:15 am)
Thank you Sebastian Potter, I will check it later.
#5
08/18/2006 (10:26 am)
%this is again whatever object you've created of type DemoPlayer. %obj is just a parameter being passed into the onReachDestination funciton
#6
08/19/2006 (5:34 pm)
function DemoPlayer::onReachDestination(%this,%obj)
{
   if (%obj.path !$= "") {
      if (%obj.currentNode == %obj.targetNode)
         %this.onEndOfPath(%obj,%obj.path);
      else
         %obj.moveToNextNode();
   }

In this case DemoPlayer is a Datablock which is the base definition of the player object. The onReachDestination callback occurs when a player object reaches a marker in the current path. In this particular instance %this is the DemoPlayer datablock, and %obj is the player object itself.

Check out the TDN article on Datablocks, and how and why they are used for more information.