Jump to content
foffy

Is there a way to automatically give units items as a mission loads?

Recommended Posts

Hey friends. I know you can manually add items to units in the editor, but how exactly would one go about making a script or something of the like that ArmA II can access that would add items to all units in the game?

 

For example, if I wanted to give everyone a medkit, how could I go about this without using the editor and manually adding one to each unit? I was curious if this could be done, and if so, how.

 

Hope my question isn't dumb or impossible for the game. :P

Share this post


Link to post
Share on other sites

in init.sqf

{
      _x addItem "your-item-classname";
} forEach allUnits;

will add a item of your choice to all units on mission start.

 

note: oops this is the arma 2 OA forums, my bad. That specific won't work, but you should be able to substitute it for something else that will. the forEach part should be fine though.

Share this post


Link to post
Share on other sites

Place a soldier in the editor and either name him, and use a script, or use his init field directly. 

removeAllWeapons this;  or removeAllWeapons soldiername; 

{this addMagazine "magazineclassname"} forEach [1,2,3,4,5,6]; or {soldiername addMagazine "magazineclassname"} forEach [1,2,3,4,5,6]; 
{this addMagazine "magazineclassname"} forEach [7,8,9,10];
{this addMagazine "magazineclassname"} forEach [11,12];  
.
.
.

this addWeapon "weaponclassname"; or soldiername addWeapon "weaponclassname";
this addWeapon "weaponclassname";
this addWeapon "weaponclassname";
this addWeapon "weaponclassname";
.
.
.
.
.
.
this addBackpack "backpackclassname"; or soliername addBackpack "backpackclassname";
(unitBackpack this) addmagazineCargoGlobal ["magazineclassname", number of magazines]; or (unitBackpack soldiername) addmagazineCargoGlobal ["magazineclassname", number of magazines];

(unitBackpack this) addmagazineCargoGlobal ["magazineclassname", number of magazines];
.
.
.

Remember you have max 12 slots for mainweapon magazines, grenades, smokes etc, and 8 slots for sidearmmagazines, launchergrenades etc. so if you add 2 handgrenades and one smoke, you can then add max 9 magazines to it.

 

Also rifle, pistol, binoculars, rangefinders, NVGoogles, GPS, etc are weapons.

