diff --git a/Assets/Fungus/Scripts/Commands/LoadFlowchart.cs b/Assets/Fungus/Scripts/Commands/LoadFlowchart.cs index 05f066c5..5800344f 100644 --- a/Assets/Fungus/Scripts/Commands/LoadFlowchart.cs +++ b/Assets/Fungus/Scripts/Commands/LoadFlowchart.cs @@ -12,71 +12,20 @@ namespace Fungus "Loads a previously saved Flowchart state. The original scene is loaded and the resume block is executed.")] public class LoadFlowchart : Command { - [Tooltip("Key for loading the saves data from PlayerPrefs. Supports variable subsitution {$VarName} and will prepend a profile name set using Set Save Profile command.")] - [SerializeField] protected StringData saveKey = new StringData("savedata"); - - protected SavePointData tempSaveData; - - protected virtual string CreateSaveKey() - { - var flowchart = GetFlowchart(); - var saveProfile = SetSaveProfile.SaveProfile; - - if (saveProfile.Length > 0) - { - return string.Format(saveProfile + "_" + flowchart.SubstituteVariables(saveKey.Value)); - } - else - { - return string.Format(flowchart.SubstituteVariables(saveKey.Value)); - } - } - - protected virtual string LoadJSONData(string key) - { - return PlayerPrefs.GetString(key); - } - - protected virtual void LoadSavedState(string jsonData) - { - tempSaveData = JsonUtility.FromJson(jsonData); - - SceneManager.sceneLoaded += OnSceneLoaded; - - // Load scene and wait - SceneManager.LoadScene(tempSaveData.sceneName); - } - - protected virtual void OnSceneLoaded(Scene scene, LoadSceneMode mode) - { - if (scene.name == tempSaveData.sceneName) - { - SavePointData.ResumeSavedState(tempSaveData); - } - - SceneManager.sceneLoaded -= OnSceneLoaded; - } + [SerializeField] protected IntegerData saveSlot = new IntegerData(0); #region Public members public override void OnEnter() { - var key = CreateSaveKey(); - var jsonData = LoadJSONData(key); - - if (jsonData == "") - { - // Save data not found, continue executing block - Continue(); - return; - } + var saveManager = FungusManager.Instance.SaveManager; - LoadSavedState(jsonData); + saveManager.Load(0); } public override string GetSummary() { - return saveKey.Value; + return saveSlot.Value.ToString(); } public override Color GetButtonColor() diff --git a/Assets/Fungus/Scripts/Components/SaveManager.cs b/Assets/Fungus/Scripts/Components/SaveManager.cs index 1bed20c3..6f9da2e9 100644 --- a/Assets/Fungus/Scripts/Components/SaveManager.cs +++ b/Assets/Fungus/Scripts/Components/SaveManager.cs @@ -26,6 +26,8 @@ namespace Fungus protected static SaveManager instance; + protected SavePointData tempSaveData; + protected virtual void Awake() { instance = this; @@ -110,6 +112,44 @@ namespace Fungus return JsonUtility.ToJson(saveData, true); } + protected virtual void RestoreSavedGame(SavePointData saveData) + { + var go = GameObject.Find(saveData.flowchartName); + if (go == null) + { + return; + } + + var flowchart = go.GetComponent(); + if (flowchart == null) + { + return; + } + + for (int i = 0; i < saveData.boolVars.Count; i++) + { + var boolVar = saveData.boolVars[i]; + flowchart.SetBooleanVariable(boolVar.key, boolVar.value); + } + for (int i = 0; i < saveData.intVars.Count; i++) + { + var intVar = saveData.intVars[i]; + flowchart.SetIntegerVariable(intVar.key, intVar.value); + } + for (int i = 0; i < saveData.floatVars.Count; i++) + { + var floatVar = saveData.floatVars[i]; + flowchart.SetFloatVariable(floatVar.key, floatVar.value); + } + for (int i = 0; i < saveData.stringVars.Count; i++) + { + var stringVar = saveData.stringVars[i]; + flowchart.SetStringVariable(stringVar.key, stringVar.value); + } + + flowchart.ExecuteBlock(saveData.resumeBlockName); + } + protected virtual void StoreJSONData(string key, string jsonData) { if (key.Length > 0) @@ -118,6 +158,26 @@ namespace Fungus } } + protected virtual string LoadJSONData(string key) + { + if (key.Length > 0) + { + return PlayerPrefs.GetString(key); + } + + return ""; + } + + protected virtual void OnSceneLoaded(Scene scene, LoadSceneMode mode) + { + if (scene.name == tempSaveData.sceneName) + { + RestoreSavedGame(tempSaveData); + } + + SceneManager.sceneLoaded -= OnSceneLoaded; + } + #region Public members public static SaveManager Instance { get { return instance; } } @@ -160,11 +220,11 @@ namespace Fungus return; } - // Load JSON data for active slot - // Convert to SavePointData - // Load scene - // Populate variables - // Execute Block and Label + var jsonData = LoadJSONData(key); + tempSaveData = JsonUtility.FromJson(jsonData); + + SceneManager.sceneLoaded += OnSceneLoaded; + SceneManager.LoadScene(tempSaveData.sceneName); } public virtual void Delete(int slot) @@ -175,17 +235,6 @@ namespace Fungus public virtual void PopulateSaveBuffer(Flowchart flowchart, string resumeBlockName) { - var block = flowchart.GetBlock("BlockName"); - - foreach (var command in block.CommandList) - { - if (command is Menu) - { - - } - } - - saveBuffer = CreateSaveData(flowchart, resumeBlockName); } diff --git a/Assets/Fungus/Scripts/Utils/SavePointData.cs b/Assets/Fungus/Scripts/Utils/SavePointData.cs index 89dc1544..45a2e889 100644 --- a/Assets/Fungus/Scripts/Utils/SavePointData.cs +++ b/Assets/Fungus/Scripts/Utils/SavePointData.cs @@ -4,82 +4,44 @@ using UnityEngine.SceneManagement; namespace Fungus { - [System.Serializable] - public class StringVar - { - public string key; - public string value; - } - - [System.Serializable] - public class IntVar - { - public string key; - public int value; - } - - [System.Serializable] - public class FloatVar - { - public string key; - public float value; - } - - [System.Serializable] - public class BoolVar - { - public string key; - public bool value; - } - - [System.Serializable] - public class SavePointData - { - public string sceneName; - public string flowchartName; - public string resumeBlockName; - - public List stringVars = new List(); - public List intVars = new List(); - public List floatVars = new List(); - public List boolVars = new List(); - - public static void ResumeSavedState(SavePointData saveData) - { - var go = GameObject.Find(saveData.flowchartName); - if (go == null) - { - return; - } - - var flowchart = go.GetComponent(); - if (flowchart == null) - { - return; - } - - for (int i = 0; i < saveData.boolVars.Count; i++) - { - var boolVar = saveData.boolVars[i]; - flowchart.SetBooleanVariable(boolVar.key, boolVar.value); - } - for (int i = 0; i < saveData.intVars.Count; i++) - { - var intVar = saveData.intVars[i]; - flowchart.SetIntegerVariable(intVar.key, intVar.value); - } - for (int i = 0; i < saveData.floatVars.Count; i++) - { - var floatVar = saveData.floatVars[i]; - flowchart.SetFloatVariable(floatVar.key, floatVar.value); - } - for (int i = 0; i < saveData.stringVars.Count; i++) - { - var stringVar = saveData.stringVars[i]; - flowchart.SetStringVariable(stringVar.key, stringVar.value); - } - - flowchart.ExecuteBlock(saveData.resumeBlockName); - } - } + [System.Serializable] + public class StringVar + { + public string key; + public string value; + } + + [System.Serializable] + public class IntVar + { + public string key; + public int value; + } + + [System.Serializable] + public class FloatVar + { + public string key; + public float value; + } + + [System.Serializable] + public class BoolVar + { + public string key; + public bool value; + } + + [System.Serializable] + public class SavePointData + { + public string sceneName; + public string flowchartName; + public string resumeBlockName; + + public List stringVars = new List(); + public List intVars = new List(); + public List floatVars = new List(); + public List boolVars = new List(); + } } \ No newline at end of file