From 71c2b4e357a8406cb3fcc3a01040dc3c8faa8890 Mon Sep 17 00:00:00 2001 From: Christopher Date: Tue, 15 Nov 2016 11:43:08 +0000 Subject: [PATCH] Added Game Loaded event handler and save key. --- Assets/Fungus/Scripts/Commands/SavePoint.cs | 19 +- Assets/Fungus/Scripts/Components/Flowchart.cs | 1 - .../Fungus/Scripts/Components/SaveManager.cs | 30 ++- .../Fungus/Scripts/Editor/SavePointEditor.cs | 44 ----- .../Scripts/EventHandlers/GameLoaded.cs | 32 ++++ .../GameLoaded.cs.meta} | 4 +- Assets/Fungus/Scripts/Utils/SavePointData.cs | 2 +- Assets/FungusExamples/SaveGame/SceneA.unity | 179 ++++++++++++++++-- 8 files changed, 225 insertions(+), 86 deletions(-) delete mode 100644 Assets/Fungus/Scripts/Editor/SavePointEditor.cs create mode 100644 Assets/Fungus/Scripts/EventHandlers/GameLoaded.cs rename Assets/Fungus/Scripts/{Editor/SavePointEditor.cs.meta => EventHandlers/GameLoaded.cs.meta} (76%) diff --git a/Assets/Fungus/Scripts/Commands/SavePoint.cs b/Assets/Fungus/Scripts/Commands/SavePoint.cs index bb9c67da..b1ce72e1 100644 --- a/Assets/Fungus/Scripts/Commands/SavePoint.cs +++ b/Assets/Fungus/Scripts/Commands/SavePoint.cs @@ -12,7 +12,7 @@ namespace Fungus "Creates a save point which can be saved to persistant storage and loaded again later.")] public class SavePoint : Command { - [SerializeField] protected Block resumeBlock; + [SerializeField] protected string saveKey; [SerializeField] protected bool saveNow; @@ -22,7 +22,7 @@ namespace Fungus { var saveManager = FungusManager.Instance.SaveManager; - saveManager.PopulateSaveBuffer(GetFlowchart(), resumeBlock.BlockName); + saveManager.PopulateSaveBuffer(GetFlowchart(), saveKey); if (saveNow) { @@ -34,12 +34,7 @@ namespace Fungus public override string GetSummary() { - if (resumeBlock == null) - { - return "Error: No resume block set"; - } - - return resumeBlock.BlockName; + return saveKey; } public override Color GetButtonColor() @@ -47,14 +42,6 @@ namespace Fungus return new Color32(235, 191, 217, 255); } - public override void GetConnectedBlocks(ref List connectedBlocks) - { - if (resumeBlock != null) - { - connectedBlocks.Add(resumeBlock); - } - } - #endregion } } \ No newline at end of file diff --git a/Assets/Fungus/Scripts/Components/Flowchart.cs b/Assets/Fungus/Scripts/Components/Flowchart.cs index 4ebdd2b7..a61f40f5 100644 --- a/Assets/Fungus/Scripts/Components/Flowchart.cs +++ b/Assets/Fungus/Scripts/Components/Flowchart.cs @@ -3,7 +3,6 @@ using UnityEngine; using UnityEngine.EventSystems; -using UnityEngine.Serialization; using System; using System.Text; using System.Linq; diff --git a/Assets/Fungus/Scripts/Components/SaveManager.cs b/Assets/Fungus/Scripts/Components/SaveManager.cs index 6f9da2e9..c4354849 100644 --- a/Assets/Fungus/Scripts/Components/SaveManager.cs +++ b/Assets/Fungus/Scripts/Components/SaveManager.cs @@ -55,14 +55,14 @@ namespace Fungus return true; } - protected virtual string CreateSaveData(Flowchart flowchart, string resumeBlockName) + protected virtual string CreateSaveData(Flowchart flowchart, string saveKey) { var saveData = new SavePointData(); // Store the scene, flowchart and block to execute on resume saveData.sceneName = SceneManager.GetActiveScene().name; saveData.flowchartName = flowchart.name; - saveData.resumeBlockName = resumeBlockName; + saveData.saveKey = saveKey; for (int i = 0; i < flowchart.Variables.Count; i++) { @@ -147,7 +147,27 @@ namespace Fungus flowchart.SetStringVariable(stringVar.key, stringVar.value); } - flowchart.ExecuteBlock(saveData.resumeBlockName); + // Fire any matching GameLoaded event handler with matching save key. + var eventHandlers = Object.FindObjectsOfType(); + for (int i = 0; i < eventHandlers.Length; i++) + { + var eventHandler = eventHandlers[i]; + eventHandler.OnGameLoaded(saveData.saveKey); + } + + // Execute any block with a Label matching the save key + var labels = Object.FindObjectsOfType