Jump to content
  • 0

Addaction in a foreach loop with _x as a different variable for each addaction.


RimBlock

Question

Hi,

 

I am currently writing a script which creates addaction items for the player based on nearby vehicles.

 

I get the _vehiclesnear array, step through it in a foreach loop, filter out 'vehicles' I am not interested in and then do an add action for each of the remaining vehicles.

 

The text for the Addaction includes the vehicle type and works fine for each addaction.  The problem is the passed variable.  I wish to add an action with the object _x as a parameter for each value of _x at the time of the add action. 

 

What I am getting is the last value of _x after the foreach loop has completed is being passed when any of the actions added are selected.

 

i.e. (code is not syntax correct as it is from my memory but you should get the general idea)

 

_vehiclesnear = ["Soldier","SUV","heli", "Player"];

 

_playerlist = ["Soldier","Player"];

 

{

if  !(_x in _playerlist) then { 

_vehicleType = typeof (_vehicleType);

_actionText = format["Vechicle is : %1", _x];

actionHandle = player addaction _actiontext, [_x], "script.sqf";

};

 

}foreach vehiclesnear;

 

The output action  would be something like

 

"Vechicle is : SUV"

"Vechicle is : heli"

 

When any option is selected, "Player" will be sent to script.sqf as it was the last value _x contained when the foreach loop finished.

 

Any suggestions of how I can get the addaction to send the correct value to the script (i.e. the value of _x when the addaction was run) ?.

 

Thanks.

 

 

Link to comment
Share on other sites

5 answers to this question

Recommended Posts

  • 0

look at how its done on the older version of remove vehicle parts and you should get a good idea as how to do this ...

 

Do you have a link to the script in the GitHub ?. 

 

If you are talking about the actions/salvage_vehicle.sqf then it seems a variable is being set every loop and that variable is being passed via the addaction .

 

I presumed (possibly incorrectly) that if _x is passing its last value then any other variable set in the loop would also do the same.

 

Thanks

Link to comment
Share on other sites

  • 0

Ok, sorted.

 

The variables are getting passed in _this select 3.

 

_this select 0 = cursor target.

_this select 1 = Player

_this select 2 = ?? (My script was receiving the number 7).

_this select 3 = both parameters passed in the addaction so (_this select 3) select 0; gets the first one and (_this select 3) select 1; gets the second.

Link to comment
Share on other sites

  • 0

Ok, sorted.

 

The variables are getting passed in _this select 3.

 

_this select 0 = cursor target.

_this select 1 = Player

_this select 2 = ?? (My script was receiving the number 7).

_this select 3 = both parameters passed in the addaction so (_this select 3) select 0; gets the first one and (_this select 3) select 1; gets the second.

 

this is how it is in your exact case and is not always the case:

_this select 0 //target / whatever the addaction is attached to
_this select 1 //caller
_this select 2 //action id (i persume this is the id the addaction is removed with)
_this select 3 // is all the variables you parsed via the addaction

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

Link to comment
Share on other sites

  • 0

this is how it is in your exact case and is not always the case:

_this select 0 //target / whatever the addaction is attached to
_this select 1 //caller
_this select 2 //action id (i persume this is the id the addaction is removed with)
_this select 3 // is all the variables you parsed via the addaction

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

 

I have no idea how I missed this on the Wiki site.  I was even reading up on the priority section just below.

 

At least my guess on what was being passed is verified in the end :) .

 

Thanks

Link to comment
Share on other sites

Create an account or sign in to comment

You need to be a member in order to leave a comment

Create an account

Sign up for a new account in our community. It's easy!

Register a new account

Sign in

Already have an account? Sign in here.

Sign In Now
  • Discord

×
×
  • Create New...