Grenades, smokes, magazines etc are  magazines. 

 
Example (ACE Sniper with SVD Sniperrifle, and VZ58 assaultrifle as backup weapon: 
removeAllWeapons this;
{this addMagazine "30Rnd_762x39_SA58"} forEach [1,2,3,4,5];
this addMagazine "ACE_Battery_Rangefinder";
{this addMagazine "10Rnd_762x54_SVD"} forEach [6,7,8,9,10,11,12];
this addMagazine "ACE_LargeBandage";
this addWeapon "Sa58V_CCO_EP1";
this selectWeapon "Sa58V_CCO_EP1";

this setVariable ["ACE_weapononback", "ACE_SVD_Bipod"];	
{this addMagazine "ACE_LargeBandage"} forEach [1,2,3];
{this addMagazine "ACE_Morphine"} forEach [4,5];
{this addMagazine "ACE_Medkit"} forEach [7,8];
this addMagazine "ACE_Epinephrine";
this addWeapon "ACE_Earplugs";
this addWeapon "ACE_Rangefinder_OD";
this addWeapon "ACE_Map_Tools";
this addWeapon "ACE_KeyCuffs";
this addWeapon "ACE_Kestrel4500";
this addWeapon "NVGoggles";
this addWeapon "ItemGPS";

this addBackpack "CZ_Backpack_EP1";
(unitBackpack this) addmagazineCargoGlobal ["10Rnd_762x54_SVD",9]; 

(unitBackpack this) addmagazineCargoGlobal ["30Rnd_762x39_SA58",6];

 

OR Use 

{_x addweapon "weaponclassname"} forEach units group player;
{_x addmagazine "magazineclassname"} forEach units group player;

to add something to all soldiers within players group. (all guys connected with player by the blue line in the editor)

 

Also last time i played arma without ACE is 3 years ago, and i didnt do any editing back then, so if my advice wont work for you, then it probably only works with ACE.

Share this post


Link to post
Share on other sites

Thanks for the posts, but I'm still a little lost. From what I gather, this is either to a specific unit or the players group. Is there any way to set something up that is all encompassing? This is to say the players group, fellow friendly AI not in said group, and opposing AI? Something that just adds items for all elements for a mission?

 

To use ACE as an example, I was thinking of trying to set up something where when the mission loads, all participating units - player, friendly, and foe - all spawn on with a few more medical items, like bandages and morphine. I wanted to know what quick way this could be done as a script or something without having to rely on the editor to manually add these in to each element. Think like how in ArmA III every unit has at least one healing item in their inventory no matter the case; this is what I'm trying to do for ArmA II, essentially. To cite ACE 2, it's like how 'SP Medic' in ACE Options gives the player two medkits. I'd like to have something that adds items to all elements and not just the player and his/her squad.

Share this post


Link to post
Share on other sites

This should cycle through each unit in each group to give infantry soldiers a medkit.
It will only do so for infantry, not for mounted units (Air, Armor, Mech).  They would need separate commands detailed for them.
It also doesn't check the soldier's inventory space first, so it might not work if the unit doesn't have the space for it.

 

// Only run on server (Avoid multiple adding).
If (isServer) then 
{
	// Get every group in the mission
	_allGroups = allGroups;

	// Cycle through each group
	{
		// Get the units in the group
		_units = units _x;

		// Cycle through each unit in the group
		{
			// Exclude non-soldier types
			if ((vehicle _x) isKindOf "CAManBase") then 
			{
				// Give the soldier what you want him to have.
				_x addMagazine "ACE_Medkit";
			};
		} forEach _units;
	} forEach _allGroups;
};

 

Change the conditions, pile up and change the inventory commands as you wish, but basically your cycling each group, and each soldier in each group, to give or take away from them what you want.  You could add a block for vehicles that cycled through the crew.

Share this post


Link to post
Share on other sites

So, one would put that in the mission's init and see if it works? Is there a way to set something like that up without the mission init? Say, for example, I wanted to set this up for the ACR campaign; to my knowledge the campaign pbo cannot be opened and edited, so the only way I'd be able to set something like this up would be a script that automatically activates outside of that pbo. I wouldn't know where to start doing that. But I hope you're seeing why I wanted an automatic thing that just gave everyone these items. :P

 

Also, how would one alter that script to consider units that load in tanks and the like? I can look at what you posted and get a picture, but as I'm really unfamiliar with scripting in general, I don't know to to specify clearly between Infantry, Tanks, Air, and the like. I'm really coming into this blind and without any vision on how to accomplish it.

Share this post


Link to post
Share on other sites

Regarding the example you mention, BI has published most of the A2 PBO's on the Arma Licensed Data Pack (ALDP) webpage.  The ACR missions PBO is part of the ALDP, and falls under the Arma Public License - Share Alike (APL-SA).  The missions_acr.pbo is found in the ALDP_A2OADLC_PBOs_APL-SA.zip at the bottom of the ALDP webpage:
https://community.bistudio.com/wiki/Arma_Licensed_Data_Pack
https://community.bistudio.com/wiki/Licenses

 

What it means is you -can- download, open, copy and modify missions from the PBO.  It must be done under the terms of the APL-SA license.  It mainly governs the sharing or releasing of what you make from the PBO.

 

Once you have the mission, the script block posted above could go in the mission's init.sqf, or it could go into any script getting called, spawned, or execVM'd by another source.  It could even go into an object's init line in editor, but you first would have to replace the _local variables with global ones since _local variables don't work in editor.

(except the magic "_x" - they would remain the same.  They have a special property involving scope :ph34r:)

 

But first create a mission with a few units and run it using an init.sqf to see how it works.

 

For vehicle crews, I would think inside the "forEach _units" you could add cycles for each crewmember (not tested):

if ((vehicle _x) isKindOf "CAManBase") then 
{
	_x addMagazine "ACE_Medkit";
};

if ((vehicle _x) isKindOf "LandVehicle") then 
{
	_crew = crew _x;
	{_x addMagazine "ACE_Medkit";} forEach _crew;
};

if ((vehicle _x) isKindOf "Air") then 
{
	_crew = crew _x;
	{_x addMagazine "ACE_Medkit";} forEach _crew;
};

 

Scripting is a learning experience, and as with every learning experience, crawl... :confused:  walk... :mellow:  run...  B)

