You don't need to add a keybind to the function keys, the groupSelectedUnits command returns those selected units.
Sometimes I like to think as I started the whole "earplugs" thing.
W0lle: The only advice I can give you is: Do not try to understand BI. You will not succeed and it only makes your brain go boom. I would even go so far and say that not even they understand their own actions :-D.
The groupselectedunits returns multiple AI units in array so how should one select individual units using this - https://community.bistudio.com/wiki/groupSelectUnit command in a script? Can anyone post a example script for both GroupselectedUnits and groupselectUnit?
Do you want to select some units for the player, or get the units that the player has selected? You said a script will run with the selected units so I assumed it was the latter.
private "_array";
_array = groupSelectedUnits player; // Will return units selected with F2, F3...
// Will select the first two units, highlighting them in the group bar
player groupSelectUnit [(units player) select 1, true];
player groupSelectUnit [(units player) select 2, true];
Sometimes I like to think as I started the whole "earplugs" thing.
W0lle: The only advice I can give you is: Do not try to understand BI. You will not succeed and it only makes your brain go boom. I would even go so far and say that not even they understand their own actions :-D.
I want to select individual units by pressing F2,F3,F4 keys etc, the code Groupselectedunits works but if i check the units status or if the unit is in vehicle in script it gives error - "Type array,Expected object". While without the units check the script works.
my example script below-
I have placed 3 ai units on map with player as leader of group and helipad h1.
Activate in radio alpha- null = [] execVM "select.sqf";
private['_handled'];
_handled = false;
sleep 0.1;
switch (_this select 1) do
{
//Key f2
case 60: {
unit2 = groupSelectedUnits player;
hint "Key F2 selected!";
if (!alive unit2) exitwith {hintsilent "unit not available!";};
if (vehicle unit2!= unit2) exitwith {hintsilent "Selected unit cannot move while in vehicle!,call script again!"};
unit2 domove getpos h1;
//This will ensure only one unit is selected at a time.
(findDisplay 46) displayRemoveAllEventHandlers "KeyDown";
};
//Key f3
case 61: {
hint "Key F3 selected!";
unit3 = groupSelectedUnits player;
unit3 domove getpos h1;
sleep 3;
//This will ensure only one unit is selected at a time.
(findDisplay 46) displayRemoveAllEventHandlers "KeyDown";
};
//Key f4
case 62: {
hint "Key F4 selected!";
unit4 = groupSelectedUnits player;
unit4 domove getpos h1;
sleep 3;
//This will ensure only one unit is selected at a time.
(findDisplay 46) displayRemoveAllEventHandlers "KeyDown";
};
};
_handled;
Look at the wiki page, groupSelectedUnits returns an array; that means a list of units. When you write `unit2 = groupSelectedUnits player;` or `unit3 = groupSelectedUnits player;`, what you're getting is actually something like this:
That's why you get an error that says "Type array,Expected object", because you're trying to use an array instead of a unit. You'd need to use the select command to pick a specific unit from that array, or use a forEach loop to do something with all the objects in the array.
Hope it's clear enough now, you don't really need to something specific for each key press, you just have to make sure it's an 'FX' key, then use groupSelectedUnits.
Sometimes I like to think as I started the whole "earplugs" thing.
W0lle: The only advice I can give you is: Do not try to understand BI. You will not succeed and it only makes your brain go boom. I would even go so far and say that not even they understand their own actions :-D.
i have corrected the errors in previous post now the script is able to select single units with keypress.
To use this script press windows left key once and then a select any single unit by pressing any F2,F3...F12 within 5 seconds.Once "unit selected" hint appears a visual marker will appear and will be placed at any structure height/level near the player's position, the selected unit will then move to the visual markers position.This script allows units in group to move to any buildings floor levels or inside structures.You can also use classname "Sign_arrow_down_large_EP1" as markericon in selectunit.sqf.
create init.sqf,selectunit.sqf,setunitpos.sqf and place in mission folder-
init.sqf
sleep 1;
disableSerialization;
_display = findDisplay 46;
_display displaySetEventHandler ["KeyDown","_nil = _this execVM ""selectUnit.sqf"""];
titleText ["Press windows Left key once to activate script","plain down"];
selectunit.sqf
private['_handled'];
_handled = false;
sleep 0.1;
switch (_this select 1) do
{
// KEY windows Left
case 219:
{
markerIcon = "Sign_sphere100cm_EP1" createVehicle [0,0,0];
_handled = true;
titleText ["Select any unit (F2,F3,F4..F12) once & point cursor towards position to move unit to position!","plain down"];
sleep 1;
hintsilent "select any single unit in 5 seconds!";
sleep 1;
hintsilent "select unit in 5";
sleep 1;
hintsilent "select unit in 4";
sleep 1;
hintsilent "select unit in 3";
sleep 1;
hintsilent "select unit in 2";
sleep 1;
hintsilent "select unit in 1";
sleep 1;
SquadUnits = groupSelectedUnits player;
unit = Squadunits select 0;
sleep 1;//Exit script if selected AI is in vehicle.
if (vehicle unit!= unit) exitwith {hintsilent "Selected unit cannot move while in vehicle!,call script again!";deletevehicle markerIcon;};
sleep 1;//Exit script if no AI unit is selected.
if (isNil "unit") exitwith {hintsilent "No unit selected,call script again!";deletevehicle markerIcon;};
hintsilent "unit selected!";
moveai = [markerIcon,unit] execVM "setunitpos.sqf";
waituntil {scriptdone moveai};
hintsilent "Select other units!";
};
};
_handled;
if (!isServer) exitWith {}; // no sense as in SP/hosted player is in server, in MP dedicated, there is no player on server.
if (!hasInterface) exitWith {}; // preferable, but be sure there will be no code for a dedi server (or HC) below this line.
sleep 1; // wath for?
waitUntil {!isNull (findDisplay 46)}; // preferable
(findDisplay 46) displaySetEventHandler ["KeyDown","_nil = _this execVM ""selectUnit.sqf"""]; // works also
titleText ["Press windows Left key once to activate script","plain down"];
selectUnit.sqf
if (!isServer) exitWith {}; // no sense
private _handled = false; // see below, added a line because this variable is worthy to avoid script repetition while maintaining the key down...
sleep 0.1; // what for?
switch (_this select 1) do // mono case? --> use if .. then..
{
// KEY windows Left
case 219:
{
markerIcon = "Sign_sphere100cm_EP1" createVehicle [0,0,0];
_handled = true; // missing line. Now your script returns true and script doesn't repeat if key stays down.
titleText ["Select any unit (F2,F3,F4..F12) once & point cursor towards position to move unit to position!","plain down"];
sleep 1;
hintsilent "select any single unit in 5 seconds!"; // a loop could be fine below.
sleep 1;
hintsilent "select unit in 5";
sleep 1;
hintsilent "select unit in 4";
sleep 1;
hintsilent "select unit in 3";
sleep 1;
hintsilent "select unit in 2";
sleep 1;
hintsilent "select unit in 1";
sleep 1;
SquadUnits = groupSelectedUnits player;
unit = Squadunits select 0;
sleep 1; // what for?
if (vehicle unit!= unit) exitwith {hintsilent "Selected unit cannot move while in vehicle!,call script again!";deletevehicle markerIcon;};
sleep 1; // what for?
if (isNil "unit") exitwith {hintsilent "No unit selected,call script again!";deletevehicle markerIcon;};
hintsilent "unit selected!";
moveai = [markerIcon,unit] execVM "setunitpos.sqf";
waituntil {scriptdone moveai};
hintsilent "Select other units!";
};
};
_handled;
setUnitPos.sqf
_marker = _this select 0;
_selectedunit = _this select 1;
sleep 1; // what for?
_marker attachTO [player,[0,3,1.3]];
sleep 5;
_marker setvelocity [0,0,0]; // useless.
detach _marker;
_marker setPosATL getPosATL player; // useless because the line below
_marker setposATL (player modelToWorld [0,3,0]); // So 3 meters in front of player?? Where is the cursor? I was waiting for something like an "onEachFramed" screenToWorld [0.5,0.5]
sleep 0.1;
_selectedunit lookat _marker; // what for?
_selectedunit dowatch _marker; // useless if the line above
_selectedunit domove getposATL _marker;
_selectedunit setspeedmode "FULL";
sleep 1;
waituntil {unitready _selectedunit};
_selectedunit lookat objnull;
_selectedunit dowatch objnull;
deletevehicle _marker;
exit; // useless
PLEASE CONTACT ME ON BI FORUMS FOR ANY SCRIPT / MOD QUESTION. TKS
This script is only for placing units at any height in buildings level like rooftops or any floor level etc not for placing on terrain level thats why I didnt use screentoworld because it will only place the visual marker on terrain/ground not on structures floor level.And there is no command for moving the visual marker with cursor.Thanks for correcting missing codes.
sleep 1;
disableSerialization;
_display = findDisplay 46;
_display displaySetEventHandler ["KeyDown","_nil = _this execVM ""selectUnit.sqf"""];
titleText ["Press windows Left key once to activate script","plain down"];
selectunit.sqf
private['_handled'];
_handled = false;
sleep 0.1;
switch (_this select 1) do
{
// KEY windows Left
case 219:
{
_handled = true;
hintsilent "select any single unit in 5 seconds!";
sleep 1;
hintsilent "select unit in 5";
sleep 1;
hintsilent "select unit in 4";
sleep 1;
hintsilent "select unit in 3";
sleep 1;
hintsilent "select unit in 2";
sleep 1;
hintsilent "select unit in 1";
sleep 1;
SquadUnits = groupSelectedUnits player;
unit = Squadunits select 0;
sleep 1;//Exit script if selected AI is in vehicle.
if (vehicle unit!= unit) exitwith {hintsilent "Selected unit cannot move while in vehicle!,call script again!";};
sleep 1;//Exit script if no AI unit is selected.
if (isNil "unit") exitwith {hintsilent "No unit selected,call script again!";};
hintsilent "unit selected!";
//Your code or script handle here....
};
};
_handled;
This post was edited by warbird (2017-02-15 21:13, ago)
Where are the building positions???
And, may I suggest you to trigger this while windows key is down, then freeze and go when windows key is up?
Why do you limit this script to one unit only?
This post was edited by Pierre MGI (2017-02-06 07:49, ago)
PLEASE CONTACT ME ON BI FORUMS FOR ANY SCRIPT / MOD QUESTION. TKS
I have edited my script now u can select single as well as multiple units with this version.
Place init.sqf, selectunits.sqf & setunitspos.sqf in your mission folder.
init.sqf
waitUntil {!isNull(findDisplay 46)};
disableSerialization;
_display = findDisplay 46;
_display displaySetEventHandler ["KeyDown","_nil = _this execVM ""selectUnits.sqf"""];
titleText ["Press windows Left key once to activate script","plain down"];
selectunits.sqf
private['_handled'];
_handled = false;
sleep 0.1;
switch (_this select 1) do
{
// KEY windows Left
case 219:
{
markerIcon = "Sign_arrow_down_large_EP1" createVehicle [0,0,0];
titleText ["Select any units (F2,F3,F4..F12) once & place marker towards position to move units to position!","plain down"];
_handled = true;
sleep 1;
hintsilent "select units in 5 seconds!";
sleep 1;
hintsilent "select units in 5";
sleep 1;
hintsilent "select units in 4";
sleep 1;
hintsilent "select units in 3";
sleep 1;
hintsilent "select units in 2";
sleep 1;
hintsilent "select units in 1";
sleep 1;
SquadUnits = groupSelectedUnits player;
countunits = count squadUnits;
sleep 0.1;
if (countunits == 0) exitwith {hintsilent "No units selected!,initiate script again!;";deletevehicle _marker;};
sleep 1;
hintsilent "units selected!";
moveai = [markerIcon,SquadUnits] execVM "setunitspos.sqf";
waituntil {scriptdone moveai};
hintsilent "Select other units!";
};
};
_handled;