Jump to content
sizraide

Using apply on group units

Recommended Posts

I have this script that changes the loadout of a unit

 

Spoiler

private _unit = _this select 0;

    _unit setVariable ["BIS_enableRandomization", false]; 
    _unit addGoggles "CUP_G_ESS_BLK";
        
    /****** RIFLE MAN ******/
    _classname = typeOf _unit;
    if(_classname == "i_soldier_F") then {
        [_unit] execVM "unit_loadouts\rifleman.sqf";
    };
    
    /****** GRENADIER MAN ******/
    if(_classname == "i_Soldier_GL_F") then {
        [_unit] execVM "unit_loadouts\grenadier.sqf";
    };
    
    /****** MEDIC MAN ******/
    if(_classname == "i_medic_F") then {        
        [_unit] execVM "unit_loadouts\medic.sqf";
    };
    
    /****** AUTORIFLE MAN ******/
    if(_classname == "i_soldier_ar_f") then {        
        [_unit] execVM "unit_loadouts\autorifleman.sqf";
    };
    
    /****** TEAMLEADER MAN ******/
    if(_classname == "i_soldier_tl_f") then {        
        [_unit] execVM "unit_loadouts\teamleader.sqf";
    };
    
    /****** MARKSMAN MAN ******/
    if(_classname == "i_soldier_m_f") then {        
        [_unit] execVM "unit_loadouts\marksman.sqf";
    };
        
    /****** RIFLEMAN AT MAN ******/
    if(_classname == "i_soldier_lat_f") then {        
        [_unit] execVM "unit_loadouts\rifleman_at.sqf";
    };

 

 

testGroup = createGroup [independent, true];
testGroup = [getPos trg1, independent, (configfile >> "CfgGroups" >> "Indep" >> "IND_F" >> "Infantry" >> "HAF_InfSquad")] spawn BIS_fnc_spawnGroup;
{ [_x] execVM "unit_loadouts\checkgear.sqf" } forEach units testGroup;

OR

(units testGroup) apply {[_x] execVM "unit_loadouts\checkgear.sqf"};

I'm trying to create a group, and apply the script to each member of the group using apply.

Don't know if there is another way, couldn't find any other way.

Share this post


Link to post
Share on other sites

The BIS_fnc_spawnGroup function creates the group itself, so there is no need to first create it with the createGroup command.

You can read about how the apply command works here. With its help, you cannot change the equipment of units.

Your first code should work correctly. Anyway, it will run your script with every unit in the group as an argument. If it does't work, look for the error in the script itself.

{ [_x] execVM "unit_loadouts\checkgear.sqf" } forEach units testGroup;

 

Share this post


Link to post
Share on other sites
1 minute ago, Ibragim A said:

The BIS_fnc_spawnGroup function creates the group itself, so there is no need to first create it with the createGroup command.

You can read about how the apply command works here. With its help, you cannot change the equipment of units.

Your first code should work correctly. Anyway, it will run your script with every unit in the group as an argument. If it does't work, look for the error in the script itself.


{ [_x] execVM "unit_loadouts\checkgear.sqf" } forEach units testGroup;
 

 


Thats what I am confused about, why is it not working?

in my unit_loadouts folder I have one of the loadout scripts here:

 

Spoiler

private _unit = (_this select 0);

removeAllWeapons _unit;
		
//Add rifle of unit
_unit addMagazines ["30Rnd_556x45_Stanag", 10];
_unit addWeapon (selectRandom["CUP_arifle_M4A3_black", "CUP_arifle_M4A1"]);
			
//Adding equipment on rifle
_unit addPrimaryWeaponItem (selectRandom["CUP_optic_CompM2_low", "CUP_optic_ACOG2"]);
_unit addPrimaryWeaponItem "CUP_acc_Flashlight";
			
//Add pistol of unit
_unit addMagazines ["9Rnd_45ACP_Mag", 4];
_unit addWeapon "hgun_ACPC2_F";
		
//Add backpack
_unit addBackpack "B_AssaultPack_dgtl";
(unitBackpack _unit) addItemCargo ["FirstAidKit", 8];
(unitBackpack _unit) addmagazineCargo ["handgrenade", 3];
(unitBackpack _unit) addmagazineCargo ["SmokeShell", 2];
(unitBackpack _unit) addmagazineCargo ["SmokeShellRed", 2];
(unitBackpack _unit) addmagazineCargo ["SmokeShellGreen", 2];

 

 

Share this post


Link to post
Share on other sites

What exactly isn't working? Group not being created? Are their equipment or weapons not changing? The script looks correct.

Share this post


Link to post
Share on other sites
1 minute ago, Ibragim A said:

What exactly isn't working? Group not being created? Are their equipment or weapons not changing? The script looks correct.

 

Yeah, the weapons and equipments are not changing.

The group spawns perfectly fine, when I execute the code no error pops up.

 

So I can't tell what the issue.

Share this post


Link to post
Share on other sites

Spawn that:


 

[] spawn {
  // testGroup = createGroup [independent, true]; // totally useless (due to next line)
  testGroup = [getPos trg1, independent, (configfile >> "CfgGroups" >> "Indep" >> "IND_F" >> "Infantry" >> "HAF_InfSquad")] call BIS_fnc_spawnGroup; // call it
  sleep 0.5;  // if needed: spawned units can be not completed before next line
  {
    _x execVM "unit_loadouts\checkgear.sqf"
  } forEach units testGroup;
};

 

  • Like 1

Share this post


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

Spawn that:


 


[] spawn {
  // testGroup = createGroup [independent, true]; // totally useless (due to next line)
  testGroup = [getPos trg1, independent, (configfile >> "CfgGroups" >> "Indep" >> "IND_F" >> "Infantry" >> "HAF_InfSquad")] call BIS_fnc_spawnGroup; // call it
  sleep 0.5;  // if needed: spawned units can be civilian just after spawn, before joining their group
  {
    _x execVM "unit_loadouts\checkgear.sqf"
  } forEach units testGroup;
};
 

 

 

 

It works now, appreciate it. Sleep 0.5 did the trick.

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

×