frame

Howdy, Stranger!

It looks like you're new here. If you want to get involved, click one of these buttons!

Sign In Register

Difficulties with basic life counter for a platform game

edited February 7 in How To
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:

1. When the player dies take away one life
2. If the resulting life count is greater than or equal to zero restart the level
3. 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.

imagePika life loss behaviors

Comments

  • WOW, thats a lot to take in, there is a section called help at the top of flowlab, go to that, click helpful examples, and scroll down until you get to the health counter stuff. If that doesn't help, then go to my game here:
    http://www.flowlab.io/game/play/707504
    Click open editor, go to library and select the "player" sprite, the health system I use is bottom left
  • edited February 8
    @jherman4, @grazer
    Saves work by saving a number to the computer/file after giving the save a name.
    This is helpful unless you need to set everything up with specific numbers already from the start.
    When a new computer/user plays, the Saves are automaticlly set to 0 until a number is later saved to it.
    One way to combat this is to set a filter to see if the save = 0 (which is a new player/user/computer) and if true, to give that save the number what you want it.
    You could also take advantage of the fact that 0 will be number of an unsaved save and using 0 for when starting out with a locked feature or an item you do not have yet.

    Also I think setting an output to a restart or nextlevel well send after the prevoius level is deleted and after next level is loaded
  • edited February 7
    You could just use "Lives" to check if its a new user, but the problem is that you need to use 0 as part of the save later on.

    I'm thinking instead of using several "New Game" saves, you can just use 1 save at the beggining (called something like "New User Check" or something) And use a once to read of the new save and filter to check if it = 0.
    If it does = 0; you can set 1 to the new save, and set 9 to "Lives".
    If it doesn't = 0; it would just go to read on "Lives"

    Image of the above description: https://drive.google.com/open?id=1YmGKM2DX-iQ6KG9lg1sRT4Q3ElO2bJYg
  • Could you possibly take the easy way out and use position to to reset the player when he takes damage and only use save keep the current number of lives when he beats a level? Here is an example http://flowlab.io/game/play/1103771
  • Yes, but if you want to reset, let's say, moving enemies, then that wouldn't work. I would recommend looking at @Latif 's The Graveyard for more help. It helped me!
Sign In or Register to comment.

flowlab.io

| make games in your browser
@ 2017 Flowlab.io, All rights reserved.

Contact us

Get In Touch