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. 38
      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.")] "Loads a previously saved Flowchart state. The original scene is loaded and the resume block is executed.")]
public class LoadFlowchart : Command 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 IntegerData saveSlot = new IntegerData(0);
[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;
}
#region Public members #region Public members
public override void OnEnter() public override void OnEnter()
{ {
var key = CreateSaveKey(); var saveManager = FungusManager.Instance.SaveManager;
var jsonData = LoadJSONData(key);
if (jsonData == "")
{
// Save data not found, continue executing block
Continue();
return;
}
LoadSavedState(jsonData); saveManager.Load(0);
} }
public override string GetSummary() public override string GetSummary()
{ {
return saveKey.Value; return saveSlot.Value.ToString();
} }
public override Color GetButtonColor() public override Color GetButtonColor()

81
Assets/Fungus/Scripts/Components/SaveManager.cs

@ -26,6 +26,8 @@ namespace Fungus
protected static SaveManager instance; protected static SaveManager instance;
protected SavePointData tempSaveData;
protected virtual void Awake() protected virtual void Awake()
{ {
instance = this; instance = this;
@ -110,6 +112,44 @@ namespace Fungus
return JsonUtility.ToJson(saveData, true); 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) protected virtual void StoreJSONData(string key, string jsonData)
{ {
if (key.Length > 0) 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 #region Public members
public static SaveManager Instance { get { return instance; } } public static SaveManager Instance { get { return instance; } }
@ -160,11 +220,11 @@ namespace Fungus
return; return;
} }
// Load JSON data for active slot var jsonData = LoadJSONData(key);
// Convert to SavePointData tempSaveData = JsonUtility.FromJson<SavePointData>(jsonData);
// Load scene
// Populate variables SceneManager.sceneLoaded += OnSceneLoaded;
// Execute Block and Label SceneManager.LoadScene(tempSaveData.sceneName);
} }
public virtual void Delete(int slot) public virtual void Delete(int slot)
@ -175,17 +235,6 @@ namespace Fungus
public virtual void PopulateSaveBuffer(Flowchart flowchart, string resumeBlockName) 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); saveBuffer = CreateSaveData(flowchart, resumeBlockName);
} }

38
Assets/Fungus/Scripts/Utils/SavePointData.cs

@ -43,43 +43,5 @@ namespace Fungus
public List<IntVar> intVars = new List<IntVar>(); public List<IntVar> intVars = new List<IntVar>();
public List<FloatVar> floatVars = new List<FloatVar>(); public List<FloatVar> floatVars = new List<FloatVar>();
public List<BoolVar> boolVars = new List<BoolVar>(); 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);
}
} }
} }
Loading…
Cancel
Save