Jump to content
Sign in to follow this  
nomadd

Best way to detect spawned unit

Recommended Posts

Question

What is the best way to detect spawned units in a mission and have a script run on them after they spawn. I know i can addeventhandler in the spawn script or setvehicleinit (which I have read is not the best way). Is there a way to run a script and detect any newly spawned units. Then run a script on them.

Nomadd

Share this post


Link to post
Share on other sites

Just update a global array when you spawn them.

Share this post


Link to post
Share on other sites

How are you spawning them? Do you get back the handle/name of the spawned group when they are spawned?

Share this post


Link to post
Share on other sites

Here is a snippet of the spawn script I am using.

private ["_Locations","_pos","_units","_unit","_randomunits","_grp","_wp"];
_Locations = [sp1,sp2];  // game logics
_pos = _Locations select floor(random(count _Locations));

_units =  ["uns_nva5sni","uns_nva5sni","uns_nva5sni","uns_nva5sni","uns_nva8b","uns_nva8a","uns_nva3mg","uns_nva2med","uns_nva4rpg","uns_nva8g","uns_nva7rto","uns_nva8d","uns_nva8f","uns_nva3amg","uns_nva8h","uns_nva4arpg","uns_nva6sap","uns_nva5asni"
];
_unit = count _units;
_randomunits =
[
_units select random _unit,_units select random _unit,_units select random _unit,_units select random _unit,_units select random _unit,_units select random _unit,_units select random _unit,_units select random _unit,_units select random _unit,_units select random _unit,_units select random _unit,_units select random _unit,_units select random _unit,_units select random _unit
];


_grp = [getpos _pos, east,_randomunits ,[],[],[],[],[4 + random 4,0]] call BIS_fnc_spawnGroup;
_grp allowFleeing 0;
_wp = _grp addWaypoint [getPos attackmrk, 1];
_wp setWaypointSpeed "FULL";
_wp setWaypointType "SAD"; 
_wp setWaypointFormation "LINE";
_wp setWaypointCombatMode "RED";
{   _x setskill ["aimingAccuracy",0.30];
   _x setskill ["spotDistance",0.50];
   _x setskill ["spotTime",0.50];
   _x setskill ["courage",1]; 
   _x setskill ["commanding",1]; 
   _x setskill ["aimingShake",0.30];
   _x setskill ["aimingSpeed",0.50];
   _x setunitpos "up";
} forEach units _grp;

I am adjusting the skill of the units as they spawn. Instead of doing this in the spawn script , is there a way to have the server run a script on all units/vehicle including any that spawn.

I hope this make sense. I am not sure the best way to ask this question my scripting knowledge is limited.

Share this post


Link to post
Share on other sites

Yes....you can use allUnits to get a list of all units at any time. But would have to make sure it ran after all units were present or it might miss some.

I think there's nothing wrong with doing it the way you are already doing it. At least you are sure that the spawned units have whatever applied to them.

Maybe you can run a separate script on the group (_grp) as it is spawned.... but it is six of one.... half dozen of the other.

Share this post


Link to post
Share on other sites
I am adjusting the skill of the units as they spawn. Instead of doing this in the spawn script , is there a way to have the server run a script on all units/vehicle including any that spawn.

well, the most reliable way is the way you already do, if it is for cleanlyness of the spawn script you could save the setSkill as a seperate script or a function and run the setskill via a

_grp execVM "skillScript.sqf";

or a

_grp call skills;

but for your question:

place this in your spawn script at top:

if (isNil "newlySpawnedUnits") then {
// will create the array and start the loop if not started.
newlySpawnedUnits = [];
[] spawn {
	while {true} do {
		{
			_grp = _x;
			{
				_x setskill ["aimingAccuracy",0.30];
				_x setskill ["spotDistance",0.50];
				_x setskill ["spotTime",0.50];
				_x setskill ["courage",1];
				_x setskill ["commanding",1];
				_x setskill ["aimingShake",0.30];
				_x setskill ["aimingSpeed",0.50];
				_x setunitpos "up";
			} forEach units _grp;
			newlySpawnedUnits = newlySpawnedUnits - [_grp];
		} foreach newlySpawnedUnits;
		waitUntil {sleep 10; count newlySpawnedUnits > 0};
	};
};
};

will check for new units every 10 seconds, then alter their skills, and when done remove them from the array so they wont get double setskill

now when you spawn units run this line after:

newlySpawnedUnits = newlySpawnedUnits + [_grp];

if you spawn units on clientsides, use publicVariable to broadcast the array whenever the array is changed.

publicVariable "newlySpawnedUnits";

this way is not recomended at all, but thats one way to do it, my personal reference, do it directly in the spawn script as function, code or seperate script.

Edited by Demonized

Share this post


Link to post
Share on other sites

Thanks for the responses/info. I will continue with the way I am doing it. I will take a closer look at the code Demonized posted ,so I can better understand it.

Nomadd

Share this post


Link to post
Share on other sites

now when you spawn units run this line after:

newlySpawnedUnits = newlySpawnedUnits + [_grp];

 

 

Just curious, what will this line do as per above example:

newlySpawnedUnits = newlySpawnedUnits + [_grp];

Does it club the 2 jobs of reading the detect new spawned units script at the top and the actual spawn group line?

 

And like he said, this will need to be put after spawning like so?:

...
_grp = [getpos _pos, east,_randomunits ,[],[],[],[],[4 + random 4,0]] call BIS_fnc_spawnGroup;
newlySpawnedUnits = newlySpawnedUnits + [_grp];
...

Then I am assuming _grp execVM "skillScript.sqf"; will be of no use as we have already defined the skills in the newlySpawnedUnits = []; array at the top?

 

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

×