I teach a game design course for elementary school students using flowlab, lately we have been working on platform games. Naturally the students were interested in adding a life counter. When I was designing a life counter in my example game I ran in some big problems with how flowlab manages numbers and saving that I couldn’t resolve without resorting to a very hacky solution. I want to discuss my problem with the community to make sure I’m not missing something here:
The behaviors I was trying to implement for my life counter were simple enough on paper:
- When the player dies take away one life
- If the resulting life count is greater than or equal to zero restart the level
- If the resulting life count is less than zero display a game over message and restart the game
This is classic behavior featured in many platform games including Super Mario Bros and Donkey Kong Country.
My problem stems from the fact that every time you restart the level (NextLevel behavior block, set to “Restart current”) it reloads the level and all the objects in it which resets all behavior blocks to their original configurations; reseting my life counter and effectively giving the player infinite lives.
I tried to resolve this in two ways. First I read on some forum post that objects in the UI layer do not reload and reset during a NextLevel so I first tried moving the counter behaviors to a gui object and had my player object send messages to the counter system in the UI. This was not successful, the numbers in the UI object would reset upon NextLevel which yielded the same infinite lives behavior as my first attempt.
Next I tried an implementation using save blocks and had a little more success. In this design I used save blocks to save the number of lives before restarting a level. If the player died when he had zero lives left (the game over scenario) then the life counter was reset to the original amount of lives and a Restart Level block was triggered. When my player object was created a Once trigger would be used to load the life counter from a Save block. This worked great except that I can find no way of initializing what value is stored in the save block. So when I loaded the life count at the very beginning of play I had no idea what value was stored in the file connected to the save block. Obviously on my own computer there are ways to make sure the correct number is stored in the file, especially using the debugger in flowlabs behavior space. However that is clearly not a satisfactory solution.
I also could not find a way to load an initial value on RestartGame but load from a save block on NextLevel. I tried using the "out’ output from next level to do something like this but that also failed. Apparently both RestartGame and NextLevel reload all the objects immediately so the “out” on both of those blocks appear to be unusable as anything triggered by them will be destroyed and reloaded before anything meaningful can happen.
I finally got it working using a fairly hacky solution. I ended up using an arbitrary number to determine whether to load from save or start with an initial value. I created another save file (save block with another name) and after every level or game reset a Once trigger would load the value from this second save file and if the value was equal to 555 it would load the life count from the first save file otherwise it would use an initial life count set in the expression block that decremented the life count. If the player lost a life but still had more left before I restarted the currently level I would save 555 to the second save file. Upon game over I would save 0 to the second file so the filter condition would fail on the restart and we would be back to our original lives. This is functional but is non-ideal in the fact that it depends on the initial value in the second save file not to be 555 (which is admittedly not likely) and it is also pretty non intuitive logically or from a programming standpoint. Anyone know of a better way to do this WITHOUT implementing a Menu at the beginning of the game?
It’s pretty frustrating to have to jump through all this crazy hoops for what is essentially a very simple data management task.
P.S. I have attached an annotated scheme for the behavior of the hacky solution I described last. As a contextual note: this game is based on the adventures of my cat Pika.