Jump to content
Sign in to follow this  
Balistek-Wrench

AddAction not passing "This"

Recommended Posts

Hey guys, I've kinda hit a wall Google can't help me climb.

I'm trying to use an AddAction to run an SQF file, but I'm having a weird problem.

The AddAction is on a chair, and is as follows:

this addAction [ 
 "sit down", 
 "execvm 'sit.sqf'", 
 [], 
 1, 
 false, 
 true, 
 "", 
 "true", 
 -1, 
 false 
];

and the sit.sqf is

if (!isServer) exitWith {};
_this select 0;
_soldier = _this select 1;
[_soldier,"SIT1","ASIS"] call BIS_fnc_ambientAnim;

When I try the action, I get Arma tells me '_this' is not defined.

My understanding is that the AddAction command defines This select 0 as the object, and this select 1 as the caller, but Arma isn't recognising the "this" in the first place.

For the record, I've also tried dropping the !isserver stuff and the _this select 0 line. I have them in there because I've seen them in other scripts, am currently use both is a script I wrote that does work.

 

I hate to bother you for something so trivial, but I can't find a solution because the problem doesn't seem to be in my script...

Share this post


Link to post
Share on other sites

this addAction [ "sit down", {[_this select 0] execVM "sit.sqf"}, [], 1, false, true, "", "true", -1, false ];

Share this post


Link to post
Share on other sites

Drop the script altogether;

this addAction [ "sit down", {[_this select 1,"SIT1","ASIS"] call BIS_fnc_ambientAnim;}, [], 1, false, true, "", "true", -1, false ]; 

 

  • Like 1

Share this post


Link to post
Share on other sites

That was fast! Thanks much.

Do I then have to pass _this select 1,2, and 3 using the method above? (if needed in future)

Something like this;

{[_this select 0,_this select 1,_this select 2, <...>] execVM "sit.sqf"}

It seemed to me on the wiki page that it always passed those. But no matter, if it works, it works.

Thanks, lsd, but I wanted to understand why this was happening so that I could avoid further issues with other scrips in the future.

WRENCH.

Share this post


Link to post
Share on other sites
4 hours ago, Balistek-Wrench said:

That was fast! Thanks much.

Do I then have to pass _this select 1,2, and 3 using the method above? (if needed in future)

Something like this;


{[_this select 0,_this select 1,_this select 2, <...>] execVM "sit.sqf"}

It seemed to me on the wiki page that it always passed those. But no matter, if it works, it works.

Thanks, lsd, but I wanted to understand why this was happening so that I could avoid further issues with other scrips in the future.

WRENCH.

 

AddAction passes an array inside its code field.
 

_this select 0// would be the object that the action has been added to.

_this select 1// would be the player(in most cases) that activated the action.

_this select 2// is the action ID, can be used to remove the action if needed.

 

These lines could be accessed like this:

params ["_object","_caller","_actionID"];

Neatly all in one line.

To test it put this action on a car or other object and activate it:

 

this addAction ["Test",{

	params ["_object","_caller","_actionID"];
	
	systemchat format ["%1 is holding the action.",typeof _object];
	systemchat format ["%1 has activated the action.",name _caller];
	systemchat format ["The action ID is %1.",_actionID];

}];

Now if you want to pass these three parameters into your script file or other function this will do just fine:

 

this addAction ["Test",{
_spawnSomeFunction = _this execVM "yourscript.sqf";
}];


//inside yourscript.sqf:
params ["_object","_caller","_actionID"];

This way the entire array from the addAction is being passed inside your function and can be accessed from within there.

It's always worth it goofing around with stuff and trying silly things to figure out how it's working.

 

_this is a magic variable, read more about those here.

 

Cheers

  • Like 3

Share this post


Link to post
Share on other sites

AHA!

so that's what lines like 

params ["_object","_caller","_actionID"];

do! I've seen those in a lot of scripts and was only vaguely aware of what they're for.

I read (and re-read) the wiki page for addaction, and it never really explained that portion of it. I'm sure it's somewhere else, but I never saw it.

 

So just for fun, let me see if I understand this from the more "computer science" side of the house.

By adding that line, the addaction command effectively replaces the variables in that array with the pertinent information. 

Then, when you use something like 

_var = _this select 0;

It's actually selecting from the array in the "params" line, not passed directly from the addaction script.

 

Meaning technically what I did in my previous post is kind of a bodge, asking it to send that data separately as an argument.

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  

×