Jump to content
Sign in to follow this  
_qor

addAction with multiple choices economically

Recommended Posts

Hey,

By usind addAction, I want to have multiple choices when using an object.

So I would just add this code one time for every menu item.

this addAction ["Recruit X","scriptX.sqf"];

But assuming there should be 20 menu items, I dont want to have a script for each.

Is there some way to check which menu item (Recruit X) has been chosen and then execute the respective code (while having them all in one file)?

Share this post


Link to post
Share on other sites

Pass the menu item in the addaction arguments to the script which gets called. Then do a switch...do for the passed argument.

something like this:

this addAction ["Recruit Dopey","scriptX.sqf","Dopey"];

or

this addAction ["Recruit Sleepy","scriptX.sqf","Sleepy"];

scriptX.sqf:

_recruit = _this select 3;

switch (_recruit) do
{
    case "Dopey": {do whatever here};
    case "Sleepy": {do whatever here};
    case "Happy": {do whatever here};
};

Is that what you mean?

Share this post


Link to post
Share on other sites

You could also just write a function that takes the caller as argument, defined in CfgFunctions.

this addAction ["Reecruit X","[_this select 1] call TAG_fnc_recruitUnit"];

TAG_fnc_recruitUnit

_caller = [_this,0,objNull,[objNull]] call BIS_fnc_param;

switch (_caller) do {
  case x: {

  };
  case y: {

  };
  case z: {

  };
};

Practically, it's the same way as panther42 described, however you don't need to define a custom variable throughout every action, but instead can just leave the addAction as is.

Share this post


Link to post
Share on other sites

Is that BIS_fnc_param in A2??

On a side note...the TAG in the function name is supposed to be your TAG(like p42 for me), not just the letters TAG. There was an OFPEC TAG database, but since OFPEC has been offline for sometime, no one knows which ones were taken. It was a good initiative.

I only say this because I've seen many scripts/functions with the letters TAG left in it...

Edited by panther42

Share this post


Link to post
Share on other sites

I don't recall the parameter function in Arma2. I think he used TAG because he was posting an example. Though I've actually forgot and used TAG in scripts myself... In any case, I'd use a function as tryteyker suggested since the code will most likely be ran at any given moment, multiple times.

Share this post


Link to post
Share on other sites

This is absolutely what I have been looking for! Didnt know that code.

Big thanks to you panther ;)

Share this post


Link to post
Share on other sites
I don't recall the parameter function in Arma2. I think he used TAG because he was posting an example. In any case, I'd use a function as tryteyker suggested since the code will most likely be ran at any given moment, multiple times.

I figured he did, but wanted those who were newer to scripting and reading this to be aware of the "TAG's" (I've seen several dialog's with your ICE tag in them...good way to keep track of your work)

No problem _qoR. That's what we're here for.

Share this post


Link to post
Share on other sites

Shit, sorry. Haven't really been around A2 for a long time, didn't really know fnc_param didn't exist in A2. Glad you fixed it though.

Share this post


Link to post
Share on other sites
I figured he did, but wanted those who were newer to scripting and reading this to be aware of the "TAG's" (I've seen several dialog's with your ICE tag in them...good way to keep track of your work)

Yeah. Better than using TAG, like I did in writing alot of my previous functions and scripts. Simple Reinforcements in example. For me, TAG stuck out like a sore thumb, which is great, so I figured it was okay. In any case, using a unique prefix turns out to be safer :).

Share this post


Link to post
Share on other sites

Question according to that switch function.

May it be that you cant change variables within the script?

_recruit = _this select 3;

switch (_recruit) do
{

    case "Mission1": 

{

script = [] execVM "mission1.sqf";

missionOn = true;

};

    case "Mission2": 

{

script = [] execVM "mission2.sqf";

missionOn = true;

};};

After activating one of those cases, missionOn will stay false as initialized in the init.sqf.

Of course setting it to true by other scripts work.

Edited by _qoR

Share this post


Link to post
Share on other sites

post using the advanced with code tags. Whatever is in the code{} will run if the switch returns "true"

Show us your addaction or however you are calling this script...

Are you using -showScriptErrors? or was the above just format error from the forum?

Edited by panther42

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  

×