Jump to content
Sign in to follow this  
kyfohatl

How to respawn a group when it has no members left? (SINGLEPLAYER)

Recommended Posts

Ok I have about 30 AI groups on the map (SINGLEPLAYER MISSION) and when one group dies (i.e. it has no mambers left) I want to respawn the exact same group (consisting of the same units) after a specific amount of time back at the base.

I was thinking of using the "killed" event handler and attaching it to every unit on the map. Once they die, I could run a script using the handler to find their group and check how many members it has left. So if there's no members left, the group is respawned. But that didn't work because once a unit dies, he joins grpNull, so I couldn't figure out which group he belonged to. Furthermore I need to know the configs/type of every unit in the group as well as the number of members, so that I can respawn the same group.

Any ideas/suggestions?

oh, and I also managed to find this thread that may help, though it uses "addAction":

http://forums.bistudio.com/showthread.php?t=113962&highlight=group+members

Thanks

Share this post


Link to post
Share on other sites

I'd save all infos about a group & its members when you set up the group or, if its a group placed in the editor, right after the mission starts.

No need for killed event handlers on every unit, just loop over all groups you have saved in a global array and once 1 group has 0 members, get the 'old' info of its members from where you saved it and respawn all units with that info.

Edited by ])rStrangelove

Share this post


Link to post
Share on other sites

Yeah saving information about the group isn't too difficult as I'm creating all of the groups using a scripts. Anyway I'm thinking of taking up your suggestion. That way I won't be able to have a standard spawning time; that is respawn a group exactly say 2 minutes after it has lost all members, but that shouldn't matter too much.

I'm trying to make a BattleField (or COD) style mission and in BattleField/COD games having a standard respawn time is pretty necessary. But in ArmA it takes ages to get to the enemy and it is generally a much slowr paste game, so I think I can get away with taking up your suggestion.

Thanks for the help

Share this post


Link to post
Share on other sites

Not sure if a group without members will still be accessible after 2 minutes. Better check that before you take it for granted. :)

Share this post


Link to post
Share on other sites
Ok I have about 30 AI groups on the map (SINGLEPLAYER MISSION) and when one group dies (i.e. it has no mambers left) I want to respawn the exact same group (consisting of the same units) after a specific amount of time back at the base.

I was thinking of using the "killed" event handler and attaching it to every unit on the map. Once they die, I could run a script using the handler to find their group and check how many members it has left. So if there's no members left, the group is respawned. But that didn't work because once a unit dies, he joins grpNull, so I couldn't figure out which group he belonged to. Furthermore I need to know the configs/type of every unit in the group as well as the number of members, so that I can respawn the same group.

Any ideas/suggestions?

oh, and I also managed to find this thread that may help, though it uses "addAction":

http://forums.bistudio.com/showthread.php?t=113962&highlight=group+members

Thanks

its very easy if you spawn them all by script. put this in trigger radio alpha. this = execVM "contractors1.sqf"; in contractors1.sqf is all of this code...

leader contractors1 sideChat "LP team killed in action";

deleteVehicle c1;

deleteVehicle c2;

deleteVehicle c3;

deleteVehicle c4;

deleteVehicle c5;

deleteVehicle contractorveh1;

PAPABEAR=[West,"HQ"];

sleep 4;

contractors1 = CreateGroup West;

c1 = contractors1 createUnit ["MERCENARY_DEFAULT18", [(getpos blueforspawn) select 0,(getpos blueforspawn) select 1,0], [], 10, "FORM"];

c2 = contractors1 createUnit ["MERCENARY_DEFAULT19A", [(getpos blueforspawn) select 0,(getpos blueforspawn) select 1,0], [], 10, "FORM"];

c3 = contractors1 createUnit ["MERCENARY_DEFAULT19A", [(getpos blueforspawn) select 0,(getpos blueforspawn) select 1,0], [], 10, "FORM"];

c4 = contractors1 createUnit ["FOX_US_SFMRW_OFFD", [(getpos blueforspawn) select 0,(getpos blueforspawn) select 1,0], [], 10, "FORM"];

c5 = contractors1 createUnit ["MERCENARY_DEFAULT26", [(getpos blueforspawn) select 0,(getpos blueforspawn) select 1,0], [], 10, "FORM"];

