Jump to content
johnnyboy

How to add action for selected units

Recommended Posts

I want player to select one or more units, then perform an action from the action menu that references the selected units.  I think I use this to get the array of selected units:

_unitArray = groupSelectedUnits player;

I have that code in side my AddAction code to be executed.  However, my problem is when I select units, the unit's vanilla action list appears and I either have to select a vanilla action or exit.  When I exit, the units are no longer selected.  I have my custom Action defined in the Player action menu, but now I don't know how to communicate selected units to the custom action.

 

What do I do fellers?

  • Like 1

Share this post


Link to post
Share on other sites

Correct me if i'm wrong but you won't be able to execute action on multiple units.

 

Problem:

  1. addAction to player <- select a group unit, your action disappears replaced by that unit's actions.
  2. addAction to units <- select one unit, you have the action for it; select multiple units, the action disappears.

My suggestion:

Use teams and two multipart actions.

Point to a building, choose "Assault Building", available teams show up for 10 seconds and times out back to assault option if none is selected.

init.sqf

Quote

JBOYSELECTINGTEAM = false;
player addAction ["Assault Building", "JBOYSELECTINGTEAM = true; []spawn{sleep 10; JBOYSELECTINGTEAM = false}", nil, 999, true, true, "", "count (groupSelectedUnits player) isEqualTo 0 && cursorTarget isKindOf 'Building' && !JBOYSELECTINGTEAM"];

//Teams
player addAction ["<t color='#FFFFFF'>TEAM WHITE Assault</t>", "assault.sqf", "MAIN", 999, true, true, "", "{(assignedTeam _x) isEqualTo 'MAIN' && !(isPlayer _x)}count units player > 0 && JBOYSELECTINGTEAM"];
player addAction ["<t color='#FF0000'>TEAM RED Assault</t>", "assault.sqf", "RED", 999, true, true, "", "{(assignedTeam _x) isEqualTo 'RED' && !(isPlayer _x)}count units player > 0 && JBOYSELECTINGTEAM"];
player addAction ["<t color='#00FF00'>TEAM GREEN Assault</t>", "assault.sqf", "GREEN", 999, true, true, "", "{(assignedTeam _x) isEqualTo 'GREEN' && !(isPlayer _x)}count units player > 0 && JBOYSELECTINGTEAM"];
player addAction ["<t color='#0000FF'>TEAM BLUE Assault</t>", "assault.sqf", "BLUE", 999, true, true, "", "{(assignedTeam _x) isEqualTo 'BLUE' && !(isPlayer _x)}count units player > 0 && JBOYSELECTINGTEAM"];
player addAction ["<t color='#FFFF00'>TEAM YELLOW Assault</t>", "assault.sqf", "YELLOW", 999, true, true, "", "{(assignedTeam _x) isEqualTo 'YELLOW' && !(isPlayer _x)}count units player > 0 && JBOYSELECTINGTEAM"];

 

You can play around with addAction/removeAction if you don't want the "condition is evaluated on each frame in non-scheduled environment"  work but it's a pain.

 

assault.sqf

Quote

private _team = _this select 3;
private _units = [];

JBOYSELECTINGTEAM = false;

{if ((assignedTeam _x) isEqualTo _team && !(_x isEqualTo player)) then{_units pushBack _x}}forEach units group player;

hint str [_team, _units];

 

Use the result from _units

 

Cheers,

Good luck.

Edited by RCA3
misspell on GREEN.
  • Like 2
  • Thanks 1

Share this post


Link to post
Share on other sites
5 minutes ago, RCA3 said:

My suggestion:

Use teams and two multipart actions.

Thanks much RCA3.  That will work perfectly.  Much appreciated.

Share this post


Link to post
Share on other sites

You can use one part action as well and it will look even better.

init.sqf

Quote

//Teams

player addAction ["<t color='#FFFFFF'>TEAM WHITE Assault</t>", "assault.sqf", "MAIN", 999, true, true, "", "{(assignedTeam _x) isEqualTo 'MAIN' && !(isPlayer _x)}count units player > 0 && count (groupSelectedUnits player) isEqualTo 0 && cursorTarget isKindOf 'Building'"];
player addAction ["<t color='#FF0000'>TEAM RED Assault</t>", "assault.sqf", "RED", 999, true, true, "", "{(assignedTeam _x) isEqualTo 'RED' && !(isPlayer _x)}count units player > 0 && count (groupSelectedUnits player) isEqualTo 0 && cursorTarget isKindOf 'Building'"];
player addAction ["<t color='#00FF00'>TEAM GREEN Assault</t>", "assault.sqf", "GREEN", 999, true, true, "", "{(assignedTeam _x) isEqualTo 'GREEN' && !(isPlayer _x)}count units player > 0 && count (groupSelectedUnits player) isEqualTo 0 && cursorTarget isKindOf 'Building'"];
player addAction ["<t color='#0000FF'>TEAM BLUE Assault</t>", "assault.sqf", "BLUE", 999, true, true, "", "{(assignedTeam _x) isEqualTo 'BLUE' && !(isPlayer _x)}count units player > 0 && count (groupSelectedUnits player) isEqualTo 0 && cursorTarget isKindOf 'Building'"];
player addAction ["<t color='#FFFF00'>TEAM YELLOW Assault</t>", "assault.sqf", "YELLOW", 999, true, true, "", "{(assignedTeam _x) isEqualTo 'YELLOW' && !(isPlayer _x)}count units player > 0 && count (groupSelectedUnits player) isEqualTo 0 && cursorTarget isKindOf 'Building'"];

 

assault.sqf

Quote

private _team = _this select 3;
private _units = [];

JBOYSELECTINGTEAM = false;

{if ((assignedTeam _x) isEqualTo _team && !(_x isEqualTo player)) then{_units pushBack _x}}forEach units group player;

hint str [_team, _units];

 

  • Like 3

Share this post


Link to post
Share on other sites

This is great, implementing now.  Question:  Why is the following part of the condition?

 count (groupSelectedUnits player) isEqualTo 0 

 

Share this post


Link to post
Share on other sites
5 hours ago, johnnyboy said:

This is great, implementing now.  Question:  Why is the following part of the condition?


 count (groupSelectedUnits player) isEqualTo 0 

 

Yes, you're right, unnecessary check please remove it. I had that from another script i have here.

The only thing it does is remove the action from the action list when you select a unit.

  • Like 1

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

×