Tutorial: High Score - CodingDino/Dino-Unity-Toolkit GitHub Wiki

In this tutorial, you will learn how to compare the player's current score with a saved High Score, and how to overwrite the high score if the player's score is higher. We'll accomplish this by using a special type of component, a Comparison, which is a combination of an Action and an Activator and which will let us make a decision by comparing two different values. In our case, these values will come from Data components.

TODO: IMAGE

This tutorial requires a persistent score system such as the one from Tutorial: Persistent Score. To begin, create a "win screen" (see Tutorial: Win Screen for more information on how to do this) and display the player's score there, just as you did in the second level from the Persistent Score tutorial.

Before we start, let's clearly state our goal behaviour:

If our player's score is higher than the saved High Score, replace the High Score with the player's current score and display a congratulatory message.

This can be seen as one activator and two actions. However, before we can dive into these, we need to create our high score GameObject and get it loading a value.

High Score GameObject

The high score GameObject is virtually identical to our normal score GameObject. In fact, the easiest way to set it up is to just duplicate the score's GameObject and change the Save ID!

Select your score's GameObject and duplicate it (either right click > Duplicate or use CTRL + D). Rename your new GameObject to "High Score" or something similar. Find the IntData component on your new GameObject and change the SaveID to "HiScore" or something similar.

TODO: Image

With this high score GameObject created, we can now start setting up our Actions and Activators

Action 1 - Save New Score

Our first Action requires us to save our new score as a high score:

...replace the High Score with the player's current score...

To do this, we simply need to use two Actions already on the IntData component of our HighScore: ActionSetValue() and ActionSaveValue().

What this means right now is that we have no extra components that need to be added to make this Action work.

Action 2 - Display Message

The last part of the goal behaviour describes a second action:

...display a congratulatory message.

Let's create a text object to use to congratulate the player! Create a new GameObject with a Text component on it by selecting from the menu Game Object > UI > Text. Rename the object to be something like "Congrats Message". Edit the Text to say something like "NEW HI SCORE!" and place it in the centre of the screen.

TODO: Image

Next, let's figure out how we will display our message. The easiest way is to just disable our Congrats object until we want it, then enable it using an EnableObjectAction, which we can later trigger from an Activator.

Select your Congrats object. Click "Add Component" and select "Dino Toolkit > Action > EnableObjectAction". The object is now ready to be enabled or disabled by an Activator!

TODO: Image

Comparison

It looks like all we need now is an Activator to finish our goal behaviour:

If our player's score is higher than the saved High Score...

What we will actually use is a special type of component from the Dino Toolkit called a Comparison. Comparisons, like Data, act like both Actions and Activators. They need to be triggered like an Action, but they themselves also trigger other Actions, like Activators.

In our case, we'll be comparing two IntData components, so we need an IntComparison.

  1. Select your "High Score" GameObject.

  2. Click Add Component and select Dino Toolkit > Comparisons > IntComparison

    TODO: IMAGE

  3. Go to your new IntComparison component on your "High Score" GameObject

  4. Make sure Standard Type is set to "DATA".

    This means we'll be comparing to an IntData component attached to a GameObject, instead of a raw typed in number.

  5. Find the Standard Data slot and drag the high score's GameObject into the slot.

    This means we will be comparing to the high score IntData's value.

    TODO: IMAGE

  6. Find the On Greater Than () section of the Activation List.

    We are choosing this section because we want to perform an action when the thing we are comparing (the score) is greater than our standard (the high score)

  7. Click the + button to add a new Activation to the On Greater Than () list.

  8. Drag in the "High Score" GameObject, select IntData > ActionSetValue from the list, and drag in the "Score" GameObject to the new slot.

    This will perform our first action, replacing the high score's value with the current score's value.

    TODO: IMAGE

  9. Click the + button to add a second Activation to the On Greater Than () list.

  10. Drag in the "High Score" GameObject, and select IntData > ActionSaveValue from the list.

    This will save the newly set high score to disk, so if the game is closed, the value will persist next time it is run.

    TODO: IMAGE

  11. Click the + button to add a third Activation to the On Greater Than () list.

  12. Drag in the "Congrats" GameObject, and select EnableObjectAction > ActionEnableObject from the list.

    This will perform our second action, turning on the congrats message.

    TODO: IMAGE

That's a lot of setup! However, we aren't quite done. At its heart, a Comparison component is still an Action - that means it won't occur on it's own! We need to trigger the comparison somehow.

Activating the Comparison

Based on our behaviour statement, it's not clear exactly when this comparison should occur. Let's modify our statement to make that clear:

When the win screen is displayed, if our player's score is higher than the saved High Score, replace the High Score with the player's current score and display a congratulatory message.

The first part of this is now our final Activator:

When the win screen is displayed...

This means we should trigger the Comparison's Action when the screen first loads - this is usually done using the On Awake () or On Start () Activations of an InitializerActivator.

Our "High Score" GameObject already has an InitializerActivator on it - let's add our new activation to the On Start () section of this Activator. This is because we want to make sure it comes AFTER the score and high score have both been loaded, which are set to load in the On Awake () section.

  1. Select your "High Score" GameObject.

  2. Find the InitializerActivator component.

  3. Click the + button to add a new Activation to the On Start () list.

  4. Drag in the "High Score" GameObject, select IntComparison > ActionCompareToTarget (Data) from the list, and drag in your normal "Score" GameObject.

    This means we will compare our current score to our high score when the game starts, and trigger any activations in the IntComparison component based on this comparison.

    WARNING: Make sure to chose the "IntData" Action and not the "int" - otherwise you won't be able to drag your "Score" GameObject in!

    TODO: IMAGE

With that, your high score system should work!

TODO: IMAGE

If you ever want to reset your high score for testing purposes, you can do that in the editor by right-clicking the IntData component and choosing "Clear Saved Value" from the menu.

TODO: IMAGE

Further Development

You could expand on this high score system even further by adding multiple high scores, or even allowing name entry and saving the player's name. Unfortunately, the Dino Toolkit doesn't support name entry just yet, but it may be added in the future.