Hello,
I have been getting to know the Unity engine and I wanted feedback on a question I have been pondering.
Currently I have an AI script for some robots who die and come back to respawn areas after a certain time.
At the moment I have the script keep a constant list of the respawn locations to reference when needed.
Whats more optimal? Keep the list at all times or only retrieving the list (going through finding all spawn locations) right before the respawn function needs to happen?
Thanks for your time and feedback
I wouldn’t worry too much about it. It’s unlikely this will make a noticeable difference for you either way. (Unless you have millions of spawn points, or respawn robots many times per second, or something like that.)
If it were me, I’d probably keep the list, but mostly for organizational reasons rather than performance. You generally want to avoid doing any difficult or complex optimizations until you’ve profiled your game and measured its unique performance bottlenecks.
Insofar as it makes any difference, keeping a memorized list of something is almost always going to be faster, but it also locks up some memory. (Recalculating the list also uses that memory, but only temporarily, so other parts of your game could use it at other times.) You’ll discover that many optimization problems in computer programming have a time vs. memory trade-off.
Also, if you were going to seriously try to optimize this, you haven’t provided enough detail. In particular, it’s important to ask exactly what you mean by “going through finding all spawn locations”. My guess is that you have some game objects representing the spawn locations, which means technically you are already storing a list of spawn locations (implicitly, in the game’s object hierarchy), and that method of storing them is probably much less efficient than a List or something like that. If those objects are only used to mark where the spawn points are (and not, e.g. to depict them visually for the player), then there’s a good chance that your true best-peformance option would be to find all the spawn locations once at start up, store their locations in variables, and then delete all of the game objects because they likely take up WAY more space than your in-code representation.
But again, this optimization is probably not large enough to be noticeable either way. Even if the spawn points are currently invisible to the player, performance considerations wouldn’t stop you from making them visible if you thought that was better for the game, right? So this whole problem is smaller than the incidental costs you would accept without even thinking while trying to improve your game.
2 Likes
Thanks so much! I’ trying to get a stronger grasp of that trade-off you mentioned when writing code for these things. Super helpful.
I agree with 100% of what @Antistone says above, but regardless of which choice you make, I suggest you create an additional respawn manager class that does one of the specific implementation above.
The rest of your program would simply call the respawn manager to get what it needs. That way if you have 57 different places that need to make an enemy respawn, if you decide you want to change how you do the spawns, you do not have 57 places to change, you only have 1 place: the respawn manager!
1 Like
Thanks for the advice! I added a “spawn manager” script that now handles all that functionality for my AI (and player) so it allowed me to clean up my AI scripts a bit.