Jump to content
Sign in to follow this  
jshock

Defining Classname within a Script

Recommended Posts

Hello scripting community,

I am trying to define a civilian, "C_man_1", to be exact, as an object of my script so that I can change just this civilians clothes and actions, not the entire civilian side (so every C_man_1 that I place in the editor will be put into this "group" of objects to be changed in the way I want them to). Below is one of the ways I tried it, but I'm still working my way up in scripting ability, so any help would be greatly appreciated!

_units = "C_man_1";

Share this post


Link to post
Share on other sites

Try something like this

_units = [];

{
 if (typeOf _x == "C_man_1") then   {
   _units set [count _units, _x];
 };
}forEach allUnits;

hint str (count _units);

Basic rundown: define an array called _units. Loop through each units on the mission, if they're the type of "c_man_1" then add them to the array. Once done, hint the amount of units / objects in _units.

Share this post


Link to post
Share on other sites

Works perfectly thanks a lot!

Another question, if you can think of a way to do it, because I've been racking my brain forever... I'm trying to put in an array of weapons to arm these civilians (now insurgents) which is easy, but I need to put the array of ammunition as well to match the weapons I put in the array, how would I make sure the right ammo gets to the right weapon on the right civilian, and say there is a large number of weapons and a bunch of "if" statements would just get messy?

I'm using the BIS_fnc_selectRandom function for the weapons array to ensure randomness.

Share this post


Link to post
Share on other sites

Hmm, the way I have approached that one is as follows:

private ["_muzzles", "_mags"];
//<...>


_muzzles = getArray(configfile >> "cfgWeapons" >> (_w select 0) >> "muzzles");

{
	if (_x=="this") then {
		_mags = getArray(configfile >> "cfgWeapons" >> (_w select 0) >> "magazines");
		{
			player addMagazines [_x, 1];
		}forEach [_mags select 0];
	} else {
		_mags = getArray(configfile >> "cfgWeapons" >> (_w select 0) >> _x >> "magazines");
		{
			player addMagazines [_x, 1];
		}forEach [_mags select 0];
	};
}forEach _muzzles;

In the above, "(_w select 0)" is actually just the classname of the weapon, as a string.

"[_mags select 0]" actually only resolves to one magazine, so you only get one added. Just change the quantity used in the "addMagazines" call if you want to add more than 1.

The reason for the "foreach" statements is that if you change "[_mags select 0]" to "_mags" it will add one of each type of usable magazine (rather than just the first one found)... although the unit needs carrying capacity or they will not all be added.

Hope that's not too confusing, but I'll be glad to clarify anything if you have specific questions. Sorry about the "_w select 0" thing, as I said, just replace that with the weapon classname obtained from your random selection. So if you do _weap = <whatever code gets a random weapon classname>; then replace every instance of (_w select 0) with (_weap). That SHOULD cover it but it only takes the first magazine type found, so if the person who coded the muzzle listed supported magazines out of order, you might get tracers instead of normal rounds (or some such).

Share this post


Link to post
Share on other sites

Ok so, I get all of them into their new "gear" and random weapons, and using your magazine finder above, but get no magazines, now I probably have it in the wrong place in the script but it's below, let me know:

_units = [];
_backpackarr = ["B_FieldPack_cbr","B_OutdoorPack_tan"];
_weaponarr = ["LMG_Zafir_F","arifle_Mk20_F"];
_uniformarr = ["U_IG_Guerilla3_1","U_IG_Guerilla3_2","U_IG_Guerilla1_1","U_IG_Guerilla2_1","U_IG_Guerilla2_2","U_IG_Guerilla2_3","U_IG_leader"];
_headarr = ["H_Shemag_olive","H_ShemagOpen_khk","H_ShemagOpen_tan"];

{
if (typeOf _x == "C_man_1") then {
	_units set [count _units, _x];
};

} foreach allunits;

if (isServer) then {

{

_backpack = _backpackarr call BIS_fnc_selectRandom;
_uniform = _uniformarr call BIS_fnc_selectRandom;
_head = _headarr call BIS_fnc_selectRandom;
_weapon = _weaponarr call BIS_fnc_selectRandom;

clearItemCargo _x;
clearWeaponCargo _x;
clearMagazineCargo _x;
removeallWeapons _x;
removeAllHandgunItems _x;
removeHeadgear _x;
removeGoggles _x;
removeUniform _x;
removeBackpack _x;
_x addBackpack _backpack;
_x addWeapon _weapon;
_x addHeadgear _head;
_x addUniform _uniform;


} foreach _units;

private ["_muzzles", "_mags"]; 

  	_muzzles = getArray(configfile >> "cfgWeapons" >> (_weapon) >> "muzzles"); 

  	 { 
      		 if (_x=="this") then { 

		_mags = getArray(configfile >> "cfgWeapons" >> (_weapon) >> "magazines"); 

		{ 
			_units addMagazines [_x, 10]; 

		} forEach [_mags select 0]; 

	} 

	else { 

		_mags = getArray(configfile >> "cfgWeapons" >> (_weapon) >> _x >> "magazines"); 

		{

			_units addMagazines [_x, 10]; 

           	} forEach [_mags select 0]; 
        	}; 

   	 } forEach _muzzles;  

};

