Hello guys,
Background:
I am trying to watch out for performance from the beginning of development with C2 and don't want to refactor my code so I decided instead of creating/destroying objects, I should disable and enable them on run time as needed. However I have a couple of questions about the whole thing before I attempt moving further into it and wanted to see if you guys can help me with it. Note the game is going to be released as an HTML5 game; playable on mobile and browsers.
Explanation:
We have a layout where we need to spawn enemies on run time. The enemies are limited to let's say 10 and depending on the level they have different types and chances. The game has a wave system; each level has 3, so each wave being done a new set of enemies should spawn. Note however that waves doesn't relate to number of enemies but rather time, this means that you could have 3 enemies left but the time for wave 1 ended so now you need to spawn 10 more regardless of the 3 that exist. All enemies are stored in a family called Enemies and there is an EnemyList (array object) that will store UID of each object.
Questions:
The easy question first; do you think object pooling in Construct 2 is worth it? In Unity for example it saves memory by not triggering the garbage collection memory thingy (I am not a coder so I don't know the full details, just know it saves memory by not activating 2 process, destroying and creating a new object).
The harder question -- if the first question's answer is yes -- now my pooling logic proved to be exhausting and complicated which drove me to think I might either be missing something or there might be a better way to do all of this. That is the question, is there a way to do the object pooling better than my below code?
Variables for preparations:
Now the action:
Basically handle the call depending on what is going on:
Pick a random number from the list of numbers in the family + 1 (since the random function isn't inclusive), then set the enemyPicker to that number. Now pick a spawner and pass its ID to the function that spawns the enemy.
The function that spawns the enemy compares enemyPicker and checks one of 3 scenarios:
1- If the enemy is invisible but overlaps the spawner -- it is deactivated and therefore we can activate the current type of enemy that is nearby and set its position to where we are.
2- There are no overlaps but there is the same type of enemy somewhere else in the map -- we then spawn a whole new enemy and push it to the EnemyList array that currently holds all the enemies we have in the level.
3- There is actually an enemy overlapping the current spawner -- it is taken by an enemy and we can't use it, so we basically call a "Look for Another Spawner" function to find another spawner.
This of course has to be repeated to all types of enemies in the game manually one by one:
Finally here is the "Look for Another Spawner" function (bottom 2 lines).
As you guys can see it gets complicated and really tedious (not to mention confusing) as I go along -- specially if I wanted to increase the variant of enemies (now there are only 6). So I am unsure if what I am doing is worth it to evade the create/destroy process -- maybe even it is worth it but I am doing it wrong. What do you guys think?
Thanks a lot in advance!
EDIT: I noticed a few "bugs" with my setup for example the first check with the Goon enemy shouldn't have inverted "Overlapping with Spawner" and also a few other places. I already fixed them in my game but I am a bit lazy to get new shots, upload, crop, save, C&P links here and update the post. Besides, I believe the bugs are irrelevant to the questions.