Jump to content
Sign in to follow this  
jaynic

Event Handler for the spawning of units / groups / vehicles etc...

Recommended Posts

Hi all,

I currently use MCC, and zeus, and some content mods (RHS Escalation etc...)

What I'd like to do is write custom loadouts for all units and vehicles - but it would not be good practice for me to manually edit the mods - as that will require the same effort when the mod updates. Rather: I need to write a wrapper class around the spawning of units both in the init line of any manually placed unit in the mission file (this process I understand) but have the ability to call the same functions when the units are spawned via zeus, or mcc.

I'm hoping there is an event handler for when ANY unit or vehicle or object is spawned in the game that I can attach a script call to via my mission.

pseudo code:

addEventHandler ["onObjectSpawn", [_theObject], "pathTo/myScript.sqf"]

I'm pretty new to arma scripting - but I can kinda tell what needs to get done - but I don't know the syntax or where to find out what to do.

Thanks

Share this post


Link to post
Share on other sites

As far as I know, you can't write a custom event handler, the easiest way to do what I think you want to do is write a script for the loadouts, then run the script with the unit passed as a parameter. Something like:

_obj = createVehicle ["B_Soldier_F", position player, [], 0, "NONE"];
[_obj] execVM "myCustomLoadout.sqf";

Share this post


Link to post
Share on other sites

Thanks for the reply. Hmm - so there is no eventHandler for "createVehicle" - that's silly.

So is there any other way to tell when an object is created, or initialized WITHOUT attaching a script directly to the unit? The notion being that: I do not know what unit is spawning - until it is spawned: therefore I must manipulate said unit after it spawns - or ideally as it is spawning.

Share this post


Link to post
Share on other sites

There are event handlers for zeus things, take a look at this page (curator means zeus player). The only other way to get all units as soon as they spawn would be some kind of hacky workaround like this:

[] spawn
{
_old = allMissionObjects "ALL";
while {true} do
{
	waitUntil {_old != (allMissionObjects "ALL")};
	_new = _old - (allMissionObjects "ALL");
	_old = allMissionObjects "ALL";
	{
		[_x] execVM "myScript";
	}forEach _new;
};
};

But allMissionObjects is a demanding command, so I'm not sure how useful that snippet will be for you. You might also want to invest time in asking the mod creators if they've included any way to get an array of units that their mod spawns.

Share this post


Link to post
Share on other sites

Yeah... I can see how that would be very taxing - considering I'm looping through everything placed each time something is placed...

Maybe if I mention more about what I'm doing you'd be able to offer me a solution I'm not even close to.

Basically - we've got the CSE Mod: The medical system in there has a whole bunch of new items that we need to carry around. These are things like bandages, morphine, etc...

All of these items replace the default FAKs that come with Arma - sp the FAK is now deprecated for us.

The good thing about FAKs is that once you killed some enemies - you went to their corpses and could take their FAKs and replenish your own. The problem with this mod, is that the loadouts for all the AI, and the vehicles with stuff in the inventory do not (of course) have any of the newly required medical goods. Ad don top of that - we have another mod that adds more custom units and factions - and of course, those have no notion of our medical system requirements either.

Therefore - I thought to myself that I would need to add these items to all classes default loadouts so that the mod feels truly integrated. When I do any search for "customize loadout" all I get is a bunch of scripts to add to the init line of units placed in the editor. But in MCC and or Zeus - units are not placed in the editor - they are placed dynamically during the mission.

So then I thought - like in any modern application - I could just add an event to some event handler when a unit is spawned - then do a dynamic check for a type eg: if(typeOf _unit == "B_Soldier_F") { doThis; } else {doThat;}

And that's what lead me to asking the question...

Thanks for your assistance.

Share this post


Link to post
Share on other sites
Yeah... I can see how that would be very taxing - considering I'm looping through everything placed each time something is placed...

Maybe if I mention more about what I'm doing you'd be able to offer me a solution I'm not even close to.

Basically - we've got the CSE Mod: The medical system in there has a whole bunch of new items that we need to carry around. These are things like bandages, morphine, etc...

All of these items replace the default FAKs that come with Arma - sp the FAK is now deprecated for us.

The good thing about FAKs is that once you killed some enemies - you went to their corpses and could take their FAKs and replenish your own. The problem with this mod, is that the loadouts for all the AI, and the vehicles with stuff in the inventory do not (of course) have any of the newly required medical goods. Ad don top of that - we have another mod that adds more custom units and factions - and of course, those have no notion of our medical system requirements either.

Therefore - I thought to myself that I would need to add these items to all classes default loadouts so that the mod feels truly integrated. When I do any search for "customize loadout" all I get is a bunch of scripts to add to the init line of units placed in the editor. But in MCC and or Zeus - units are not placed in the editor - they are placed dynamically during the mission.

So then I thought - like in any modern application - I could just add an event to some event handler when a unit is spawned - then do a dynamic check for a type eg: if(typeOf _unit == "B_Soldier_F") { doThis; } else {doThat;}

And that's what lead me to asking the question...

Thanks for your assistance.

You can use the Zeus eventhandler CuratorGroupPlaced. It fires when a new group is placed by Zeus.

Use units command to get array of group's units.