Share this post


Link to post
Share on other sites

Gah, sorry man! Try this

_units = [];
_backpackarr = ["B_FieldPack_cbr","B_OutdoorPack_tan"];
_weaponarr = ["LMG_Zafir_F","arifle_Mk20_F"];
_uniformarr = ["U_IG_Guerilla3_1","U_IG_Guerilla3_2","U_IG_Guerilla1_1","U_IG_Guerilla2_1","U_IG_Guerilla2_2","U_IG_Guerilla2_3","U_IG_leader"];
_headarr = ["H_Shemag_olive","H_ShemagOpen_khk","H_ShemagOpen_tan"];

{
if (typeOf _x == "C_man_1") then {
	_units set [count _units, _x];
};

} foreach allunits;

if (isServer) then {

{

_backpack = _backpackarr call BIS_fnc_selectRandom;
_uniform = _uniformarr call BIS_fnc_selectRandom;
_head = _headarr call BIS_fnc_selectRandom;
_weapon = _weaponarr call BIS_fnc_selectRandom;
  	_muzzles = getArray(configfile >> "cfgWeapons" >> (_weapon) >> "muzzles"); 
       _unit = _x; // _x will be superceded, below, so rename it so we can still access it

clearItemCargo _x;
clearWeaponCargo _x;
clearMagazineCargo _x;
removeallWeapons _x;
removeAllHandgunItems _x;
removeHeadgear _x;
removeGoggles _x;
removeUniform _x;
removeBackpack _x;
_x addBackpack _backpack;
_x addHeadgear _head;
_x addUniform _uniform;

  	 { 
      		 if (_x=="this") then { // no longer does _x refer to the unit
		_mags = getArray(configfile >> "cfgWeapons" >> (_weapon) >> "magazines"); 
		{ 
			_unit addMagazines [_x, 10]; 
		} forEach [_mags select 0]; 
	} 
	else { 
		_mags = getArray(configfile >> "cfgWeapons" >> (_weapon) >> _x >> "magazines"); 
		{
				_unit addMagazines [_x, 10]; 
           	} forEach [_mags select 0]; 
        	}; 
   	 } forEach _muzzles;  

_x addWeapon _weapon;


} foreach _units;



};

Haven't actually tried this but hopefully you can glean the solution if it doesn't work precisely that way. The problem is, my script only gave it to the player, and I think addMagazines only works on units (not unitArrays)

Share this post


Link to post
Share on other sites

Cuel & Dwringer both of you are amazing thanks for the help both of your sections work perfectly in the script, and I can't thank you enough.

In fact, I was contemplating making the script public, under my regiments name, but will happily give both of you full credit with your contributions to this script, if you would both allow me to do so.

PM on your decisions so we don't get this thread locked/flagged and so on, I know the rules are tough when we get off subject.

Thanks a lot again, no matter your decision,

J.Shock and the whole of the J-SERE Development Team

PS - It seems to work seamlessly with mods as well(or at least the couple I have personally tested), and this could be a great tool for the Arma Community.

Share this post


Link to post
Share on other sites

Heh no need to credit my suggestion, I didn't plagiarize anybody but I basically copied others when figuring out how to access the configs :) Anyway, to keep this post on topic, I want to point out that my technique will actually not fill the player's inventory with magazines (if the capacity WOULD fill the player's inventory). For example, if the player has room for 9 magazines plus one in the gun, the player will only get 9 magazines. Instead, add just ONE magazine, then add the weapon, then add the remaining magazines - that way you get all 10. This is a minor issue and not really worth coding a solution IMHO. But it is an issue nonetheless ;)

Share this post


Link to post
Share on other sites

Yeah with the random backpack chosen, it just tosses the extra all in there anyhow, so unless someone really wanted no backpacks, there isn't much of an issue :P.

Share this post


Link to post
Share on other sites

Sure, don't need my permission for that short code :)

Share this post


Link to post
Share on other sites

Well either way thanks to the both of you, and I added a special thanks line to the post for you anyways, I just really appreciate the help!

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  

×