Saving, Loading, and High Scores

Often times you want to create a game where it remembers certain details about the previous time it was played.

What you want to save depends heavily on the game or app you're creating. Whether you want to save a score, the position, or the overall progress, you need to plan how and what you want to save.

 

In this tutorial we'll cover saving a players score, and creating a simply high score or "Best Previous" score system.

For this tutorial, we'll assume you have created a simple game where the player picks up coins (Part 5 of Creating a Platform Game), and the number of coins is displayed with a label on the screen.

 

Saving

When it comes to saving, there are two important questions you want to ask your self. What do you want to save, and when should you save? In hyperPad, the Save Behavior can only save a single value at a time. So you really need to think about what you're saving.

In this tutorial, we'll save the score (number of coins collected) that is displayed using a label on the screen.

Now that we know we're saving the score from the label, the next thing to figure out is when to save.

There are many options here, and they depend on your gameplay or overall design. You can have it save every time the player picks up the coin, save when the player loses, save when the player beats the level, save when the player presses a button, or what ever else you think is needed for your situation.

For this tutorial, we'll set up a button that saves the score when the player presses the save button. This same behavior logic can be applied if you want to save when the player loses or finishes the level. You just need to trigger these behaviors when you win or lose.

 

Select your Save Button object, and open the behaviors.

From the Interaction category of the behavior editor add a "Stopped Touching" behavior.

 

Now from the UI category add a "Get Label" behavior, and connect it to the "Stopped Touching" behavior from the previous step.

 

Tap the "Get Label" behavior to view the behavior properties. From the behavior properties, tap the area that represents the current object, then select the label in your scene that represents your score.

 

Next from the Custom category add a "Save to File" behavior and connect it to the "Stopped Touching".

 

Select the "Save to File" behavior your just added to view the behavior properties. Note the "Key Type" is set to existing. Existing keys are predefined things you want to save. 

First you need to add the title or description of something you want to save. This is called a "Key". Tap the "Select or Create Key" button to bring out the list of keys.

Enter a descriptive title at the top, then press the + button to add your first key. Since we want to save the score, enter "Score" as the key.

Next you need to actually save the score from the label. To do this, output the value from the corner of the "Get Label" behavior into the input field of the "Save to File" properties.

 

That's it! At this point, your game should be saving your score right when you press the save button. But you haven't actually done anything with the saved value. The next thing you need to do is Load the score and actually use it in your scene.

 

Loading

With your score saved, you now need to load it and use it in your scene. When it comes to loading, there is one important question you need to answer. When do you want to load?

Like saving, the answer depends heavily on your project. You can load as soon as your scene starts, when the player presses a button, or when ever some other event is triggered. It really depends on what you're trying to achieve.

For this tutorial, we'll load right when the scene starts and show the previous saved score.

 

To start, add a label into your scene. We'll use this label to show your saved score.

 

Next  we need to add the actual load logic. For this tutorial, we'll add the logic to the Save Button from the previous steps. However depending on your game, you may need to put your logic elsewhere. Select the save button and open the behavior editor from the object properties.

From the Custom category add the "Load from File" behavior and do not connect it to anything. Since it's not connected to any other behavior, the load behavior will execute right when the scene starts (when the button exists in the scene). If you wanted to load based on a certain even, or other trigger then you would connect it to other behaviors.

 

Now select the "Load from File" behavior to view the behavior properties. Once again, make sure "Existing" is selected for the key type.

Next tap the "Select Key" button to open the list of saved keys. From the list choose the "Score" key you added earlier.

 

At this point, you have technically loaded the score. But you're not doing anything with it yet. Lets put that loaded value to use.

 

From the UI category, add a "Set Label" behavior and connect it to the Load behavior your added previously.

 

Next select the "Set Label" behavior to view it's properties and remember to choose the new label you just added that represents the loaded score. Once you selected the correct label to change, drag the output value from the corner of the "Load from File" behavior into the input field of the Set Label behavior.

 

That's all you have to do! Your progress will save and be loaded into a label the next time you play.

 

High Score

One thing you may have noticed is that your score is saved every time you press the button and overwrites your previous score. Sometimes this is fine, but usually you only want to save when the player reaches a score greater then their previous one.

 

To implement this "High Score" system we only need to slightly modify our saving logic.

Start by going back to where the saving behaviors are placed (the save button).

First you need to disconnect the "Save to File" behavior from your Stopped Touching behavior. Tap the line connecting the two behaviors to disconnect them.

 

Next from the Logic category ad an IF behavior. The IF behavior will check when a certain condition is met before executing. In this case we only want to save IF the score is greater than the previous (loaded) score.

Connect the IF behavior to the "Stop Touching" behavior. 

 

Next you need to tap the IF behavior to open the behavior properties and set up the condition.

Drag the output value from "Get Label" into the first input field of the IF behavior.

Then drag the output value from the "Load from File" behavior into the second input field of the IF behavior.

 

Now you need to change the condition. Tap the "Is Equal To" button, and select the Grater than or Equal to () option.

 

Finally, connect your old save behavior to the IF. Now your IF behavior will only execute the connected save, IF the value from the label is greater than (or equal) to the loaded value.

 

That's all you need to do. You should now have a working saving and loading system that will save the players score only if they beat their previous attempt.

 

 

Other Saving

This tutorial covered saving a score. But sometimes you wan't to save other things, like a players position, or the current level etc.

The concepts you learned in this tutorial still apply. You just need to change when you're saving, and what you're saving.

Take saving the players position for example. An objects position is given in 2 numbers. The X, and the Y. For this, you would simply use 2 save behaviors. One to save the X position, and the other to save the Y position. Then when it comes to loading, you would use 2 load behaviors. Then instead of setting a label, you would simply move the player object to the loaded X and Y positions.

Making a plan of what you're trying to save, and when you need to save,  you can create a system that works for almost any situation.

 

 

0 Comments

Article is closed for comments.