Author Topic: %this variable  (Read 777 times)

What exactly does the %this variable stand for? I know what client and player are, but is %this just a user created variable? Or is it part of torque?

The %this variable is most often used to represent the object in one of it's methods.
example:

Code: [Select]
function Object::doSomething(%this, %arg1)
{
//stuff here
}

It doesn't have to be named %this, but it's quite common as it's easy to remember.

The %this variable is most often used to represent the object in one of it's methods.
example:

Code: [Select]
function Object::doSomething(%this, %arg1)
{
//stuff here
}

It doesn't have to be named %this, but it's quite common as it's easy to remember.

So it can be named anything that isn't already a variable? That makes sense.

So it can be named anything that isn't already a variable? That makes sense.
What?
You can name your variable however you want.
Code: [Select]
function servercmdlol(%doge,%wtf,%cat)
{
//do nothing
}



So it can be named anything that isn't already a variable? That makes sense.

Like Zeblote said, you can name variables whatever you want. When you see a line like this:

Code: [Select]
function addResource(%client,%resource,%amount,%modifier)
Don't think of the parameters within the parenthesis as predetermined names. When this function is called, some values are going to be passed to it. What you put in the parenthesis are the variables you wish these values to be placed into, so that you can access these values from within your code. That is to say, if four values are passed, you can retrieve the first value with %client, the second value with %resource, and so on. As such, you could use code such as this:

Code: [Select]
function addResource(%a,%b,%c,%d)
And the value that was originally going to be accessed with %amount will now be accessed with %c. However, that is usually less appealing to read, so many scripters will name their parameters something more revealing for when they go back to the code in several months or when others take a look at it.

%this is just the most common variable name for the first variable in a method. It's because some other languages use it as a requirement.

that isn't already a variable?

Local variables %whatever, only need to be unique to a function.
e.g.
function example(%var, %hello)
{
}

function anotherFunction(%var, %hello)
{
}
Would be perfectly fine and both functions can operate separately without any variable-based confusion

However globals $whatever, need to be unique to the whole game.