Recap: Save Slots
Part of the Game Architecture in Unity section of Coddy's Unity journey. Lesson 27 of 50.
Challenge
MediumA farming game has three save slots, each in its own file: slot1.json, slot2.json and slot3.json in Application.persistentDataPath. Write SlotSave and the SlotMenu:
SlotSave(in SlotSave.cs) is a[System.Serializable]class with the public fieldsdayandgold. A new farm starts on day 1 with 20 gold, and a save from an older version that lacks a value gets that same starting value.- When the game starts, the menu loads every slot that has a file. Slot 1 is selected; the down arrow selects the next slot and the up arrow the one before, stopping at slot 3 and slot 1.
- The three
slotTextsshow each slot asSlot 2: Day 4, 65 goldorSlot 2: empty, and the selected slot's line starts with>, as> Slot 1: empty. - Space on an empty slot starts a new farm there. Space on a slot in use plays a day: the day goes up by 1 and the gold by 15. Either way the slot is saved to its file.
- The Delete button calls
DeleteSelected(), which deletes the selected slot's file and empties the slot.
Press Play, start a farm or two and play some days, then Stop and Play again. Then Check.
Try it yourself
using System.IO;
using UnityEngine;
using UnityEngine.InputSystem;
using TMPro;
public class SlotMenu : MonoBehaviour
{
public TMP_Text[] slotTexts;
// The Delete button calls this
public void DeleteSelected()
{
}
}
All lessons in Game Architecture in Unity
1Components, Not Hierarchies
Small ComponentsA Base Enemy ScriptAbstract ComponentsComponents Working TogetherRecap: Modular Turret10Project: Arena Survivor
The HeroEnemy Types