Jump to content
Sign in to follow this  
gameboi

'this' pointer does not work in sqf file.

Recommended Posts

Hello.

I'm new at scripting. Basically if I type:

removeallweapons this;

in the init field of a unit, it works just fine. However, if I put this code in a sqf file, it doesn't work. The only workaround I have is to put this code in the sqf file:

waitUntil {!isNull player};
_unit = _this select 0;
removeallweapons _unit;

Now it works like a charm. I don't understand why the 'this' pointer won't work for me. In the init field of the unit I just have the basic code to execute the script:

_script = player execVM "loadout.sqf"; 

So nothing wrong there.

Can anyone help me out? Cheers!

Share this post


Link to post
Share on other sites

_unit = _this [color="#FF0000"]select [/color]0;

the "select" is refering to a position in an array: [0,1,2,3...etc]

as you are passing a player (unit/object) and not an array as "_this", that will not work.

try either

 _unit = _this; 

or (recommended)

 [player] execVM "loadout.sqf" 

Cheers

Edited by roy86

Share this post


Link to post
Share on other sites
_unit = _this [color="#FF0000"]select [/color]0;

the "select" is refering to a position in an array: [0,1,2,3...etc]

as you are passing a player (unit/object) and not an array as "_this", that will not work.

try either

 _unit = _this; 

or (recommended)

 [player] execVM "loadout.sqf" 

Cheers

_unit = _this select 0; works just fine by the way.

But '_unit = _this' in the script file doesn't work either.

However, the goal is that it should work for AI as well! That's what I'm trying to accomplish.

Besides, it works for other people, I wonder why not for me. Everybody types 'removeallweapons this;' in their script file to refer to the calling object.

Edited by Gameboi

Share this post


Link to post
Share on other sites

No to sound rude but no, they don't since it doesn't work. Are you sure you're not mistaking "this" for "_this"? There's a difference

Share this post


Link to post
Share on other sites
No to sound rude but no, they don't since it doesn't work. Are you sure you're not mistaking "this" for "_this"? There's a difference

You're not rude at all.

I learned from this video (short one):

Share this post


Link to post
Share on other sites

If you're using

 
[this] execVM "loadout.sqf"

In the editor, it will work in loadout.sqf if you have

_unit = _this select 0;
removeAllWeapons _unit;

I can't watch the video but that's probably what he is doing

Share this post


Link to post
Share on other sites
If you're using

 
[this] execVM "loadout.sqf"

In the editor, it will work in loadout.sqf if you have

_unit = _this select 0;
removeAllWeapons _unit;

I can't watch the video but that's probably what he is doing

Yeppers, it works fine that way. I think the youtuber was a bit sloppy with his first few lines of code. He didn't replace 'this' with '_unit' for some reason. Got me confused.

Share this post


Link to post
Share on other sites

'this' refers to 'this object'.

eg. If you have a unit called 'boss' then you can refer to it anywhere as 'boss' but if you are using the init field of the 'boss' unit you can just use 'this' which refers to 'this object'

Within the script, the '_this' refers to the script argument. When you are calling

player execVM "loadout.sqf"

you are actually passing the 'player' variable (referring to the player object) to the script as an argument. This is the same as putting in the player init box:

nul = this execVM "loadout.sqf"

(Remember 'this' refers to 'this object')

Ok - Back to the script - Inside the script as we know now, '_this' refers to the argument passed. In the above instance you have just passed a single object so '_this' refers to the object you passed (ie. 'player')!

In your instance that is probably all you need but you can pass more than one value - eg. for a teleport script if you would want to pass an object (eg. the player) and a position. This is done by passing an array as the argument instead of just one object.

eg.

 [player, (getPos TeleportLocation)] execVM "teleport.sqf"

In this instance, the object passed is actually a reference to a 'list' of objects. We are still only passing one argument (one array) but it contains multiple things.

Back inside the script again - '_this' still refers to the argument but instead of it referring to just a single object, it is referring to an array now, so what we have practically is

_this = [player, (getPos TeleportLocation)]

If we want to access the first object, we use the array handling syntax (great guide here: http://forums.bistudio.com/showthread.php?100559-Beginners-guide-Arrays ) to select it.

so in this instance if we want the player, we use _this select 0 (as player is the first element)

So our teleport script may look like:

_unitToTeleport = _this select 0; //First element of the array passed to the script
_location = _this select 1; //Second element of the array passed to the script

_unitToTeleport setPos _location;

Hope it puts it into perspective a bit!

Share this post


Link to post
Share on other sites
'this' refers to 'this object'.

eg. If you have a unit called 'boss' then you can refer to it anywhere as 'boss' but if you are using the init field of the 'boss' unit you can just use 'this' which refers to 'this object'

Within the script, the '_this' refers to the script argument. When you are calling

player execVM "loadout.sqf"

you are actually passing the 'player' variable (referring to the player object) to the script as an argument. This is the same as putting in the player init box:

nul = this execVM "loadout.sqf"

(Remember 'this' refers to 'this object')

Ok - Back to the script - Inside the script as we know now, '_this' refers to the argument passed. In the above instance you have just passed a single object so '_this' refers to the object you passed (ie. 'player')!

In your instance that is probably all you need but you can pass more than one value - eg. for a teleport script if you would want to pass an object (eg. the player) and a position. This is done by passing an array as the argument instead of just one object.

eg.

 [player, (getPos TeleportLocation)] execVM "teleport.sqf"

In this instance, the object passed is actually a reference to a 'list' of objects. We are still only passing one argument (one array) but it contains multiple things.

Back inside the script again - '_this' still refers to the argument but instead of it referring to just a single object, it is referring to an array now, so what we have practically is

_this = [player, (getPos TeleportLocation)]

If we want to access the first object, we use the array handling syntax (great guide here: http://forums.bistudio.com/showthread.php?100559-Beginners-guide-Arrays ) to select it.

so in this instance if we want the player, we use _this select 0 (as player is the first element)

So our teleport script may look like:

_unitToTeleport = _this select 0; //First element of the array passed to the script
_location = _this select 1; //Second element of the array passed to the script

_unitToTeleport setPos _location;

Hope it puts it into perspective a bit!

Good post, I deduced this today. I just was confused by that youtube video. I thought more options were possible but I think the uploader was just sloppy. Thanks a bunch!

Share this post


Link to post
Share on other sites

Hi guys, I was searching the forum looking for the solution to this very problem. Thank you both!

Rob

Share this post


Link to post
Share on other sites

Please sign in to comment

You will be able to leave a comment after signing in



Sign In Now
Sign in to follow this  

×