Browse Source

Change #68 Replace Save/Load Globals with Save/Load Variable

Added custom property drawer for Variable properties.
Updated If, Set Variable, etc. to use new property drawer.
Added new Set Save Profile, Save Variable, Load Variable & Delete Save
Key commands.
master
chrisgregan 10 years ago
parent
commit
944ada64a1
  1. 5
      Assets/Fungus/FungusScript/Editor/IfEditor.cs
  2. 48
      Assets/Fungus/FungusScript/Editor/RandomFloatEditor.cs
  3. 48
      Assets/Fungus/FungusScript/Editor/RandomIntegerEditor.cs
  4. 8
      Assets/Fungus/FungusScript/Editor/SetVariableEditor.cs
  5. 47
      Assets/Fungus/FungusScript/Editor/VariableEditor.cs
  6. 50
      Assets/Fungus/FungusScript/Scripts/Commands/DeleteSaveKey.cs
  7. 2
      Assets/Fungus/FungusScript/Scripts/Commands/DeleteSaveKey.cs.meta
  8. 7
      Assets/Fungus/FungusScript/Scripts/Commands/If.cs
  9. 33
      Assets/Fungus/FungusScript/Scripts/Commands/LoadGlobals.cs
  10. 97
      Assets/Fungus/FungusScript/Scripts/Commands/LoadVariable.cs
  11. 0
      Assets/Fungus/FungusScript/Scripts/Commands/LoadVariable.cs.meta
  12. 1
      Assets/Fungus/FungusScript/Scripts/Commands/RandomFloat.cs
  13. 1
      Assets/Fungus/FungusScript/Scripts/Commands/RandomInteger.cs
  14. 33
      Assets/Fungus/FungusScript/Scripts/Commands/SaveGlobals.cs
  15. 99
      Assets/Fungus/FungusScript/Scripts/Commands/SaveVariable.cs
  16. 0
      Assets/Fungus/FungusScript/Scripts/Commands/SaveVariable.cs.meta
  17. 39
      Assets/Fungus/FungusScript/Scripts/Commands/SetSaveProfile.cs
  18. 2
      Assets/Fungus/FungusScript/Scripts/Commands/SetSaveProfile.cs.meta
  19. 4
      Assets/Fungus/FungusScript/Scripts/Commands/SetVariable.cs
  20. 10
      Assets/Fungus/FungusScript/Scripts/Variable.cs

5
Assets/Fungus/FungusScript/Editor/IfEditor.cs