Ignore the grey hairs and rising blood pressure, it's both incidental and inevitable.  :butbut:

Share this post


Link to post
Share on other sites

Thanks. Though, is there a way to try and set up a script like an addon, so I essentially don't have to edit every ArmA II campaign I wanna play? I can do it manually, but that's a lot of work.  :P

 

And assuming I can mix the two scripts you posted before to get what I want, would this be an ideal result? I want two large bandages and one morphine to emulate ACE 3, as well as to work around the ACE 2 bug that's plagued ArmA II since last year; items have to be added to an AI's inventory or the AI has to be personally inspected for them to realize they have these items. It used to auto-add and auto-initialize as long as ACE Wounds was on, but now they have to be manually added to AI inventory, hence why I asked for a scripting workaround.

 

If (isServer) then
{  
    _allGroups = allGroups;

    { 
        _units = units _x;

        {
            if ((vehicle _x) isKindOf "CAManBase") then
            {
                // Give the soldier what you want him to have.
                _x addMagazine "ACE_LargeBandage";

                _x addMagazine "ACE_LargeBandage";

                _x addMagazine "ACE_Morphine";
            };
        } forEach _units;
    } forEach _allGroups;
};

 

if ((vehicle _x) isKindOf "CAManBase") then
{
    _x addMagazine "ACE_Medkit";

    _x addMagazine "ACE_LargeBandage";

    _x addMagazine "ACE_Morphine";

};

if ((vehicle _x) isKindOf "LandVehicle") then
{
    _crew = crew _x;
    {_x addMagazine "ACE_LargeBandage";_x addMagazine "ACE_LargeBandage";_x addMagazine "ACE_Morphine";} forEach _crew;

};

if ((vehicle _x) isKindOf "Air") then
{
    _crew = crew _x;
    {_x addMagazine "ACE_LargeBandage";_x addMagazine "ACE_LargeBandage";_x addMagazine "ACE_Morphine";} forEach _crew;
};

Share this post


Link to post
Share on other sites

I don't know, on both counts.  Somebody more knowledgeable would need to weigh in on that.

 

Tested, and this works in an init.sqf:

 

If (isServer) then 
{  
	_allGroups = allGroups;
	{ 
		_units = units _x;
		{
			if ((vehicle _x) isKindOf "CAManBase") then
			{
				_unit = _x;
		//		_magazines = magazines _x;
		//		{_unit removeMagazine _x} forEach _magazines;

				// The repetitive adding method:
				_x addMagazine "ACE_LargeBandage";
				_x addMagazine "ACE_LargeBandage";
				_x addMagazine "ACE_Morphine";
			};

			if ((vehicle _x) isKindOf "LandVehicle") then 
			{
				_crew = crew _x;
				{
					_unit = _x;
		//			_magazines = magazines _x;
		//			{_unit removeMagazine _x} forEach _magazines;

					// A less repetitive way of adding:
					{_unit addMagazine "ACE_LargeBandage"} forEach [1,2];
					_x addMagazine "ACE_Morphine";
				} forEach _crew;
			};

			if ((vehicle _x) isKindOf "Air") then 
			{
				_crew = crew _x;
				{
					_unit = _x;
		//			_magazines = magazines _x;
		//			{_unit removeMagazine _x} forEach _magazines;

					{_unit addMagazine "ACE_LargeBandage"} forEach [1,2];
					_x addMagazine "ACE_Morphine";
				} forEach _crew;
			};
		} forEach _units;
	} forEach _allGroups;
};

 

The magazine lines that are commented out, if you uncomment them the units will start with no magazines except the three ACE items.  The lines are there for if you or somebody wants to try doing full magazine loadouts for each of the unit types.

 

// edit -- ha, I edited as you were posting.  Glad you worked it out.

