Which way is the best to spawn enemies

Started by
7 comments, last by G-Dot 5 years ago

Hello everyone! I was working on my spawn algorithm for enemies and come up with the question. Wchich way is the best to actually spawn them in Unreal Engine 4? I want to describe my situation. In my game there are a lot of moments when enemies spawn constantly, for example there are always more then five enemies (aka AI Actors) on the level. To keep them always on map I got an algorithm but I don't know how I should do it properly. There are two ways that I found out:

  1. Just a normal lifecycle: enemy spawns, he dies from player and actor and controller get destroyed.
  2. A little bit tricky. I spawn a good amount of enemies at the begging of level at one hided place (for example 20 enemies under the map). Thaen I spawn some amount of them and after death instead of being destroyed Actor returns to his initial place and wait till his next spawn.

I don't know which way is better from technical point that's why I can't decide which one to use. But my algorithm can handle both of them.

Advertisement

I like going with one, because then you wouldn't have unused resources (the enemies sitting around under the map), and spawn new enemies when needed.

That is unless there is a reason to not kill and move the actors.

Your second example is an object pool. If there are lots of the same enemy in the level that die and "spawn" frequently, this is an optimal choice. In cases where any object gets spawned and destroyed frequently, such as bullets of a weapon, an object pool is certainly a valid choice.

EDIT: If you haven't yet, check out Reset(). It will simplify the respawning functionality.

9 hours ago, conditi0n said:

Your second example is an object pool. If there are lots of the same enemy in the level that die and "spawn" frequently, this is an optimal choice. In cases where any object gets spawned and destroyed frequently, such as bullets of a weapon, an object pool is certainly a valid choice.

Yeah, I think this is my choice, because gameplay is kind of divide into two parts: one of them is an action, where enemies spawn each 40-50 seconds. But I got 7 types of enemy in my games and some of them (weak ones) spawn really frequent and in big amounts, but more strong enemies spawn with slower rate. Also I didn't understand a  trick with bullets. Where are I should stole them if they don't use. And yeah, I got different types of bullets for each weapon in my game, but they all inherit from main class.

40-50 interval seconds is almost never, think around 500 / second or more as "frequently".

 

2 hours ago, Alberth said:

40-50 interval seconds is almost never, think around 500 / second or more as "frequently".

 

But In this 40-50 seconds interval I will spawn not one single enemy but f good amount of them. And I think it's impossible from gameplay point if view to spawn 500 enenmies per second.

Yes, my main point was mostly that what you see as "often" may not be so often from a computer point of view, running at a few GHz.

Before considering more complicated schemes like pre-construction or object pooling, I'd first try the simplest approach. If that works, you're done. If it doesn't work, measure where your performance problem is, and fix it.

In about 9999 of the 10000 times, our guess of where a problem is, is just dead wrong. Avoid wasting time on solving problems that don't even exist.

 

4 hours ago, Alberth said:

Yes, my main point was mostly that what you see as "often" may not be so often from a computer point of view, running at a few GHz.

Before considering more complicated schemes like pre-construction or object pooling, I'd first try the simplest approach. If that works, you're done. If it doesn't work, measure where your performance problem is, and fix it.

In about 9999 of the 10000 times, our guess of where a problem is, is just dead wrong. Avoid wasting time on solving problems that don't even exist.

 

Yeah, I got you point. It's very wisdom advice and I should give a try to my new algorithm. Rigth now I spawn enemies without any pool and every time they die it means that their objects get destroyed. But it's very stupid and straigth forward: i just spawn predetermint groups of enemies. My new algorithm tries to normalize spawn amount by max enemy count and max enenmy weigth. But it's not fully integrated and I hope it will work better then current one.

This topic is closed to new replies.

Advertisement