%this (Can Someone Explain)
by Shakey · in Torque Game Builder · 12/16/2009 (2:05 pm) · 9 replies
I think if I understood this, it would help me out a lot better. I have the The beginning guide to Torque Script and I have read the TDN information. But I am not grasping the %this variable.
From what I can tell from the code below if; %this is a instance handle to an AIManager object, but what is the object it references. Also in the code is %this.player, %this.spawn and %this.schedule, what are these instances referencing. Is this the same %this variable passed into the function, used throughout, or are these different values for the variable.
Please help me to understand this. If I can grasp this I think I can better understand what is going on.
From what I can tell from the code below if; %this is a instance handle to an AIManager object, but what is the object it references. Also in the code is %this.player, %this.spawn and %this.schedule, what are these instances referencing. Is this the same %this variable passed into the function, used throughout, or are these different values for the variable.
Please help me to understand this. If I can grasp this I think I can better understand what is going on.
function AIManager::think(%this)
{
if (!isObject(%this.player))
%this.player = %this.spawn();
%this.schedule(500,think);
}
#2
In the method definition if I have a SceneObject declared as %path, within, then everywhere in the method where there is %this.(property), it refers to my %path object.
12/16/2009 (2:34 pm)
Ok so then the following from the documentationfunction t2dSceneObject::move(%this, %amount)
{
%position = %this.getPosition();
%newPosition = t2dVectorAdd(%position, %amount);
%this.setPosition(%newPosition);
return %newPosition;
}Quote:
Methods are very similar to functions. The difference is they are associated with a specific class type. The above example creates a method for t2dSceneObjects. Assuming %object is a t2dSceneObject, this method could be called like this: %object.move(“10 5”);
That would move %object 10 units in the x direction and 5 units in the y direction. Inside the method definition, the %this variable is actually a reference to the object that called the method. So in this case, it is the same as %object. %this is always the first parameter in a method definition (though it doesn't necessarily have to be named %this), and is automatically passed to the method by the engine.
In the method definition if I have a SceneObject declared as %path, within, then everywhere in the method where there is %this.(property), it refers to my %path object.
#3
12/16/2009 (4:02 pm)
Looking at the code above from my previous post, the same method definition, would %this be a reference to a enemy object, any enemy object. Would I need to define a enemy object within the definition for instance %dogEnemy = %this. So then everywhere in the definition where %this is, would be referenced for %dogEnemy.function Enemy::move(%this, %amount)
{
%position = %this.getPosition();
%newPosition = t2dVectorAdd(%position, %amount);
%this.setPosition(%newPosition);
return %newPosition;
}
#4
When "%dog1.move()" is called, "%this" will equal "%dog1". Same with "%dog2.move()": "%this " will equal "%dog2".
12/16/2009 (5:04 pm)
While "%this" can reference any Enemy object, it will only reference the one you are currently calling "move" on.%dog1 = new t2dStaticSprite() { class = "Enemy"; ...other stuff... };
%dog2 = new t2dStaticSprite() { class = "Enemy"; ...other stuff... };
%dog1.move( "100 100" );
%dog2.move( "-200 -200" );
function Enemy::move( %this, %amount ) { ... code here ... }When "%dog1.move()" is called, "%this" will equal "%dog1". Same with "%dog2.move()": "%this " will equal "%dog2".
#5
I create a static sprite in the level builder and give it a name of star
className of StarClass. So in this function
I read this as, the t2dscene gets loaded as well as any references to the StarClass using the %this variable, in this case the star object from the level builder.
Am I correct, more to follow if this is correct, and thanks for the help
12/16/2009 (6:11 pm)
Ok I see, it is making since to me a little bit more now. I think most of my problems are coming from using the level editor and associating it with the script code. Especially with the onlevelloaded(). You have objects created using the level editor, So I see it as creating the objects and getting them initialized through the onlevelLoaded() function call. For instance.I create a static sprite in the level builder and give it a name of star
className of StarClass. So in this function
function StarClass::OnLevelLoaded(%this, %scenegraph)
{
}I read this as, the t2dscene gets loaded as well as any references to the StarClass using the %this variable, in this case the star object from the level builder.
Am I correct, more to follow if this is correct, and thanks for the help
#6
Since you've named your object "Star", you could also use
But if you plan on more objects also being classed as "StarClass", you should leave what you have.
12/16/2009 (6:32 pm)
Yep! That's correct! "%this" will equal "Star" and "%scenegraph" will equal the scene that just loaded.Since you've named your object "Star", you could also use
function Star::onLevelLoaded( %this, %sceneGraph )
{
}But if you plan on more objects also being classed as "StarClass", you should leave what you have.
#7
Especially when doing the shooter tutorial, when creating the player. Why would you do this if, %star is already created in the builder( not the exact same variables, but you know what I mean), And if it already created and referenced from the function, why do it twice, this is where I am getting confused also.
12/16/2009 (7:07 pm)
Ok thanks here is the last question. If I have my class named starClass, what would this represent, as I see it alot function StarClass::onLevelLoaded(%this, %scenegraph)
{
%star = %this;
}Especially when doing the shooter tutorial, when creating the player. Why would you do this if, %star is already created in the builder( not the exact same variables, but you know what I mean), And if it already created and referenced from the function, why do it twice, this is where I am getting confused also.
#8
If you've named your object "Star", you already have a global way to reference it. (Like "Star.move( "100 100" );".)
Sometimes I see "$star = %this", which is setting a global variable to the value of the object. But if you ever create another object with the class "StarClass", that global object will be clobbered.
12/16/2009 (8:45 pm)
You should not do that.If you've named your object "Star", you already have a global way to reference it. (Like "Star.move( "100 100" );".)
Sometimes I see "$star = %this", which is setting a global variable to the value of the object. But if you ever create another object with the class "StarClass", that global object will be clobbered.
#9
function doSomething(){... }
without the context of an object. If you now define
function MyClass::doSomething(%this){...}
you have two different functions. Now lets use this functions within MyClass:
function MyClass::useFunctions(%this){
doSomething(); // Calls function
%this.doSomething() // Calls method of MyClass
}
This is not necessary in languages like Java, where the stand-alone function is not possible at all. It is then clear that you always refer to the MyClass method, and "doSomething()" (omitting %this) refers to "MyClass::doSomething()". In TorqueScript however it is mandatory to have a first argument in the argument list which will keep the handle to the class. In Java et.al the use of "this" is optional, and "this" is implicitely defined.
And you can of course write
function MyClass::doSomething(%myOwnName){...}
and %myOwnName now is the handle to MyClass.
12/17/2009 (4:10 am)
I think another way to explain the use of %this (which can be named differnt btw.) is that it solves ambuguity problems. Unlike "real" object-oriented programs TorqueScript allows the definition of functions outside of objects. So if you can have a functionfunction doSomething(){... }
without the context of an object. If you now define
function MyClass::doSomething(%this){...}
you have two different functions. Now lets use this functions within MyClass:
function MyClass::useFunctions(%this){
doSomething(); // Calls function
%this.doSomething() // Calls method of MyClass
}
This is not necessary in languages like Java, where the stand-alone function is not possible at all. It is then clear that you always refer to the MyClass method, and "doSomething()" (omitting %this) refers to "MyClass::doSomething()". In TorqueScript however it is mandatory to have a first argument in the argument list which will keep the handle to the class. In Java et.al the use of "this" is optional, and "this" is implicitely defined.
And you can of course write
function MyClass::doSomething(%myOwnName){...}
and %myOwnName now is the handle to MyClass.
Torque 3D Owner Marc Dreamora Schaerer
Gayasoft
For example if you have
myAIManager.think();
then in reality that is a AIManager::think(myAIManager); and myAIManager will then become %this within the function