0
\$\begingroup\$

I know this may have been asked before but I would like an updated answer. So how do I respawn enemies in Gamemaker Studio 2?

Say I have an object called objBullet and an enemy objenemy; when they collide with each other I want the bullet to destroy the enemy, and the enemy to respawn after a few seconds. As a bonus, if you could tell me how to respawn the enemy in a new random spot that'd be awesome.

\$\endgroup\$
2
\$\begingroup\$

Since your enemies are destroyed and don't respawn instantly, you need a different object to spawn them using a timer.

I implemented this minimal working example, in which every time an enemy is destroyed it notifies an object objSpawnManager, that will choose a random spot and create a new enemy there.

Spawn locations

Every spawn point is identified by an instance of objSpawn, and the only thing it does is, well... exist. We place as many spawn as we want, because we want to recover their x and y position when we'll need them later. Spawn locations are stored in a List data structure in objSpawnManager.

Enemy behaviour

When an enemy is hit (I simulated hit with a simple mouse click for simplicity) it destroys itself, then increments the value of variable queue of the objSpawnManager. This variable tells the objSpawnManager how many enemies are waiting for respawn yet.

Spawn manager

The object objSpawnManager is in charge of the spawning system. Its variable spawnTime is the number of seconds for spawning a new enemy; spawnPoints is a DS List containing all ids of objSpawn instances.

This is how it works:

  • Step Event: the manager checks if there are enemies waiting for respawn (queue>0); if so, it starts the alarm countdown if it isn't counting down already (!(alarm[0]>=0), "alarm not working").

  • Alarm 0 Event: get the id of a random spawn point from the DS List, then create a new enemy at such spawn location, finally decrease the value of queue.

And that's it. The rest of the code just sets up some variables, and takes care of freeing memory when objects aren't needed anymore.

\$\endgroup\$
  • \$\begingroup\$ thanks its just what I was trying to do love the example too it really helped me. \$\endgroup\$ – sketcherskt May 10 '18 at 22:05

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Not the answer you're looking for? Browse other questions tagged or ask your own question.