c1 = leader contractors1;

contractorveh1 = createVehicle ["SUV_PMC", position blueforspawn, [], 3, ""];

contractors1 addVehicle contractorveh1;

{_x setSkill 1} foreach units contractors1;

c5 assignAsDriver contractorveh1;

con1 = createTrigger ["EmptyDetector", [0,0,0]];

con1 setTriggerArea [0, 0, 0, false];

con1 setTriggerActivation ["NONE", "present", false];

con1 setTriggerTimeout [20, 20, 20, false ];

con1 setTriggerStatements ["!(alive c1) && !(alive c2) && !(alive c3) && !(alive c4) && !(alive c5)", "player exec ""contractors1.sqf""", ""];

con1_1 = createTrigger ["EmptyDetector", [0,0,0]];

con1_1 setTriggerArea [500, 500, 0, false];

con1_1 setTriggerActivation ["EAST", "WEST D", true];

con1_1 setTriggerStatements ["this", "player exec ""contractors1\stress.sqf""", ""];

con1_1 attachto [contractorveh1,[0,0,0],""];

as you yo see each unit is named c1,c2,c3,c4,c5 and the trigger says that if they die then re run the sript which spawns them again. if u get confused let me know

Share this post


Link to post
Share on other sites

@kyfohatl

Try this mate.....it might be just what you need.

I have removed all the extra code I need for my missions and trimmed this down for you. It is well commented so should be easy to follow if you want to change anything and hack it to death.

I created a demo mission that can be found here at Mediafire.

twirl_respwnGrp.sqf:-

[color="SeaGreen"]/*
Respawn a group infinitely or a set number of times***

Author: Twirly.
Date: February 2011

Execute from ONE member of a groups init like this:-
nul = [group this,999,[10,30],_waypt] execVM "twirl_respwnGrp.sqf";

Execute from a script like this:-
nul = [_group,999,[10,30],_waypt] execVM "twirl_respwnGrp.sqf";

Where:-
_group  = the group to respawn...
999      = the number of respawns... use -1 for perpetual respawn
[10,30] = the minimum and maximum time between respawns in seconds
_waypt = the groups waypoint...can be an object or a position

*/[/color]

private ["_group","_waypt","_nresp","_minmaxt","_mintim","_maxtim","_i","_j","_dude","_alive",
	 "_dead","_typs","_spawnpos","_weap","_mags","_prim","_wp"];

_group = 	_this select 0;
_nresp = 	_this select 1;
_minmaxt = 	_this select 2;	//array of min/max tim before respawn. Eg...[30,180]
_waypt = 	_this select 3;

//if an object was passed...get the position of the object
if (typename _waypt == "OBJECT") then {_waypt = getpos _waypt};

//get the spawn position of the group
_spawnpos = getPos leader _group;

//get the number of units in the group
_numunits = count units _group;

//get the minimum and maximum times for respawn
_mintim = _minmaxt select 0;
_maxtim = _minmaxt select 1;

//initialise the necessary arrays
_typs = [];
_weap = [];
_mags = [];
_prim = [];

//save unit types and weapons...mags etc.
for [{_i=0},{_i<count units _group},{_i=_i+1}] do {

//select the unit
_dude = units _group select _i;

//save the typeof unit and the weapons into separate arrays
_typs = _typs + [typeOf _dude];
_weap = _weap + [weapons _dude];
_mags = _mags + [magazines _dude];
_prim = _prim + [primaryWeapon _dude];

//love my sleep :)
sleep 0.01;

};

