Page 1 of 1

Respawning AI

Posted: Sun Dec 23, 2007 2:15 pm
by Bromi
Hi all im new to the forums and new to modding,

i have created a SP map where the allies all start in trucks and get transported to a forest where the main battle is this is all working fine, i have looked all over and found some examples of how to spawn an enemy and make it respawn however i have got none to work i just want a line of code that says "if $targetname is killed respawn at $targetname2.origin for X number of times"

many thanks in advance

Posted: Mon Dec 24, 2007 2:03 pm
by bdbodger
Well taking some lines from a map I did you can make your ai into spawners like this to start

Code: Select all


local.aispawntime = getcvar (sc_aispawntime)

if !(local.aispawntime)
setcvar sc_aispawntime 15

	level.snowguy = exec global/makearray.scr $snowguy

	for (local.i=1;local.i <= level.snowguy.size;local.i++)
	level.snowguy[local.i] = waitthread global/spawner.scr::spawner_create level.snowguy[local.i]
That creates spawners from all ai that have the targetname $snowguy . Then I did a loop so that there was only 1 of each ai at a time and that when they died they would resawn . So if I had 5 ai then the max number of ai at a time would be 5 . In this map I also used cvars to be able to turn the ai on and off if I wanted . After turning the ai into spawners it was time to spawn them for the first time . In this bit level.snowdude is an ai created from level.snowguy who is our spawner I can spawn level.snowguy as many times as I need but in this script I only do it one at a time .

Code: Select all

if((getcvar sc_noai) != "1")
{

	for (local.i=1;local.i <= level.snowguy.size;local.i++)
	level.snowdude[local.i] = waitthread global/spawner.scr::spawner_activate level.snowguy[local.i]

	thread respawn_ai
}

After I spawned the ai for the first time I then run the respawn_ai thread that ckecks if the ai are still alive and if not respawn a new one .

Code: Select all

respawn_ai:

while(1)
{
	if((getcvar sc_noai) != "1")
	{
		for(local.i=1;local.i <= level.snowguy.size ;local.i++)
		{
			if!(isalive level.snowdude[local.i])
			{
			wait (getcvar sc_aispawntime)
			level.snowdude[local.i] = waitthread global/spawner.scr::spawner_activate level.snowguy[local.i]
			}
		}
		if (level.aigone == 1)
		level.aigone = 0
	}
	if(((getcvar sc_noai) == "1")&&(local.aigone !=1))
	{
		for(local.i=1;local.i<= level.snowdude.size;local.i++)
		{

		if(level.snowdude[local.i])
		level.snowdude[local.i] remove

		level.aigone = 1
		}
	}

waitframe
}
end
When you make ai into spawners they have the same settings as they did when you made them so I had set the ai to have type_idle runner and had created paths for them to follow connected so the last pathnode in the path targeted the first one so they just kept running the path over and over when they wern't trying to kill you that is . You can do the same with patrol paths too . If you only want them to be able to respawn a certain number of times you can give them a key/value to keep track of how many times they have spawned and use that .

Code: Select all

if (level.snowguy[local.i].NumSpawns == NIL)
level.snowguy[local.i].NumSpawns = 0

if!(isalive level.snowdude[local.i]) && level.snowguy[local.i].NumSpawns < 5)
{
 level.snowguy[local.i].NumSpawns++

etc etc spawn the guy

}

Posted: Tue Dec 25, 2007 8:01 pm
by Bromi
Thankyou Very Much this is excellent!!!!!!!!!!!!!

is there any good references on Cvars Anywhere i woul likr to learn more about them and what they are usefull for

Posted: Wed Dec 26, 2007 8:25 am
by bdbodger
If you are talking about ones you make yourself like

if((getcvar sc_noai) != "1")

then they can be anything you want to use . A cvar is a console variable and keeps it's value between maps so if sc_noai was set to 1 it stays at 1 untill you change it with rcon if it is a remote server . That way if you can rcon the server you can turn the ai off if you want or the players on the server want you to . If you type cvarlist into the console you can get a list of what your cvars are set at including the ones the game has set . To make your own cvar you can use setcvar and if that cvar does not exist it will be created . It can be used to set the number of ai in the map if your script is setup that way to read the cvar . A script that uses stufftext to put text in the players console can also set cvars on the players machine . $player[local.i] sufftext "set mycvar 1" . Why would you want to do that well for one reason certain menus use cvars to set the value of some of the things on the menu . I did a mod that used that for a custom map and menu . I set a cvar on the players machine then poped up a menu that showed the cvars value . This can be abused and some players won't like you changing any of thier settings .