Keeping score

Let's have a score so the player knows how well they've done. We'll need another variable for this. You might think "lets put the score as one of the player's instance variables!". That's not a bad first idea, but remember the value is stored "in" the object. If there are no instances, there are no variables either! So if we destroy the player, we can no longer tell what their score was, because it was destroyed with the player.

Instead, we can use a global variable. Like an instance variable, a global variable (or just "global") can store text or a number. Each variable can store a single number or a single piece of text. Global variables are also available to the entire game across all layouts - convenient if we were to add other levels.

Right-click the space at the bottom of the event sheet, and select Add global variable.

Enter Score as the name. The other field defaults are OK, it'll make it a number starting at 0.

Now the global variable appears as a line in the event sheet. It's in this event sheet, but it can be accessed from any event sheet in any layout.

Let's give the player a point for killing a monster. In our "Monster: health less or equal 0" event (when a monster dies), click Add action, and select SystemAdd to (under Global & local variables), and choose Score with a value of 1. Now the event should look like this:

Now the player has a score, which increases by 1 for every monster they kill - but they can't see their score! Let's show it to them with a text object.

Share this Tutorial

Make games in your browser

You may use this tutorial for any purpose (even commercial) if you properly attribute it. Click here for more information.