Earlier today I saw it fit to change the way that snakes spawn on the levels slightly, they were coming in massive waves and I felt that gradual, constant snakes would suit the levels’ design a lot better, then there was survival – mostly easy until it tipped past a point of becoming impossible with swarms of snakes surrounding the player.
So In my head I had thought to myself, ‘Well it shouldn’t be too hard, we have a system to increase/decrease the snake numbers however we wish, I’ll just do that’.
Then, I opened up the script for the snake spawning.
For 20 minutes I sat there in bewilderment, wondering what we were thinking back then.
I couldn’t make out what was going on in our old code, it was a mess. There was even leftover code from when I apparently tried to merge the snake spawning and ‘space scenery’ spawning systems. I just couldn’t work with it, so I deleted the whole thing and started again.
The new system is much better, of course. It does everything that the old system did and more in a third of the code, not to mention all of it is placed in one object instead of over 10.
Here’s how it works:
Firstly, the new script is run every 2 seconds, instead of 30 times a second (even though the amount of snakes depends on the score, which is incremented in seconds).
So the first thing that happens is that the score is divided by 5, then adding a random amount between 1 and 5, this gives us the total amount of snakes that should be spawned. The script then counts the total amount of all of the snakes currently spawned, and finds the difference between that and the number I mentioned above.
This difference gives us the amount of snakes that need to be spawned during this run of the script. The next thing to be done is to split this number up between all of the snake types.
Then finally comes the part where the snakes are spawned, the room is checked first, to see which directions the snakes should be coming from. (If we had snakes coming in from above on the levels, it’d be really mean.)
So the script simply cycles through each snake type and picks a random point (within the specified areas of course), then finally the script is set to run again in 2 seconds.
Much better right? Well you wouldn’t know unless you knew the old system, so I undertook the tedious task of deducing how the old system worked:
It performed similar to start off with, picking a calculated number depending on the player’s score, checking the room etc.
The old system also spawned more snakes with their direction directly up, this caused the old system to simply stop spawning snakes at times because there were always snakes inside the room, as spawning facing upwards gives the snakes a lot longer life, as their main cause of death is leaving the game room.
The major difference is that the old system waited until a certain variable in an object (specific to each snake type) told the script that it was allowed to spawn, and these objects only did this when the amount of snakes currently spawned were 0. This caused the ‘wave’ effect, the snakes only spawned all at once, never gradually.
Whereas the new system finds out how many snakes are currently not spawned that’re supposed to be, and spawns until it hits that number.
Another cool thing I did today was find out that GameMaker uses the same seed for it’s random functions every time, so I couldn’t have that and I’ve set the seed to change to the current date/time every time the main menu is loaded. This doesn’t mean random is going to be more random, only it won’t be the same set of random every time.
Tired. Signing Off.