In this tutorial, we will create a basic GUI which will contain a series of save slots, allowing up to 4 separate saved games. Each saved game will store a level number; when the save slot is clicked, that level will be loaded.
Creating a Save Slot Button
Our save slots will be represented by 4 buttons; one for each save slot. We will create a new scene which will contain these buttons, though it would also be possible to create them in an existing scene.
- Create a new scene and add a new GameObject to it. This will be our button.
- Add an FSM to this GameObject and name it something appropriate. We will name our first state 'Idle'.
- In the Action Browser, add a 'GUI Button' Action to the first state and set it to a suitable size and position.
- Right-click the state, select 'Add Transition' and add a 'MOUSE DOWN' transition.
- In our 'GUI Button' state, set 'Send Event' to 'FINISHED'. Now when this button is pressed, it will fire an event.
- In the 'Text' field, type 'Save Slot 1', or something similar. This will be the label on our button.
- Go to the 'Variables' tab and add a variable named 'LevelNumber' with a Type of 'Int', and set it's value to the number of your first level (we've set ours to '1').
- Also under the 'Variables' tab, click 'Global Variables' and add a global variable called 'SaveSlot' of type 'String'. This will tell us where to save data for the currently selected slot.
- Now go to the 'Events' tab and add two new events: 'EXISTS' and 'DOESNOTEXIST'. Note that you will have to write the name of the event into the 'Add Event' text field, and then press Return to add the Event.
- Add a new state to the FSM called 'Set Save Slot', and add a 'FINISHED' transition to it.
- Add a 'Set String Value' action to our 'Set Save Slot' state, set the 'String Variable' field to the 'SaveSlot' global variable and set the 'String Value' field to the tag for this save slot (in this case, 'slot1').
- Add a new state to the FSM, which we will call 'Exists'.
- Right-Click the first 'Idle' state and set the 'Transition Target' to our 'Exists' state. When the button is pressed, this state will be activated.
- Open the Action Browser and add an 'Exists' Action (found in the 'Easy Save 2' section) to the 'Exists' state.
- In the 'Exists' state, set the 'If Exists' and 'If Does Not Exist' fields to the 'EXISTS' and 'DOESNOTEXIST' states we made earlier.
- Set the 'Unique Tag' to our 'SaveSlot' global variable. This is where we save our level number for this save slot.
- Add the two transitions to our 'Exists' state: 'EXISTS' and 'DOESNOTEXIST'.
- Create a new 'Load' state and link it to the 'EXISTS' transition of the 'Exists' state.
- Add a 'Load Int' Action to this State, set the 'Load Value' field to our 'LevelNumber' variable, and the Unique Tag field to our 'SaveSlot' global variable.
- Add a 'FINISHED' transition to this.
- Create a new 'LoadLevel' state and link it to the 'DOESNOTEXIST' transition of the 'Exists' state, and the 'FINISHED' transition of the 'Load' state.
- Add a 'Load Level Num' Action to this State, set the 'Level Index' field to our 'LevelNumber' variable, and untick 'Dont Destroy On Load'.
Your FSM should look something like these:
Saving our Level Number
The easiest way to do this is to save the level number when we enter the level.
- Create a new GameObject in your scene and add an FSM to it.
- Name the first state 'Save', open the Action Browser and add the 'Save Int' state found under the 'Easy Save 2' section to it.
- In the 'Save Int' action, set the 'Save Value' to your level number, or a variable containing your level number.
- Set the 'Unique Tag' to our 'SaveSlot' global variable that we made earlier.
- When the scene starts, it will save the level number to the correct save slot. This tutorial is complete!
It should also be noted that you need to add the 'Save Slot' scene to 'Scenes in Build' for this to work.
Your FSM for the level should look like this:
Find below the Scenes for this example. You may need to go to 'Scenes in Build' list in 'Files > Build Settings' and add the Level 1, Level 2 and Level 3 scenes to the scene list.