Edited by opusfmspol

Share this post


Link to post
Share on other sites

I was able to test the above by applying this to an init, and it seems to be working, at least for infantry. For tanks and air units, it is apparently not working at all. I realized the above script wasn't ideal, so it had to essentially match your first script and merely replace the value being referenced. You have to essentially repeat it three times.

 

If (isServer) then 
{  
    _allGroups = allGroups;
 
    { 
        _units = units _x;
 
        {
            if ((vehicle _x) isKindOf "LandVehicle") then
            {
                // Give the soldier what you want him to have.
                _x addMagazine "ACE_LargeBandage";
                _x addMagazine "ACE_LargeBandage";
                _x addMagazine "ACE_Morphine";
            };
        } forEach _units;
    } forEach _allGroups;
};
 
 
If (isServer) then 
{  
    _allGroups = allGroups;
 
    { 
        _units = units _x;
 
        {
            if ((vehicle _x) isKindOf "Air") then
            {
                // Give the soldier what you want him to have.
                _x addMagazine "ACE_LargeBandage";
                _x addMagazine "ACE_LargeBandage";
                _x addMagazine "ACE_Morphine";
            };
        } forEach _units;
    } forEach _allGroups;
};
 
If (isServer) then 
{  
    _allGroups = allGroups;
 
    { 
        _units = units _x;
 
        {
            if ((vehicle _x) isKindOf "CAManBase") then
            {
                _x addMagazine "ACE_LargeBandage";
                _x addMagazine "ACE_LargeBandage";
                _x addMagazine "ACE_Morphine";
            };
        } forEach _units;
    } forEach _allGroups;
};

 
It seems to be working for me, though as I said, I would like to figure out how to set this up as a setup beyond editing the missions themselves. Say if I want to play CWR2, ArmA Rearmed 2, and all of ArmA II's campaigns, that's a lot of adding this in. :P
 
Thank you for helping me this much, at the very least. You've really helped a problem I had with the game for months.
 
I have a pbo of it here, for those interested.
  • Like 1

Share this post


Link to post
Share on other sites

 

I was able to test the above by applying this to an init, and it seems to be working, at least for infantry. For tanks and air units, it is apparently not working at all. I realized the above script wasn't ideal, so it had to essentially match your first script and merely replace the value being referenced. You have to essentially repeat it three times.

 

If (isServer) then 
{  
    _allGroups = allGroups;
 
    { 
        _units = units _x;
 
        {
            if ((vehicle _x) isKindOf "LandVehicle") then
            {
                // Give the soldier what you want him to have.
                _x addMagazine "ACE_LargeBandage";
                _x addMagazine "ACE_LargeBandage";
                _x addMagazine "ACE_Morphine";
            };
        } forEach _units;
    } forEach _allGroups;
};
 
 
If (isServer) then 
{  
    _allGroups = allGroups;
 
    { 
        _units = units _x;
 
        {
            if ((vehicle _x) isKindOf "Air") then
            {
                // Give the soldier what you want him to have.
                _x addMagazine "ACE_LargeBandage";
                _x addMagazine "ACE_LargeBandage";
                _x addMagazine "ACE_Morphine";
            };
        } forEach _units;
    } forEach _allGroups;
};
 
If (isServer) then 
{  
    _allGroups = allGroups;
 
    { 
        _units = units _x;
 
        {
            if ((vehicle _x) isKindOf "CAManBase") then
            {
                _x addMagazine "ACE_LargeBandage";
                _x addMagazine "ACE_LargeBandage";
                _x addMagazine "ACE_Morphine";
            };
        } forEach _units;
    } forEach _allGroups;
};

 
It seems to be working for me, though as I said, I would like to figure out how to set this up as a setup beyond editing the missions themselves. Say if I want to play CWR2, ArmA Rearmed 2, and all of ArmA II's campaigns, that's a lot of adding this in. :P
 
Thank you for helping me this much, at the very least. You've really helped a problem I had with the game for months.
 
I have a pbo of it here, for those interested.

 

Apparently I need a decryption key to access your file.

Would you care to oblige?

Many thanks

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

×