Saving & Loading
Saving and loading Feel Speak data.
Speaker Dialogue
Choices
Examples
With ESave Free
using Esper.ESave;
using Esper.FeelSpeak;
using Esper.FeelSpeak.Graph;
using Esper.FeelSpeak.Graph.Data;
using Esper.FeelSpeak.Overworld;
using UnityEngine;
public class FeelSpeakSavingExample : MonoBehaviour
{
/// <summary>
/// The save file.
/// </summary>
private SaveFile saveFile;
/// <summary>
/// The speaker to save data for.
/// </summary>
[SerializeField]
private Speaker speaker;
private void Awake()
{
// Get the save file from the SaveFileSetup component
saveFile = GetComponent<SaveFileSetup>().GetSaveFile();
// Add an event that will save a choice when it's made
Dialogue.onChoiceMade.AddListener(SaveChoice);
}
/// <summary>
/// Saves a choice.
/// </summary>
/// <param name="choice">The choice to save.</param>
public void SaveChoice(Choice choice)
{
// Technically the whole Choice struct can be saved, but we really only need to save the name
// for checking purposes. The value type we save it as doesn't matter since we only need to
// to check if the choice was made or not, which we will do with saveFile.HasData("choice name").
saveFile.AddOrUpdateData(choice.name, true);
// Confirm changes. This is simply here for informational purposes. Save should actually be
// called as minimally as possible.
saveFile.Save();
}
/// <summary>
/// Checks if a choice was made.
/// </summary>
/// <param name="name">The name of the choice.</param>
/// <returns>True if the specific choice was made by the player. Otherwise, false.</returns>
public bool WasChoiceMade(string name)
{
return saveFile.HasData(name);
}
/// <summary>
/// Saves the speaker's current dialogue.
/// </summary>
public void SaveSpeakerDialogue()
{
// Get the dialogue graph ID
var dialogueGraphId = speaker.dialogue.id;
// Save it
saveFile.AddOrUpdateData("MySpeakerDialogue", dialogueGraphId);
// Confirm changes
saveFile.Save();
}
/// <summary>
/// Loads and sets the speaker's current dialogue from the save file.
/// </summary>
public void LoadSpeakerDialogue()
{
// Loads the dialogue graph ID
var dialogueGraphId = saveFile.GetData<int>("MySpeakerDialogue");
// Set it to the speaker by using the ID to get the graph
speaker.dialogue = FeelSpeak.GetDialogueGraph(dialogueGraphId);
}
}ESave Pro
Last updated