//main loop....keep looping as long as _nresp is not equal to zero
while {_nresp !=0} do {

//not sure if this is necessary
deleteWaypoint [_group,0];

//add the waypoint
_wp = _group addWaypoint [_waypt, 10];
[_group, 1] setWaypointSpeed "NORMAL";
[_group, 1] setWaypointBehaviour "AWARE";
[_group, 1] setWaypointType "MOVE";
[_group, 1] setWaypointCombatMode "RED";

//no running away
_group allowfleeing 0;

//works better if you track the dead units separately
_dead = [];

//main loop
while {(count units _group >= 1)} do {

	for [{_i=0},{_i<count units _group},{_i=_i+1}] do {

		//select unit _i
		_dude = units _group select _i;

		//make sure the unit can always walk
		if (alive _dude and not canStand _dude) then {
			_dude setDammage ((getDammage _dude) - 0.1);
		};

		//if unit is dead add to the dead array
		if (not(alive _dude)) then {
			if (not(_dude in _dead)) then {
				_dead = _dead + [_dude];
			};
		};
		sleep 0.01;
	};
	sleep 1;
};

//hide the bodies...then delete
for [{_i=0},{_i<_numunits},{_i=_i+1}] do {
	_dude = _dead select _i;
	hideBody _dude;
	sleep 4;
	deleteVehicle _dude;
	sleep 0.01;
};

//wait for a random time between mintim and maxtim before we respawn the group
_wait = time + _mintim + (random _maxtim);
waitUntil {(time > _wait)};

//respawn the group restore weapons, mags etc
for [{_i=0},{_i<_numunits},{_i=_i+1}] do {
	_dude = _typs select _i createUnit [_spawnpos, _group, ""];
	removeAllWeapons _dude;
	{_dude addMagazine _x} forEach (_mags select _i);
	{_dude addWeapon _x} forEach (_weap select _i);
	_dude selectweapon (_prim select _i);
	sleep 0.1;
};

//update the number of respawns
_nresp =_nresp - 1;

//hint...side, group and respawns left
_respleft = _nresp;
if (_nresp <0) then {_respleft = "Unlimited"};

hint format ["%1:%2 has respawned\n%3 respawns remaining", side _group, _group, _respleft];

};

Edited by twirly
Clarity

Share this post


Link to post
Share on other sites

@twirly:

Thank you very much. I think that is very close to what I need. Just one question: Would this method use up too much processing power if I'm running 30+ AI groups on the map? (since it has to be run seperately for every group). By the way all of the comments made the script really easy to understand :) (I'm pretty much a begginer at scripting). I should start writing comments in my scripts too.

@quincy:

Interesting method. So basically with your way, I need to have a seperate script for every group, and every time the group "dies" it re-executes itself to recreate the same group via a trigger? If I'm correct, then wouldn't you need some sort of a "deleteVehicle" command at the end of the script to delete the trigger that was made in the script's previous run? Anyway thanks alot for the help :)

Share this post


Link to post
Share on other sites
@twirly:

....Would this method use up too much processing power if I'm running 30+ AI groups on the map?

I don't know man....try it and see. I run lots of scripts including this one on my groups, vehicles etc...without any noticeable hit.

This processor is an AMD 3.0 Gig Quad Core....so nothing too fancy.

I think it will go OK for you....download the demo mission and copy and paste groups till you have 30+ and see what happens!!

Share this post


Link to post
Share on other sites

Great thread, looked for this for ages.

Also thanks for the great replies.

Share this post


Link to post
Share on other sites

Yeah I would also like to thank the repliers again.

@twirly: Yeah, I got FPS hit, but it was becuase of having 35 AI gorups on the map. Adding a looping script to every group doesn't seem to increase the FPS hit.

Share this post


Link to post
Share on other sites
I don't know man....try it and see. I run lots of scripts including this one on my groups, vehicles etc...without any noticeable hit.

This processor is an AMD 3.0 Gig Quad Core....so nothing too fancy.

I think it will go OK for you....download the demo mission and copy and paste groups till you have 30+ and see what happens!!

Hello Twirly!

Again i just want to say thanks for writing this script (life saver). Now you mentioned here that you use this to respawn vehicals however ive been studying your script (to try and learn, but tbh im failing. Way over my head) and i cant see anything about vehicals.

Obviosly the best way to find out would be to quickly test this but im at work.

Thanks

Steve

Share this post


Link to post
Share on other sites

I use a separate script for vehicles. Try Tophe's script here for the vehicles. http://forums.bistudio.com/showthread.php?t=76445&highlight=tophe+vehicle+respawn

My scripts are purpose built and I will have to find one that would suit and delete all the stuff that would make it NOT work for you and then test. That will take more time than I have right now mate. Try Tophe's script above and if you need something different I'll see what I can do.

Edited by twirly

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  

×