Making an entity spawn every so often?

Posted By: Bloodtake23

Making an entity spawn every so often? - 04/17/12 22:52

Hello,

I want to have a spawn system for ammo packs that will drop from the sky every 2 minutes. I also want to have an Enemy spawn every 10 seconds. Is there a bit of code for a action that I can assign to a model in WED?

Thanks in advance!
Posted By: Bloodtake23

Re: Making an entity spawn every so often? - 04/18/12 00:13

It also needs to be in a specific area! laugh
Posted By: Carlos3DGS

Re: Making an entity spawn every so often? - 04/18/12 01:43

declare a global entity pointer for each object you want to use this like so:

Code:
ENTITY* Ammo_Pickup1;




then create an action like this:

Code:
action Ammo_Pickup_Action()
{
while(condition for being alive)
{
//code to execute while alive (like spinning, moving, detecting when it has been picked up,etc)
wait(1);
}

ent_remove(me); //destroy when its condition to exsist is not met

wait(-10); //to wait 10 seconds after it has been destroyed for example

Ammo_Pickup1=ent_create(ammo.mdl,position,Ammo_Pickup_Action); //create it again re-using the global pointer 10 seconds after it has been removed
}



Then all that is left to do is create your first one like this, and it will automatically work and when it finished destroys itself, waits 10 seconds, and re-creates another copy of itself:

Code:
Ammo_Pickup1=ent_create(ammo.mdl,position,Ammo_Pickup_Action);


Posted By: Bloodtake23

Re: Making an entity spawn every so often? - 04/18/12 17:25

For the position bit what should it look like? Also can I use my or another term to just do it where I put it in my wmb file?
Posted By: Wjbender

Re: Making an entity spawn every so often? - 04/18/12 19:04

simply create a dummy mdl and place them where you want to in
wed and assign a action(code) to that dummy mdl ..

then run the function he gave you from your action

function Ammo_Pickup()
{
//the code he gave you
}

//the position gan then be retrieved from me/you pointers
//you would know wich one i suppose

action whatever()
{
ammo_pickup();
}

Posted By: Carlos3DGS

Re: Making an entity spawn every so often? - 04/19/12 00:34

Code:
action Ammo_Pickup_Action()
{

   VECTOR create_pos; //vector for storing starting position
   vec_set(create_pos.x,my.x); //save initial position

   while(condition for being alive)
   {

      //code to execute while alive (like spinning, moving, detecting when it has been picked up,etc)

      //condition check for while loop to end when the object should be destroyed

      wait(1);
   }

   ent_remove(me); //destroy when its condition to exsist is not met

   wait(-10); //to wait 10 seconds after it has been destroyed for example

   ent_create("ammo.mdl",create_pos,Ammo_Pickup_Action); //create it again re-using the local vector's stored position 10 seconds after it has been removed
}


There you go, I added two lines at the begining of the function to create a local vector and set it to store the entitie's position on creation.
Also modified the new entity creation line at the end to use that local vector.

Assuming you are using simple scripts, I removed the need for a global entity pointer. If you are placing in wed, all you need to do is place the object and apply this action, nothing more required (besides filling in the while condition with what you want the entity to do and something to stop the while loop when it should be destroyed).
© 2024 lite-C Forums