Browse Source

Loading of saved data

master
Christopher 8 years ago
parent
commit
b642fb5522
  1. 59
      Assets/Fungus/Scripts/Commands/LoadFlowchart.cs
  2. 81
      Assets/Fungus/Scripts/Components/SaveManager.cs
  3. 118
      Assets/Fungus/Scripts/Utils/SavePointData.cs

59
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<SavePointData>(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()

81
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<Flowchart>();
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<SavePointData>(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);
}

118
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<StringVar> stringVars = new List<StringVar>();
public List<IntVar> intVars = new List<IntVar>();
public List<FloatVar> floatVars = new List<FloatVar>();
public List<BoolVar> boolVars = new List<BoolVar>();
public static void ResumeSavedState(SavePointData saveData)
{
var go = GameObject.Find(saveData.flowchartName);
if (go == null)
{
return;
}
var flowchart = go.GetComponent<Flowchart>();
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<StringVar> stringVars = new List<StringVar>();
public List<IntVar> intVars = new List<IntVar>();
public List<FloatVar> floatVars = new List<FloatVar>();
public List<BoolVar> boolVars = new List<BoolVar>();
}
}
Loading…
Cancel
Save