/// <summary> /// Encodes the objects to be saved as a list of SaveDataItems. /// </summary public virtual void Encode(List <SaveDataItem> saveDataItems) { for (int i = 0; i < flowcharts.Count; i++) { var flowchart = flowcharts[i]; var flowchartData = FlowchartData.Encode(flowchart); var saveDataItem = SaveDataItem.Create(FlowchartDataKey, JsonUtility.ToJson(flowchartData)); saveDataItems.Add(saveDataItem); } }
/// <summary> /// Encodes the data in a Flowchart into a structure that can be stored by the save system. /// </summary> public static FlowchartData Encode(Flowchart flowchart) { var flowchartData = new FlowchartData(); flowchartData.FlowchartName = flowchart.name; for (int i = 0; i < flowchart.Variables.Count; i++) { var v = flowchart.Variables[i]; // Save string var stringVariable = v as StringVariable; if (stringVariable != null) { var d = new StringVar(); d.Key = stringVariable.Key; d.Value = stringVariable.Value; flowchartData.StringVars.Add(d); } // Save int var intVariable = v as IntegerVariable; if (intVariable != null) { var d = new IntVar(); d.Key = intVariable.Key; d.Value = intVariable.Value; flowchartData.IntVars.Add(d); } // Save float var floatVariable = v as FloatVariable; if (floatVariable != null) { var d = new FloatVar(); d.Key = floatVariable.Key; d.Value = floatVariable.Value; flowchartData.FloatVars.Add(d); } // Save bool var boolVariable = v as BooleanVariable; if (boolVariable != null) { var d = new BoolVar(); d.Key = boolVariable.Key; d.Value = boolVariable.Value; flowchartData.BoolVars.Add(d); } } return(flowchartData); }
/// <summary> /// Decodes a FlowchartData object and uses it to restore the state of a Flowchart in the scene. /// </summary> public static void Decode(FlowchartData flowchartData) { var go = GameObject.Find(flowchartData.FlowchartName); if (go == null) { Debug.LogError("Failed to find flowchart object specified in save data"); return; } var flowchart = go.GetComponent <Flowchart>(); if (flowchart == null) { Debug.LogError("Failed to find flowchart object specified in save data"); return; } for (int i = 0; i < flowchartData.BoolVars.Count; i++) { var boolVar = flowchartData.BoolVars[i]; flowchart.SetBooleanVariable(boolVar.Key, boolVar.Value); } for (int i = 0; i < flowchartData.IntVars.Count; i++) { var intVar = flowchartData.IntVars[i]; flowchart.SetIntegerVariable(intVar.Key, intVar.Value); } for (int i = 0; i < flowchartData.FloatVars.Count; i++) { var floatVar = flowchartData.FloatVars[i]; flowchart.SetFloatVariable(floatVar.Key, floatVar.Value); } for (int i = 0; i < flowchartData.StringVars.Count; i++) { var stringVar = flowchartData.StringVars[i]; flowchart.SetStringVariable(stringVar.Key, stringVar.Value); } }
/// <summary> /// Decodes the loaded list of SaveDataItems to restore the saved game state. /// </summary> public virtual void Decode(List <SaveDataItem> saveDataItems) { for (int i = 0; i < saveDataItems.Count; i++) { var saveDataItem = saveDataItems[i]; if (saveDataItem == null) { continue; } if (saveDataItem.DataType == FlowchartDataKey) { var flowchartData = JsonUtility.FromJson <FlowchartData>(saveDataItem.Data); if (flowchartData == null) { Debug.LogError("Failed to decode Flowchart save data item"); return; } FlowchartData.Decode(flowchartData); } } }