Then in the eventhandler execute your script passing the array of units for that group to it.

In your script use foreach command to loop through your units.

Use a switch statement to equip your units based on their classname with the 'default' block as your simple infantry loadout for unspecified classnames. Maybe just executing a loadout script for that unit.

_group = _this;  //spawned group passed to script

{
//determine side of unit
switch (side _x) do
{
	case west:
	{
		//determine classname of unit
		switch (toLower(typeOf _x)) do
		{
			default { _handle = [_x] execVM 'loadout.sqf'; waitUntil{scriptDone _handle}; };  //unspecified classname
			case "b_soldier_at": { _handle = [_x] execVM 'loadout.sqf'; waitUntil{scriptDone _handle}; };
			case "b_soldier_aa": { _handle = [_x] execVM 'loadout.sqf'; waitUntil{scriptDone _handle}; };
		};
	};
	case east:
	{

	};
	case resistance:
	{

	};
	default  //civilian
	{

	};
};
} forEach (units _group);

Share this post


Link to post
Share on other sites

Its a shame that the Init EH only works for the object its self so can only really be used by being added to the the objects config. Would be nice if they introduced a Init/Spawned/Created EH for addMissionEventHandler to catch things being spawned in, then this sort of thing would be possible without having to do crazy loops through everything. Wouldnt of thought it would be to hard to implement engine side.

Maybe something a request ticket could be opened for. Would be immensely useful for the community, people would not have to request changes to spawning scripts as they would be able to catch the new objects themselves.

Share this post


Link to post
Share on other sites

Hi, i know this post is a bit old now but if it is useful this is how I got around the issue using ALiVE and adding custom loadouts to all spawned units

in description.ext

class Extended_Init_EventHandlers {
class Man {
 init = "_this call (compile preprocessFileLineNumbers 'basicloadout2.sqf')";
};
};

and a sample of my loadout script

private "_unit";
_unit = _this select 0;

if ((side _unit == west) and (!isPlayer _unit)) then {
removeGoggles _unit;
removeBackpack _unit;
removeHeadgear _unit;
removeVest _unit;
removeUniform _unit;
removeAllWeapons _unit;
removeAllAssignedItems _unit;

 	switch (true) do{

	case (_unit isKindOf "rhsusf_army_ucp_medic") :
	{

		_unit forceAddUniform "TRYK_U_B_NATO_UCP_CombatUniform";
		for "_i" from 1 to 5 do {_unit addItemToUniform "FirstAidKit";};
		_unit addVest "TRYK_V_ArmorVest_tan";
		for "_i" from 1 to 2 do {_unit addItemToVest "SmokeShell";};
		for "_i" from 1 to 2 do {_unit addItemToVest "HandGrenade";};
		for "_i" from 1 to 7 do {_unit addItemToVest "rhs_mag_30Rnd_556x45_Mk318_Stanag";};
		for "_i" from 1 to 3 do {_unit addItemToVest "RH_15Rnd_9x19_SIG";};
		_unit addHeadgear "rhsusf_ach_helmet_headset_ucp";
		_unit addGoggles "TRYK_US_ESS_Glasses";

		comment "Add weapons";
		_unit addWeapon "rhs_weap_m16a4_carryhandle";
		_unit addPrimaryWeaponItem "RH_eotexps3";
		_unit addWeapon "RH_p226";
		_unit addWeapon "Binocular";

		comment "Add items";
		_unit linkItem "ItemMap";
		_unit linkItem "ItemCompass";
		_unit linkItem "ItemWatch";
		_unit linkItem "ItemRadio";
		_unit linkItem "ItemGPS";
		_unit linkItem "NVGoggles_OPFOR";
           	};

	case (_unit isKindOf "rhsusf_army_ucp_teamleader") :
	{

		comment "Add containers";
		_unit forceAddUniform "TRYK_U_B_NATO_UCP_CombatUniform";
		for "_i" from 1 to 2 do {_unit addItemToUniform "RH_15Rnd_9x19_SIG";};
		_unit addVest "TRYK_V_ArmorVest_tan";
		_unit addItemToVest "SmokeShell";
		_unit addItemToVest "HandGrenade";
		for "_i" from 1 to 7 do {_unit addItemToVest "rhs_mag_30Rnd_556x45_Mk318_Stanag";};
		for "_i" from 1 to 5 do {_unit addItemToVest "1Rnd_HE_Grenade_shell";};
		_unit addHeadgear "rhsusf_ach_helmet_ucp";
		_unit addGoggles "G_Lowprofile";

		comment "Add weapons";
		_unit addWeapon "rhs_weap_m4a1_carryhandle_grip2";
		_unit addPrimaryWeaponItem "rhsusf_acc_eotech_552";
		_unit addWeapon "RH_p226";
		_unit addWeapon "Binocular";

		comment "Add items";
		_unit linkItem "ItemMap";
		_unit linkItem "ItemCompass";
		_unit linkItem "ItemWatch";
		_unit linkItem "ItemRadio";
		_unit linkItem "ItemGPS";
		_unit linkItem "NVGoggles_OPFOR";
	};

and I just did this for each unit classname of the side I wanted to change. works well.

  • 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
Sign in to follow this  

×