Jump to content
Sign in to follow this  
DChristy87

addAction Problem

Recommended Posts

I have an sqf that creates a fireplace and creates a group:

_action = _this select 0;
_caller = _this select 1;
_id = _this select 2;
_caller removeAction _id;
_grp = createGroup WEST;
[_caller] joinSilent _grp;
_grp selectLeader _caller;
_spawnCamp = createVehicle ["FirePlace_burning_F",getPos player, [], 0, "CAN_COLLIDE"];
_spawnCamp setpos  (player modelToWorld [0,1.5,0]);
_spawnCamp setDir ([_spawnCamp, player] call BIS_fnc_dirTo);
_spawnCamp addAction ["Join this camp?", {_this select 1 joinSilent _grp}];

It gives an addAction to the fireplace. I want the addAction to make the caller join the group that was created at the same time as the fireplace. The addAction shows up just fine but when you click it, it reads an error "undefined variable #_grp". Does anyone have any idea what I could be doing wrong?

Share this post


Link to post
Share on other sites
_spawnCamp addAction ["Join this camp?", {[_this select 1] joinSilent _grp}];

Share this post


Link to post
Share on other sites

Well, joinSilent takes an array to the left and a group to the right. So you just needed to turn the _this select 1 into an array but enclosing it with [ ].

Share this post


Link to post
Share on other sites

Yeah, it's taking me a while to learn small things like that. It usually takes me quite a bit of time to figure out when to enclose something in brackets and then even longer because I'm never entirely sure which brackets ()[]{} to use :-\ I'll get there someday.

Share this post


Link to post
Share on other sites

If you can't help, see here, and copy an example and edit it as you need. ;)

Share this post


Link to post
Share on other sites

The code executed on activation of the action is effectively a separate thread than the script that added the action. The code executed stands on its own without a higher scope that called it. Therefore, the error means that you have used a local variable that is undefined in the current scope. You can make use of the feature of addAction that allows you to pass arguments to the code. It is detailed on this wiki page (arguments are after the code):

http://community.bistudio.com/wiki/addAction

Using this, I would write the addAction statement like so:

_spawnCamp addAction ["Join this camp?", {[_this select 1] joinSilent (_this select 3)}, _grp];

The '(_this select 3)' refers to the argument passed into the code of the addAction; this is the value defined in your script as '_grp'. This argument remains a persistent property of the action, storing the value of '_grp' separately from your function that executed the addAction. Changing the value of '_grp' in your function will not affect the code of the addAction.

Share this post


Link to post
Share on other sites

Ahhh, thanks for the explanation! I'll implement this and then try it out with my friend when he has time :) Thanks again!

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  

×