Forum Jump :

Author Message


Mc Script

Posts: 1034

Level: Member

Country: ch
Location: St. Gallen
Occupation: Armaholic addon tester
Age: 48
In-game name: Myke

#30139 Posted at 2008-08-06 16:07        
What's "this"?
PUN intended

While editing and scripting, almost everywhere you'll encounter the word "this". Sometimes with, sometimes without previous underline.
And mostly you overlook it since you don't know what it is for but anything works as you like, so why care about.

Here a brief overlook what's "this" for, where you may ancounter it and what does it there.

Let's begin with the most obvious: "this" in scriptcalls.

Many times you may encounter scripts (no matter if .sqf or .sqs style) that are called like this:
this exec "script.sqs"
[this] exec "another_script.sqs"
You may have noted, these scriptcalls are always in objects (unit, vehicle, building, just anything) initialization lines. Never you'll find such a scriptcall inside another script.

Here, "this" just means: "this object/man/vehicle, not the other 2 meters on the left".
So if you have a unit WEST/AT Soldier/Myke, then this is this unit. No one else.
So anything that the script does, as long it refers to the unit passed, will be done to/with this unit.

This leads us to the second place where you may encounter "this": inside scripts.

Each script can get one or multiple variables through the exec/execVM command:
variable exec "script.sqs"
Ok, we've passed the variable to the script. But inside the script we have to read the variable:
_myvariable = _this;
Here it is: "_this"
Don't be confused about the underlines, they mean only that the value of the variable isn't shared with other scripts.
In this example we pass a single variable to the script but we could also pass multiple variables by using an array (this is mostly the case):
[variable1, variable2, variable3] exec "script.sqs"
Now if we use the line above
_myvariable = _this;
then "_myvariable would contain the whole array, not a single variable. So we use the "select" command:
_myvariable1 = _this select 0;
_myvariable2 = _this select 1;
_myvariable3 = _this select 2;
Now we've splitted the array up into independent variable. All this by using "this".

Now there's another place where you encounter "this" but maybe you didn't noticed it yet: in triggers.

Whenever you create a new trigger, in the condition line already stands "this". What does it there?
Well, for sure you already used triggers by setting size, shape, position, activated by and all those drop down settings. All these settings are "this".
As long "this" is in the condition line, all the above settings are used as condition to fire the trigger.
You can extend the conditions by extending "this" with other conditions. Practical example:
In a mission you would have destroyed vehicle "Shilka" and players must reach extraction point. Well, extraction point is easy: trigger with settings "players side/present". But this trigger will also fire if the player runs directly to the extraction point without killing the Shilka.
Now we extend the conditions:
this AND (! alive Shilka)
So now our end trigger will only fire if players side is present in trigger area AND the shilka is destroyed. If you remove "this AND", then all above settings are irrelevant and the trigger will fire as soon the Shilka is destroyed, no matter of all settings.

Well, this was a short introduction of "this". Hope you can use "this".