@ -39,10 +39,7 @@ namespace Fungus
return;
}
VariableEditor.VariableField(variableProp,
new GUIContent("Variable", "Variable to use in operation"),
t.GetFungusScript(),
null);
EditorGUILayout.PropertyField(variableProp);
if (variableProp.objectReferenceValue == null)
{

48
Assets/Fungus/FungusScript/Editor/RandomFloatEditor.cs

@ -1,48 +0,0 @@
using UnityEditor;
using UnityEngine;
using System.Collections;
using System.Collections.Generic;
using System.Reflection;
namespace Fungus
{
[CustomEditor (typeof(RandomFloat))]
public class RandomFloatEditor : CommandEditor
{
protected SerializedProperty variableProp;
protected SerializedProperty minValueProp;
protected SerializedProperty maxValueProp;
protected virtual void OnEnable()
{
variableProp = serializedObject.FindProperty("variable");
minValueProp = serializedObject.FindProperty("minValue");
maxValueProp = serializedObject.FindProperty("maxValue");
}
public override void DrawCommandGUI()
{
serializedObject.Update();
RandomFloat t = target as RandomFloat;
FungusScript fungusScript = t.GetFungusScript();
if (fungusScript == null)
{
return;
}
VariableEditor.VariableField(variableProp,
new GUIContent("Variable", "Variable to use in operation"),
t.GetFungusScript(),
(v) => (v.GetType() == typeof(FloatVariable)));
EditorGUILayout.PropertyField(minValueProp);
EditorGUILayout.PropertyField(maxValueProp);
serializedObject.ApplyModifiedProperties();
}
}
}

48
Assets/Fungus/FungusScript/Editor/RandomIntegerEditor.cs

@ -1,48 +0,0 @@
using UnityEditor;
using UnityEngine;
using System.Collections;
using System.Collections.Generic;
using System.Reflection;
namespace Fungus
{
[CustomEditor (typeof(RandomInteger))]
public class RandomIntegerEditor : CommandEditor
{
protected SerializedProperty variableProp;
protected SerializedProperty minValueProp;
protected SerializedProperty maxValueProp;
protected virtual void OnEnable()
{
variableProp = serializedObject.FindProperty("variable");
minValueProp = serializedObject.FindProperty("minValue");
maxValueProp = serializedObject.FindProperty("maxValue");
}
public override void DrawCommandGUI()
{
serializedObject.Update();
RandomInteger t = target as RandomInteger;
FungusScript fungusScript = t.GetFungusScript();
if (fungusScript == null)
{
return;
}
VariableEditor.VariableField(variableProp,
new GUIContent("Variable", "Variable to use in operation"),
t.GetFungusScript(),
(v) => (v.GetType() == typeof(IntegerVariable)));
EditorGUILayout.PropertyField(minValueProp);
EditorGUILayout.PropertyField(maxValueProp);
serializedObject.ApplyModifiedProperties();
}
}
}

8
Assets/Fungus/FungusScript/Editor/SetVariableEditor.cs

@ -38,13 +38,7 @@ namespace Fungus
return;
}
VariableEditor.VariableField(variableProp,
new GUIContent("Set Variable", "Variable to set"),
fungusScript,
(v) => (v.GetType() == typeof(BooleanVariable) ||
v.GetType() == typeof(IntegerVariable) ||
v.GetType() == typeof(FloatVariable) ||
v.GetType() == typeof(StringVariable)));
EditorGUILayout.PropertyField(variableProp);
if (variableProp.objectReferenceValue == null)
{

47
Assets/Fungus/FungusScript/Editor/VariableEditor.cs

@ -32,7 +32,11 @@ namespace Fungus
return null;
}
static public void VariableField(SerializedProperty property, GUIContent label, FungusScript fungusScript, Func<Variable, bool> filter = null)
static public void VariableField(SerializedProperty property,
GUIContent label,
FungusScript fungusScript,
Func<Variable, bool> filter,
Func<string, int, string[], int> drawer = null)
{
List<string> variableKeys = new List<string>();
List<Variable> variableObjects = new List<Variable>();
@ -90,12 +94,49 @@ namespace Fungus
}
}
selectedIndex = EditorGUILayout.Popup(label.text, selectedIndex, variableKeys.ToArray());
if (drawer == null)
{
selectedIndex = EditorGUILayout.Popup(label.text, selectedIndex, variableKeys.ToArray());
}
else
{
selectedIndex = drawer(label.text, selectedIndex, variableKeys.ToArray());
}
property.objectReferenceValue = variableObjects[selectedIndex];
}
}
[CustomPropertyDrawer(typeof(VariablePropertyAttribute))]
public class VariableDrawer : PropertyDrawer
{
public override void OnGUI (Rect position, SerializedProperty property, GUIContent label)
{
VariablePropertyAttribute variableProperty = attribute as VariablePropertyAttribute;
EditorGUI.BeginProperty(position, label, property);
// Filter the variables by the types listed in the VariableProperty attribute
Func<Variable, bool> compare = v =>
{
if (variableProperty.VariableTypes.Length == 0)
{
return true;
}
return variableProperty.VariableTypes.Contains<System.Type>(v.GetType());
};
VariableEditor.VariableField(property,
label,
FungusScriptWindow.GetFungusScript(),
compare,
(s,t,u) => (EditorGUI.Popup(position, s, t, u)));
EditorGUI.EndProperty();
}
}
public class VariableDataDrawer<T> : PropertyDrawer where T : Variable
{

50
Assets/Fungus/FungusScript/Scripts/Commands/DeleteSaveKey.cs

@ -0,0 +1,50 @@
using UnityEngine;
using System;
using System.Collections;
namespace Fungus
{
[CommandInfo("Scripting",
"Delete Save Key",
"Deletes a saved value from permanent storage.")]
[AddComponentMenu("")]
public class DeleteSaveKey : Command
{
[Tooltip("Name of the saved value. Supports variable substition e.g. \"player_{$PlayerNumber}")]
public string key = "";
public override void OnEnter()
{
if (key == "")
{
Continue();
return;
}
FungusScript fungusScript = GetFungusScript();
// Prepend the current save profile (if any)
string prefsKey = SetSaveProfile.saveProfile + "_" + fungusScript.SubstituteVariables(key);
PlayerPrefs.DeleteKey(prefsKey);
Continue();
}
public override string GetSummary()
{
if (key.Length == 0)
{
return "Error: No stored value key selected";
}
return key;
}
public override Color GetButtonColor()
{
return new Color32(235, 191, 217, 255);
}
}
}

2
Assets/Fungus/FungusScript/Editor/RandomFloatEditor.cs.meta → Assets/Fungus/FungusScript/Scripts/Commands/DeleteSaveKey.cs.meta

@ -1,5 +1,5 @@
fileFormatVersion: 2
guid: e1674366d369d428eac4568d9f0dae19
guid: e1205d6641f2146b19d496037f937ba0
MonoImporter:
serializedVersion: 2
defaultReferences: []

7
Assets/Fungus/FungusScript/Scripts/Commands/If.cs

@ -20,7 +20,12 @@ namespace Fungus
[AddComponentMenu("")]
public class If : Command
{
[Tooltip("The variable whos value will be checked")]
[Tooltip("Variable to use in expression")]
[VariableProperty(typeof(BooleanVariable),
typeof(IntegerVariable),
typeof(FloatVariable),
typeof(StringVariable))]
public Variable variable;
[Tooltip("The type of comparison to be performed")]

33
Assets/Fungus/FungusScript/Scripts/Commands/LoadGlobals.cs

@ -1,33 +0,0 @@
using UnityEngine;
using System;
using System.Collections;
namespace Fungus
{
[CommandInfo("Scripting",
"Load Globals",
"Loads a set of global variables previously saved using the Save Globals command.")]
[AddComponentMenu("")]
public class LoadGlobals : Command
{
[Tooltip("Save Name of saved global variable values")]
public string saveName = "";
public override void OnEnter()
{
//GlobalVariables.Load(saveName);
Continue();
}
public override string GetSummary()
{
return saveName;
}
public override Color GetButtonColor()
{
return new Color32(235, 191, 217, 255);
}
}
}

97
Assets/Fungus/FungusScript/Scripts/Commands/LoadVariable.cs

@ -0,0 +1,97 @@
using UnityEngine;
using System;
using System.Collections;
namespace Fungus
{
[CommandInfo("Scripting",
"Load Variable",
"Loads a saved value and stores it in a Boolean, Integer, Float or String variable. If the key is not found then the variable is not modified.")]
[AddComponentMenu("")]
public class LoadVariable : Command
{
[Tooltip("Name of the saved value. Supports variable substition e.g. \"player_{$PlayerNumber}\"")]
public string key = "";
[Tooltip("Variable to store the value in.")]
[VariableProperty(typeof(BooleanVariable),
typeof(IntegerVariable),
typeof(FloatVariable),
typeof(StringVariable))]
public Variable variable;
public override void OnEnter()
{
if (key == "" ||
variable == null)
{
Continue();
return;
}
FungusScript fungusScript = GetFungusScript();
// Prepend the current save profile (if any)
string prefsKey = SetSaveProfile.saveProfile + "_" + fungusScript.SubstituteVariables(key);
System.Type variableType = variable.GetType();
if (variableType == typeof(BooleanVariable))
{
BooleanVariable booleanVariable = variable as BooleanVariable;
if (booleanVariable != null)
{
// PlayerPrefs does not have bool accessors, so just use int
booleanVariable.value = (PlayerPrefs.GetInt(prefsKey) == 1);
}
}
else if (variableType == typeof(IntegerVariable))
{
IntegerVariable integerVariable = variable as IntegerVariable;
if (integerVariable != null)
{
integerVariable.value = PlayerPrefs.GetInt(prefsKey);
}
}
else if (variableType == typeof(FloatVariable))
{
FloatVariable floatVariable = variable as FloatVariable;
if (floatVariable != null)
{
floatVariable.value = PlayerPrefs.GetFloat(prefsKey);
}
}
else if (variableType == typeof(StringVariable))
{
StringVariable stringVariable = variable as StringVariable;
if (stringVariable != null)
{
stringVariable.value = PlayerPrefs.GetString(prefsKey);
}
}
Continue();
}
public override string GetSummary()
{
if (key.Length == 0)
{
return "Error: No stored value key selected";
}
if (variable == null)
{
return "Error: No variable selected";
}
return "'" + key + "' into " + variable.key;
}
public override Color GetButtonColor()
{
return new Color32(235, 191, 217, 255);
}
}
}

0
Assets/Fungus/FungusScript/Scripts/Commands/LoadGlobals.cs.meta → Assets/Fungus/FungusScript/Scripts/Commands/LoadVariable.cs.meta

1
Assets/Fungus/FungusScript/Scripts/Commands/RandomFloat.cs

@ -10,6 +10,7 @@ namespace Fungus
public class RandomFloat : Command
{
[Tooltip("The variable whos value will be set")]
[VariableProperty(typeof(FloatVariable))]
public FloatVariable variable;
[Tooltip("Minimum value for random range")]

1
Assets/Fungus/FungusScript/Scripts/Commands/RandomInteger.cs

@ -10,6 +10,7 @@ namespace Fungus
public class RandomInteger : Command
{
[Tooltip("The variable whos value will be set")]
[VariableProperty(typeof(IntegerVariable))]
public IntegerVariable variable;
[Tooltip("Minimum value for random range")]

33
Assets/Fungus/FungusScript/Scripts/Commands/SaveGlobals.cs

@ -1,33 +0,0 @@
using UnityEngine;
using System;
using System.Collections;
namespace Fungus
{
[CommandInfo("Scripting",
"Save Globals",
"Saves all current global variables to persistent storage. These can be loaded back in again in future using the LoadGlobals command. This provides a basic save game system.")]
[AddComponentMenu("")]
public class SaveGlobals : Command
{
[Tooltip("Save Name of saved global variable values")]
public string saveName = "";
public override void OnEnter()
{
//GlobalVariables.Save(saveName);
Continue();
}
public override string GetSummary()
{
return saveName;
}
public override Color GetButtonColor()
{
return new Color32(235, 191, 217, 255);
}
}
}

99
Assets/Fungus/FungusScript/Scripts/Commands/SaveVariable.cs

@ -0,0 +1,99 @@
using UnityEngine;
using System;
using System.Collections;
namespace Fungus
{
[CommandInfo("Scripting",
"Save Variable",
"Save an Boolean, Integer, Float or String variable to persistent storage using a string key. " +
"The value can be loaded again later using the Load Variable command. You can also " +
"use the Set Save Profile command to manage separate save profiles for multiple players.")]
[AddComponentMenu("")]
public class SaveVariable : Command
{
[Tooltip("Name of the saved value. Supports variable substition e.g. \"player_{$PlayerNumber}")]
public string key = "";
[Tooltip("Variable to read the value from. Only Boolean, Integer, Float and String are supported.")]
[VariableProperty(typeof(BooleanVariable),
typeof(IntegerVariable),
typeof(FloatVariable),
typeof(StringVariable))]
public Variable variable;
public override void OnEnter()
{
if (key == "" ||
variable == null)
{
Continue();
return;
}
FungusScript fungusScript = GetFungusScript();
// Prepend the current save profile (if any)
string prefsKey = SetSaveProfile.saveProfile + "_" + fungusScript.SubstituteVariables(key);
System.Type variableType = variable.GetType();
if (variableType == typeof(BooleanVariable))
{
BooleanVariable booleanVariable = variable as BooleanVariable;
if (booleanVariable != null)
{
// PlayerPrefs does not have bool accessors, so just use int
PlayerPrefs.SetInt(prefsKey, booleanVariable.value ? 1 : 0);
}
}
else if (variableType == typeof(IntegerVariable))
{
IntegerVariable integerVariable = variable as IntegerVariable;
if (integerVariable != null)
{
PlayerPrefs.SetInt(prefsKey, integerVariable.value);
}
}
else if (variableType == typeof(FloatVariable))
{
FloatVariable floatVariable = variable as FloatVariable;
if (floatVariable != null)
{
PlayerPrefs.SetFloat(prefsKey, floatVariable.value);
}
}
else if (variableType == typeof(StringVariable))
{
StringVariable stringVariable = variable as StringVariable;
if (stringVariable != null)
{
PlayerPrefs.SetString(prefsKey, stringVariable.value);
}
}
Continue();
}
public override string GetSummary()
{
if (key.Length == 0)
{
return "Error: No stored value key selected";
}
if (variable == null)
{
return "Error: No variable selected";
}
return variable.key + " into '" + key + "'";
}
public override Color GetButtonColor()
{
return new Color32(235, 191, 217, 255);
}
}
}

0
Assets/Fungus/FungusScript/Scripts/Commands/SaveGlobals.cs.meta → Assets/Fungus/FungusScript/Scripts/Commands/SaveVariable.cs.meta

39
Assets/Fungus/FungusScript/Scripts/Commands/SetSaveProfile.cs

@ -0,0 +1,39 @@
using UnityEngine;
using System;
using System.Collections;
namespace Fungus
{
[CommandInfo("Scripting",
"Set Save Profile",
"Sets the active profile that the Save Variable and Load Variable commands will use. This is useful to crete multiple player save games. Once set, the profile applies across all Fungus Scripts and will also persist across scene loads.")]
[AddComponentMenu("")]
public class SetSaveProfile : Command
{
/**
* Shared save profile name used by SaveVariable and LoadVariable.
*/
public static string saveProfile = "";
[Tooltip("Name of save profile to make active.")]
public string saveProfileName = "";
public override void OnEnter()
{
saveProfile = saveProfileName;
Continue();
}
public override string GetSummary()
{
return saveProfileName;
}
public override Color GetButtonColor()
{
return new Color32(235, 191, 217, 255);
}
}
}

2
Assets/Fungus/FungusScript/Editor/RandomIntegerEditor.cs.meta → Assets/Fungus/FungusScript/Scripts/Commands/SetSaveProfile.cs.meta

@ -1,5 +1,5 @@
fileFormatVersion: 2
guid: 61c5d800d9a7e4e18864b9c00b0b2fe9
guid: 9a240aa44597844f89e6d1ba15a96b04
MonoImporter:
serializedVersion: 2
defaultReferences: []

4
Assets/Fungus/FungusScript/Scripts/Commands/SetVariable.cs

@ -20,6 +20,10 @@ namespace Fungus
}
[Tooltip("The variable whos value will be set")]
[VariableProperty(typeof(BooleanVariable),
typeof(IntegerVariable),
typeof(FloatVariable),
typeof(StringVariable))]
public Variable variable;
[Tooltip("The type of math operation to be performed")]

10
Assets/Fungus/FungusScript/Scripts/Variable.cs

@ -25,6 +25,16 @@ namespace Fungus
public int Order { get; set; }
}
public class VariablePropertyAttribute : PropertyAttribute
{
public VariablePropertyAttribute (params System.Type[] variableTypes)
{
this.VariableTypes = variableTypes;
}
public System.Type[] VariableTypes { get; set; }
}
[RequireComponent(typeof(FungusScript))]
public abstract class Variable : MonoBehaviour
{

Loading…
Cancel
Save