diff --git a/Assets/Fungus/Editor/FungusScript/AddOptionEditor.cs b/Assets/Fungus/Editor/FungusScript/AddOptionEditor.cs index a2107d43..3a4fb362 100644 --- a/Assets/Fungus/Editor/FungusScript/AddOptionEditor.cs +++ b/Assets/Fungus/Editor/FungusScript/AddOptionEditor.cs @@ -20,17 +20,15 @@ namespace Fungus.Script Sequence targetSequence = SequenceEditor.SequenceField(new GUIContent("Target Sequence", "Sequence to execute when option is selected"), t.GetFungusScript(), t.targetSequence); AddOption.ShowCondition showCondition = (AddOption.ShowCondition)EditorGUILayout.EnumPopup(new GUIContent("Show Condition", "Condition when this option should be visible."), t.showCondition); - string booleanVariableKey = t.booleanVariableKey; + BooleanVariable booleanVariable = t.booleanVariable; if (showCondition == AddOption.ShowCondition.BooleanIsFalse || showCondition == AddOption.ShowCondition.BooleanIsTrue) { - VariableType type = VariableType.Boolean; - booleanVariableKey = SequenceEditor.VariableField (new GUIContent ("Boolean Variable", "Boolean variable to test for condition"), - t.GetFungusScript (), - t.booleanVariableKey, - ref type, - v => { return v.type == VariableType.Boolean; }); + booleanVariable = SequenceEditor.VariableField (new GUIContent ("Boolean Variable", "Boolean variable to test for condition"), + t.GetFungusScript (), + t.booleanVariable, + v => { return v.GetType() == typeof(BooleanVariable); }) as BooleanVariable; } if (EditorGUI.EndChangeCheck()) @@ -39,7 +37,7 @@ namespace Fungus.Script t.optionText = optionText; t.targetSequence = targetSequence; t.showCondition = showCondition; - t.booleanVariableKey = booleanVariableKey; + t.booleanVariable = booleanVariable; } } } diff --git a/Assets/Fungus/Editor/FungusScript/FungusScriptEditor.cs b/Assets/Fungus/Editor/FungusScript/FungusScriptEditor.cs index fcb39251..28b49ba4 100644 --- a/Assets/Fungus/Editor/FungusScript/FungusScriptEditor.cs +++ b/Assets/Fungus/Editor/FungusScript/FungusScriptEditor.cs @@ -3,12 +3,12 @@ using UnityEngine; using System.Collections; using System.Collections.Generic; using Fungus; -using Rotorz.ReorderableList; -using System.Linq; +//using Rotorz.ReorderableList; +//using System.Linq; namespace Fungus.Script { - + /* [CustomPropertyDrawer (typeof(Variable))] public class VariableDrawer : PropertyDrawer { @@ -56,20 +56,21 @@ namespace Fungus.Script EditorGUI.EndProperty(); } } + */ [CustomEditor (typeof(FungusScript))] public class FungusScriptEditor : Editor { SerializedProperty variablesProperty; - void OnEnable() - { - variablesProperty = serializedObject.FindProperty("variables"); - } + //void OnEnable() + //{ + // variablesProperty = serializedObject.FindProperty("variables"); + //} public override void OnInspectorGUI() { - serializedObject.Update(); + //serializedObject.Update(); FungusScript t = target as FungusScript; @@ -111,10 +112,10 @@ namespace Fungus.Script EditorGUILayout.LabelField(new GUIContent("Error: Please select a Start Sequence"), style); } - ReorderableListGUI.Title("Variables"); - ReorderableListGUI.ListField(variablesProperty); + //ReorderableListGUI.Title("Variables"); + //ReorderableListGUI.ListField(variablesProperty); - serializedObject.ApplyModifiedProperties(); + //serializedObject.ApplyModifiedProperties(); } } diff --git a/Assets/Fungus/Editor/FungusScript/JumpEditor.cs b/Assets/Fungus/Editor/FungusScript/JumpEditor.cs index 6c8afbd6..70cee6c2 100644 --- a/Assets/Fungus/Editor/FungusScript/JumpEditor.cs +++ b/Assets/Fungus/Editor/FungusScript/JumpEditor.cs @@ -36,43 +36,28 @@ namespace Fungus.Script } return; } - - List keys = new List(); - keys.Add(""); - int index = 0; - for (int i = 0; i < sc.variables.Count; ++i) - { - Variable v = sc.variables[i]; - keys.Add(v.key); - if (v.key == t.variableKey && - index == 0) - { - index = i + 1; - } - } - - int newIndex = EditorGUILayout.Popup("Compare Variable", index, keys.ToArray()); - bool keyChanged = (t.variableKey != keys[newIndex]); + FungusVariable fungusVariable = SequenceEditor.VariableField(new GUIContent("Compare Variable", "Variable to use in compare operation"), + t.GetFungusScript(), + t.variable, + null); - if (keyChanged) + if (fungusVariable != t.variable) { Undo.RecordObject(t, "Select variable"); - t.variableKey = keys[newIndex]; + t.variable = fungusVariable; } - if (t.variableKey == "") + if (t.variable == null) { return; } - VariableType variableType = sc.variables[newIndex - 1].type; - List operatorList = new List(); operatorList.Add(new GUIContent("==")); operatorList.Add(new GUIContent("!=")); - if (variableType == VariableType.Integer || - variableType == VariableType.Float) + if (t.variable.GetType() == typeof(IntegerVariable) || + t.variable.GetType() == typeof(FloatVariable)) { operatorList.Add(new GUIContent("<")); operatorList.Add(new GUIContent(">")); @@ -95,20 +80,21 @@ namespace Fungus.Script float floatValue = t.floatData.value; string stringValue = t.stringData.value; - switch (variableType) + if (t.variable.GetType() == typeof(BooleanVariable)) { - case VariableType.Boolean: booleanValue = EditorGUILayout.Toggle(new GUIContent("Boolean Value", "The boolean value to set the variable with"), booleanValue); - break; - case VariableType.Integer: + } + else if (t.variable.GetType() == typeof(IntegerVariable)) + { integerValue = EditorGUILayout.IntField(new GUIContent("Integer Value", "The integer value to set the variable with"), integerValue); - break; - case VariableType.Float: + } + else if (t.variable.GetType() == typeof(FloatVariable)) + { floatValue = EditorGUILayout.FloatField(new GUIContent("Float Value", "The float value to set the variable with"), floatValue); - break; - case VariableType.String: + } + else if (t.variable.GetType() == typeof(StringVariable)) + { stringValue = EditorGUILayout.TextField(new GUIContent("String Value", "The string value to set the variable with"), stringValue); - break; } if (booleanValue != t.booleanData.value) diff --git a/Assets/Fungus/Editor/FungusScript/SayEditor.cs b/Assets/Fungus/Editor/FungusScript/SayEditor.cs index 44510692..408b8def 100644 --- a/Assets/Fungus/Editor/FungusScript/SayEditor.cs +++ b/Assets/Fungus/Editor/FungusScript/SayEditor.cs @@ -24,17 +24,15 @@ namespace Fungus.Script Say.ShowCondition showCondition = (Say.ShowCondition)EditorGUILayout.EnumPopup(new GUIContent("Show Condition", "Condition when this say text should be visible."), t.showCondition); - string booleanVariableKey = t.booleanVariableKey; + BooleanVariable booleanVariable = t.booleanVariable; if (showCondition == Say.ShowCondition.BooleanIsFalse || showCondition == Say.ShowCondition.BooleanIsTrue) { - VariableType type = VariableType.Boolean; - booleanVariableKey = SequenceEditor.VariableField (new GUIContent ("Boolean Variable", "Boolean variable to test for condition"), - t.GetFungusScript (), - t.booleanVariableKey, - ref type, - v => { return v.type == VariableType.Boolean; }); + booleanVariable = SequenceEditor.VariableField (new GUIContent ("Boolean Variable", "Boolean variable to test for condition"), + t.GetFungusScript (), + t.booleanVariable, + v => { return v.GetType() == typeof(BooleanVariable); }) as BooleanVariable; } if (EditorGUI.EndChangeCheck()) @@ -43,7 +41,7 @@ namespace Fungus.Script t.character = character; t.text = text; t.showCondition = showCondition; - t.booleanVariableKey = booleanVariableKey; + t.booleanVariable = booleanVariable; } } diff --git a/Assets/Fungus/Editor/FungusScript/SequenceEditor.cs b/Assets/Fungus/Editor/FungusScript/SequenceEditor.cs index 15c373c0..8cb74e4e 100644 --- a/Assets/Fungus/Editor/FungusScript/SequenceEditor.cs +++ b/Assets/Fungus/Editor/FungusScript/SequenceEditor.cs @@ -3,6 +3,7 @@ using UnityEngine; using System; using System.Collections; using System.Collections.Generic; +using System.Linq; namespace Fungus.Script { @@ -48,15 +49,19 @@ namespace Fungus.Script return result; } - static public string VariableField(GUIContent label, FungusScript fungusScript, string variableKey, ref VariableType variableType, Func filter = null) + static public FungusVariable VariableField(GUIContent label, FungusScript fungusScript, FungusVariable variable, Func filter = null) { - List keys = new List(); - keys.Add(""); + List variableKeys = new List(); + List variableObjects = new List(); + + variableKeys.Add(""); + variableObjects.Add(null); + + FungusVariable[] variables = fungusScript.GetComponents(); int index = 0; - for (int i = 0; i < fungusScript.variables.Count; ++i) + int selectedIndex = 0; + foreach (FungusVariable v in variables) { - Variable v = fungusScript.variables[i]; - if (filter != null) { if (!filter(v)) @@ -65,22 +70,20 @@ namespace Fungus.Script } } - keys.Add(v.key); - if (v.key == variableKey && - index == 0) + variableKeys.Add(v.key); + variableObjects.Add(v); + + index++; + + if (v == variable) { - index = i + 1; + selectedIndex = index; } } - - int newIndex = EditorGUILayout.Popup(label.text, index, keys.ToArray()); - - if (newIndex > 0) - { - variableType = fungusScript.variables[newIndex - 1].type; - } - - return keys[newIndex]; + + selectedIndex = EditorGUILayout.Popup(label.text, selectedIndex, variableKeys.ToArray()); + + return variableObjects[selectedIndex]; } } diff --git a/Assets/Fungus/Editor/FungusScript/SetEditor.cs b/Assets/Fungus/Editor/FungusScript/SetEditor.cs index d3604e0f..a6529b88 100644 --- a/Assets/Fungus/Editor/FungusScript/SetEditor.cs +++ b/Assets/Fungus/Editor/FungusScript/SetEditor.cs @@ -19,37 +19,34 @@ namespace Fungus.Script return; } - VariableType variableType = VariableType.Boolean; - string variableKey = SequenceEditor.VariableField(new GUIContent("Variable", "Variable to set"), - fungusScript, - t.variableKey, - ref variableType); - - - if (variableKey != t.variableKey) + FungusVariable variable = SequenceEditor.VariableField(new GUIContent("Variable", "Variable to set"), + fungusScript, + t.variable); + + if (variable != t.variable) { Undo.RecordObject(t, "Set Variable Key"); - t.variableKey = variableKey; + t.variable = variable; } - if (t.variableKey == "") + if (t.variable == null) { return; } List operatorsList = new List(); operatorsList.Add(new GUIContent("=")); - if (variableType == VariableType.Boolean) + if (variable.GetType() == typeof(BooleanVariable)) { operatorsList.Add(new GUIContent("!")); } - else if (variableType == VariableType.Integer || - variableType == VariableType.Float) + else if (variable.GetType() == typeof(IntegerVariable) || + variable.GetType() == typeof(FloatVariable)) { - operatorsList.Add(new GUIContent("+")); - operatorsList.Add(new GUIContent("-")); - operatorsList.Add(new GUIContent("*")); - operatorsList.Add(new GUIContent("/")); + operatorsList.Add(new GUIContent("+")); + operatorsList.Add(new GUIContent("-")); + operatorsList.Add(new GUIContent("*")); + operatorsList.Add(new GUIContent("/")); } int selectedIndex = 0; @@ -79,11 +76,9 @@ namespace Fungus.Script selectedIndex = EditorGUILayout.Popup(new GUIContent("Operator", "Arithmetic operator to use"), selectedIndex, operatorsList.ToArray()); Set.SetOperator setOperator = Set.SetOperator.Assign; - switch (variableType) + if (variable.GetType() == typeof(BooleanVariable) || + variable.GetType() == typeof(StringVariable)) { - default: - case VariableType.Boolean: - case VariableType.String: switch (selectedIndex) { default: @@ -94,9 +89,10 @@ namespace Fungus.Script setOperator = Set.SetOperator.Negate; break; } - break; - case VariableType.Integer: - case VariableType.Float: + } + else if (variable.GetType() == typeof(IntegerVariable) || + variable.GetType() == typeof(FloatVariable)) + { switch (selectedIndex) { default: @@ -116,7 +112,6 @@ namespace Fungus.Script setOperator = Set.SetOperator.Divide; break; } - break; } if (setOperator != t.setOperator) @@ -130,20 +125,21 @@ namespace Fungus.Script float floatValue = t.floatData.value; string stringValue = t.stringData.value; - switch (variableType) + if (variable.GetType() == typeof(BooleanVariable)) { - case VariableType.Boolean: booleanValue = EditorGUILayout.Toggle(new GUIContent("Boolean Value", "The boolean value to set the variable with"), booleanValue); - break; - case VariableType.Integer: + } + else if (variable.GetType() == typeof(IntegerVariable)) + { integerValue = EditorGUILayout.IntField(new GUIContent("Integer Value", "The integer value to set the variable with"), integerValue); - break; - case VariableType.Float: + } + else if (variable.GetType() == typeof(FloatVariable)) + { floatValue = EditorGUILayout.FloatField(new GUIContent("Float Value", "The float value to set the variable with"), floatValue); - break; - case VariableType.String: + } + else if (variable.GetType() == typeof(StringVariable)) + { stringValue = EditorGUILayout.TextField(new GUIContent("String Value", "The string value to set the variable with"), stringValue); - break; } if (booleanValue != t.booleanData.value) diff --git a/Assets/Fungus/Editor/FungusScript/VariablesWindow.cs b/Assets/Fungus/Editor/FungusScript/VariablesWindow.cs index 3177a839..1ca3e099 100644 --- a/Assets/Fungus/Editor/FungusScript/VariablesWindow.cs +++ b/Assets/Fungus/Editor/FungusScript/VariablesWindow.cs @@ -27,24 +27,25 @@ namespace Fungus.Script } // Warn about conflicting global variable types - Dictionary globalTypes = new Dictionary(); + Dictionary globals = new Dictionary(); FungusScript[] fungusScripts = GameObject.FindObjectsOfType(); foreach (FungusScript fs in fungusScripts) { - foreach (Variable v in fs.variables) + FungusVariable[] variables = fs.GetComponents(); + foreach (FungusVariable v in variables) { if (v.scope == VariableScope.Global) { - if (globalTypes.ContainsKey(v.key)) + if (globals.ContainsKey(v.key)) { - if (globalTypes[v.key] != v.type) + if (globals[v.key].GetType() != v.GetType()) { GUIStyle errorStyle = new GUIStyle(GUI.skin.label); errorStyle.normal.textColor = new Color(1,0,0); GUILayout.Label("Error: Global '" + v.key + "' must use the same type in all scripts.", errorStyle); } } - globalTypes[v.key] = v.type; + globals[v.key] = v; } } } @@ -71,7 +72,8 @@ namespace Fungus.Script boxStyle.margin.top = 0; boxStyle.margin.bottom = 0; - foreach (Variable variable in fungusScript.variables) + FungusVariable[] fsVariables = fungusScript.GetComponents(); + foreach (FungusVariable variable in fsVariables) { GUILayout.BeginHorizontal(boxStyle); @@ -91,33 +93,31 @@ namespace Fungus.Script break; } - switch (variable.type) + if (variable.GetType() == typeof(BooleanVariable)) { - case VariableType.Boolean: typeString = "Boolean"; - valueString = variable.BooleanValue ? "True" : "False"; - break; - case VariableType.Integer: + valueString = (variable as BooleanVariable).Value ? "True" : "False"; + } + else if (variable.GetType() == typeof(IntegerVariable)) + { typeString = "Integer"; - valueString = variable.IntegerValue.ToString(); - break; - case VariableType.Float: + valueString = (variable as IntegerVariable).Value.ToString(); + } + else if (variable.GetType() == typeof(FloatVariable)) + { typeString = "Float"; - valueString = variable.FloatValue.ToString(); - break; - case VariableType.String: + valueString = (variable as FloatVariable).Value.ToString(); + } + else if (variable.GetType() == typeof(StringVariable)) + { typeString = "String"; + valueString = (variable as StringVariable).Value; - if (variable.StringValue == null || - variable.StringValue.Length == 0) + if (valueString == null || + valueString.Length == 0) { valueString = "\"\""; } - else - { - valueString = variable.StringValue; - } - break; } GUILayout.Label(keyString, GUILayout.Width(columnWidth)); diff --git a/Assets/Fungus/VisualScripting/AddOption.cs b/Assets/Fungus/VisualScripting/AddOption.cs index f9a40566..d1d5c5bb 100644 --- a/Assets/Fungus/VisualScripting/AddOption.cs +++ b/Assets/Fungus/VisualScripting/AddOption.cs @@ -18,7 +18,7 @@ namespace Fungus.Script public string optionText; public Sequence targetSequence; public ShowCondition showCondition; - public string booleanVariableKey; + public BooleanVariable booleanVariable; public override void OnEnter() { @@ -39,20 +39,19 @@ namespace Fungus.Script } else { - Variable v = parentFungusScript.GetVariable(booleanVariableKey); - if (v == null) + if (booleanVariable == null) { showOption = false; } else { if (showCondition == ShowCondition.BooleanIsTrue && - v.BooleanValue != true) + booleanVariable.Value != true) { showOption = false; } else if (showCondition == ShowCondition.BooleanIsFalse && - v.BooleanValue != false) + booleanVariable.Value != false) { showOption = false; } diff --git a/Assets/Fungus/VisualScripting/BooleanVariable.cs b/Assets/Fungus/VisualScripting/BooleanVariable.cs new file mode 100644 index 00000000..2c5de2be --- /dev/null +++ b/Assets/Fungus/VisualScripting/BooleanVariable.cs @@ -0,0 +1,18 @@ +using UnityEngine; +using System.Collections; + +namespace Fungus.Script +{ + + public class BooleanVariable : FungusVariable + { + bool booleanValue; + + public bool Value + { + get { return (scope == VariableScope.Local) ? booleanValue : Variables.GetBoolean(key); } + set { if (scope == VariableScope.Local) { booleanValue = value; } else { Variables.SetBoolean(key, value); } } + } + } + +} \ No newline at end of file diff --git a/Assets/Fungus/VisualScripting/BooleanVariable.cs.meta b/Assets/Fungus/VisualScripting/BooleanVariable.cs.meta new file mode 100644 index 00000000..c32e891e --- /dev/null +++ b/Assets/Fungus/VisualScripting/BooleanVariable.cs.meta @@ -0,0 +1,8 @@ +fileFormatVersion: 2 +guid: 5d02d9822eec54c98afe95bb497211b3 +MonoImporter: + serializedVersion: 2 + defaultReferences: [] + executionOrder: 0 + icon: {instanceID: 0} + userData: diff --git a/Assets/Fungus/VisualScripting/FloatVariable.cs b/Assets/Fungus/VisualScripting/FloatVariable.cs new file mode 100644 index 00000000..8782690c --- /dev/null +++ b/Assets/Fungus/VisualScripting/FloatVariable.cs @@ -0,0 +1,18 @@ +using UnityEngine; +using System.Collections; + +namespace Fungus.Script +{ + + public class FloatVariable : FungusVariable + { + float floatValue; + + public float Value + { + get { return (scope == VariableScope.Local) ? floatValue : Variables.GetFloat(key); } + set { if (scope == VariableScope.Local) { floatValue = value; } else { Variables.SetFloat(key, value); } } + } + } + +} \ No newline at end of file diff --git a/Assets/Fungus/VisualScripting/FloatVariable.cs.meta b/Assets/Fungus/VisualScripting/FloatVariable.cs.meta new file mode 100644 index 00000000..a229b440 --- /dev/null +++ b/Assets/Fungus/VisualScripting/FloatVariable.cs.meta @@ -0,0 +1,8 @@ +fileFormatVersion: 2 +guid: 705fa1ac97df74e3a84ff952ffd923f1 +MonoImporter: + serializedVersion: 2 + defaultReferences: [] + executionOrder: 0 + icon: {instanceID: 0} + userData: diff --git a/Assets/Fungus/VisualScripting/FungusScript.cs b/Assets/Fungus/VisualScripting/FungusScript.cs index 2b59ad52..9a23ec4a 100644 --- a/Assets/Fungus/VisualScripting/FungusScript.cs +++ b/Assets/Fungus/VisualScripting/FungusScript.cs @@ -18,8 +18,6 @@ namespace Fungus.Script [System.NonSerialized] public Sequence activeSequence; - public List variables = new List(); - public bool startAutomatically = false; void Start() @@ -54,18 +52,6 @@ namespace Fungus.Script activeSequence = sequence; sequence.ExecuteNextCommand(); } - - public Variable GetVariable(string key) - { - foreach (Variable v in variables) - { - if (v.key == key) - { - return v; - } - } - return null; - } } } \ No newline at end of file diff --git a/Assets/Fungus/VisualScripting/IntegerVariable.cs b/Assets/Fungus/VisualScripting/IntegerVariable.cs new file mode 100644 index 00000000..f4aab4a8 --- /dev/null +++ b/Assets/Fungus/VisualScripting/IntegerVariable.cs @@ -0,0 +1,18 @@ +using UnityEngine; +using System.Collections; + +namespace Fungus.Script +{ + + public class IntegerVariable : FungusVariable + { + int integerValue; + + public int Value + { + get { return (scope == VariableScope.Local) ? integerValue : Variables.GetInteger(key); } + set { if (scope == VariableScope.Local) { integerValue = value; } else { Variables.SetInteger(key, value); } } + } + } + +} \ No newline at end of file diff --git a/Assets/Fungus/VisualScripting/IntegerVariable.cs.meta b/Assets/Fungus/VisualScripting/IntegerVariable.cs.meta new file mode 100644 index 00000000..300d977e --- /dev/null +++ b/Assets/Fungus/VisualScripting/IntegerVariable.cs.meta @@ -0,0 +1,8 @@ +fileFormatVersion: 2 +guid: afb91b566ceda411bad1e9d3c3243ecc +MonoImporter: + serializedVersion: 2 + defaultReferences: [] + executionOrder: 0 + icon: {instanceID: 0} + userData: diff --git a/Assets/Fungus/VisualScripting/Jump.cs b/Assets/Fungus/VisualScripting/Jump.cs index c9ed145f..f6ae3b85 100644 --- a/Assets/Fungus/VisualScripting/Jump.cs +++ b/Assets/Fungus/VisualScripting/Jump.cs @@ -26,7 +26,7 @@ namespace Fungus.Script public Sequence targetSequence; // Only used for Always condition - public string variableKey; + public FungusVariable variable; public CompareOperator compareOperator; @@ -51,82 +51,87 @@ namespace Fungus.Script return; } - Variable v = parentFungusScript.GetVariable(variableKey); - bool condition = false; - switch (v.type) + if (variable == null) + { + Continue(); + return; + } + + if (variable.GetType() == typeof(BooleanVariable)) { - case VariableType.Boolean: switch (compareOperator) { case CompareOperator.Equals: - condition = (v.BooleanValue == booleanData.value); + condition = (variable as BooleanVariable).Value == booleanData.value; break; case CompareOperator.NotEquals: default: - condition = (v.BooleanValue != booleanData.value); + condition = (variable as BooleanVariable).Value != booleanData.value; break; } - break; - case VariableType.Integer: + } + else if (variable.GetType() == typeof(IntegerVariable)) + { switch (compareOperator) { case CompareOperator.Equals: - condition = (v.IntegerValue == integerData.value); + condition = (variable as IntegerVariable).Value == integerData.value; break; case CompareOperator.NotEquals: - condition = (v.IntegerValue != integerData.value); + condition = (variable as IntegerVariable).Value != integerData.value; break; case CompareOperator.LessThan: - condition = (v.IntegerValue < integerData.value); + condition = (variable as IntegerVariable).Value < integerData.value; break; case CompareOperator.GreaterThan: - condition = (v.IntegerValue > integerData.value); + condition = (variable as IntegerVariable).Value > integerData.value; break; case CompareOperator.LessThanOrEquals: - condition = (v.IntegerValue <= integerData.value); + condition = (variable as IntegerVariable).Value <= integerData.value; break; case CompareOperator.GreaterThanOrEquals: - condition = (v.IntegerValue >= integerData.value); + condition = (variable as IntegerVariable).Value >= integerData.value; break; } - break; - case VariableType.Float: + } + else if (variable.GetType() == typeof(FloatVariable)) + { switch (compareOperator) { case CompareOperator.Equals: - condition = (v.FloatValue == floatData.value); + condition = (variable as FloatVariable).Value == floatData.value; break; case CompareOperator.NotEquals: - condition = (v.FloatValue != floatData.value); + condition = (variable as FloatVariable).Value != floatData.value; break; case CompareOperator.LessThan: - condition = (v.FloatValue < floatData.value); + condition = (variable as FloatVariable).Value < floatData.value; break; case CompareOperator.GreaterThan: - condition = (v.FloatValue > floatData.value); + condition = (variable as FloatVariable).Value > floatData.value; break; case CompareOperator.LessThanOrEquals: - condition = (v.FloatValue <= floatData.value); + condition = (variable as FloatVariable).Value <= floatData.value; break; case CompareOperator.GreaterThanOrEquals: - condition = (v.FloatValue >= floatData.value); + condition = (variable as FloatVariable).Value >= floatData.value; break; } - break; - case VariableType.String: + } + else if (variable.GetType() == typeof(StringVariable)) + { switch (compareOperator) { case CompareOperator.Equals: - condition = (v.StringValue == stringData.value); + condition = (variable as StringVariable).Value == stringData.value; break; case CompareOperator.NotEquals: default: - condition = (v.StringValue != stringData.value); + condition = (variable as StringVariable).Value != stringData.value; break; } - break; } if (condition) diff --git a/Assets/Fungus/VisualScripting/Say.cs b/Assets/Fungus/VisualScripting/Say.cs index 0e34b2bc..28e14c21 100644 --- a/Assets/Fungus/VisualScripting/Say.cs +++ b/Assets/Fungus/VisualScripting/Say.cs @@ -18,7 +18,7 @@ namespace Fungus.Script public string character; public string text; public ShowCondition showCondition; - public string booleanVariableKey; + public BooleanVariable booleanVariable; int executionCount; @@ -46,20 +46,19 @@ namespace Fungus.Script } else { - Variable v = parentFungusScript.GetVariable(booleanVariableKey); - if (v == null) + if (booleanVariable == null) { showSayText = false; } else { if (showCondition == ShowCondition.BooleanIsTrue && - v.BooleanValue != true) + booleanVariable.Value != true) { showSayText = false; } else if (showCondition == ShowCondition.BooleanIsFalse && - v.BooleanValue != false) + booleanVariable.Value != false) { showSayText = false; } diff --git a/Assets/Fungus/VisualScripting/Set.cs b/Assets/Fungus/VisualScripting/Set.cs index 7fed6d32..8f0f90ae 100644 --- a/Assets/Fungus/VisualScripting/Set.cs +++ b/Assets/Fungus/VisualScripting/Set.cs @@ -16,7 +16,7 @@ namespace Fungus.Script Divide // / } - public string variableKey; + public FungusVariable variable; public SetOperator setOperator; @@ -30,89 +30,82 @@ namespace Fungus.Script public override void OnEnter() { - if (variableKey.Length == 0) + if (variable == null) { Continue(); return; } - Variable v = parentFungusScript.GetVariable(variableKey); - if (v == null) + if (variable.GetType() == typeof(BooleanVariable)) { - Debug.LogError("Variable " + variableKey + " not defined."); + switch (setOperator) + { + default: + case SetOperator.Assign: + (variable as BooleanVariable).Value = booleanData.value; + break; + case SetOperator.Negate: + (variable as BooleanVariable).Value = !booleanData.value; + break; + } } - else + else if (variable.GetType() == typeof(IntegerVariable)) { - switch (v.type) + switch (setOperator) { - case VariableType.Boolean: - switch (setOperator) - { - default: - case SetOperator.Assign: - v.BooleanValue = booleanData.value; - break; - case SetOperator.Negate: - v.BooleanValue = !booleanData.value; - break; - } + default: + case SetOperator.Assign: + (variable as IntegerVariable).Value = integerData.value; + break; + case SetOperator.Negate: + (variable as IntegerVariable).Value = -integerData.value; + break; + case SetOperator.Add: + (variable as IntegerVariable).Value += integerData.value; break; - case VariableType.Integer: - switch (setOperator) - { - default: - case SetOperator.Assign: - v.IntegerValue = integerData.value; - break; - case SetOperator.Negate: - v.IntegerValue = -integerData.value; - break; - case SetOperator.Add: - v.IntegerValue += integerData.value; - break; - case SetOperator.Subtract: - v.IntegerValue -= integerData.value; - break; - case SetOperator.Multiply: - v.IntegerValue *= integerData.value; - break; - case SetOperator.Divide: - v.IntegerValue /= integerData.value; - break; - } + case SetOperator.Subtract: + (variable as IntegerVariable).Value -= integerData.value; break; - case VariableType.Float: - switch (setOperator) - { - default: - case SetOperator.Assign: - v.FloatValue = floatData.value; - break; - case SetOperator.Negate: - v.FloatValue = -floatData.value; - break; - case SetOperator.Add: - v.FloatValue += floatData.value; - break; - case SetOperator.Subtract: - v.FloatValue -= floatData.value; - break; - case SetOperator.Multiply: - v.FloatValue *= floatData.value; - break; - case SetOperator.Divide: - v.FloatValue /= floatData.value; - break; - } + case SetOperator.Multiply: + (variable as IntegerVariable).Value *= integerData.value; break; - case VariableType.String: - switch (setOperator) - { - default: - case SetOperator.Assign: - v.StringValue = stringData.value; - break; - } + case SetOperator.Divide: + (variable as IntegerVariable).Value /= integerData.value; + break; + } + } + else if (variable.GetType() == typeof(FloatVariable)) + { + switch (setOperator) + { + default: + case SetOperator.Assign: + (variable as FloatVariable).Value = floatData.value; + break; + case SetOperator.Negate: + (variable as FloatVariable).Value = -floatData.value; + break; + case SetOperator.Add: + (variable as FloatVariable).Value += floatData.value; + break; + case SetOperator.Subtract: + (variable as FloatVariable).Value -= floatData.value; + break; + case SetOperator.Multiply: + (variable as FloatVariable).Value *= floatData.value; + break; + case SetOperator.Divide: + (variable as FloatVariable).Value /= floatData.value; + break; + } + } + else if (variable.GetType() == typeof(StringVariable)) + { + switch (setOperator) + { + default: + case SetOperator.Assign: + (variable as StringVariable).Value = stringData.value; break; } } diff --git a/Assets/Fungus/VisualScripting/StringVariable.cs b/Assets/Fungus/VisualScripting/StringVariable.cs new file mode 100644 index 00000000..a6395bee --- /dev/null +++ b/Assets/Fungus/VisualScripting/StringVariable.cs @@ -0,0 +1,18 @@ +using UnityEngine; +using System.Collections; + +namespace Fungus.Script +{ + + public class StringVariable : FungusVariable + { + string stringValue; + + public string Value + { + get { return (scope == VariableScope.Local) ? stringValue : Variables.GetString(key); } + set { if (scope == VariableScope.Local) { stringValue = value; } else { Variables.SetString(key, value); } } + } + } + +} \ No newline at end of file diff --git a/Assets/Fungus/VisualScripting/StringVariable.cs.meta b/Assets/Fungus/VisualScripting/StringVariable.cs.meta new file mode 100644 index 00000000..5f2e5795 --- /dev/null +++ b/Assets/Fungus/VisualScripting/StringVariable.cs.meta @@ -0,0 +1,8 @@ +fileFormatVersion: 2 +guid: 4580f28dd8581476b810b38eea2f1316 +MonoImporter: + serializedVersion: 2 + defaultReferences: [] + executionOrder: 0 + icon: {instanceID: 0} + userData: diff --git a/Assets/Fungus/VisualScripting/Variable.cs b/Assets/Fungus/VisualScripting/Variable.cs index 6c0336d8..5da439ae 100644 --- a/Assets/Fungus/VisualScripting/Variable.cs +++ b/Assets/Fungus/VisualScripting/Variable.cs @@ -4,7 +4,12 @@ using System.Collections; namespace Fungus.Script { - + public class FungusVariable : MonoBehaviour + { + public VariableScope scope; + public string key; + } + public enum VariableType { Boolean, @@ -18,64 +23,7 @@ namespace Fungus.Script Local, Global }; - - [Serializable] - public class Variable - { - public string key; - public VariableType type; - public VariableScope scope; - - bool booleanValue; - int integerValue; - float floatValue; - string stringValue; - - public bool BooleanValue - { - get { return (scope == VariableScope.Local) ? booleanValue : Variables.GetBoolean(key); } - set { if (scope == VariableScope.Local) { booleanValue = value; } else { Variables.SetBoolean(key, value); } } - } - - public int IntegerValue - { - get { return (scope == VariableScope.Local) ? integerValue : Variables.GetInteger(key); } - set { if (scope == VariableScope.Local) { integerValue = value; } else { Variables.SetInteger(key, value); } } - } - - public float FloatValue - { - get { return (scope == VariableScope.Local) ? floatValue : Variables.GetFloat(key); } - set { if (scope == VariableScope.Local) { floatValue = value; } else { Variables.SetFloat(key, value); } } - } - - public string StringValue - { - get { return (scope == VariableScope.Local) ? stringValue : Variables.GetString(key); } - set { if (scope == VariableScope.Local) { stringValue = value; } else { Variables.SetString(key, value); } } - } - - public bool IsSameType(Variable other) - { - return (this.type == other.type); - } - - public bool Assign(Variable other) - { - if (!IsSameType(other)) - { - return false; - } - - booleanValue = other.booleanValue; - integerValue = other.integerValue; - floatValue = other.floatValue; - stringValue = other.stringValue; - - return true; - } - } - + [Serializable] public class VariableData { diff --git a/Assets/Shuttle/ShuttleGame.unity b/Assets/Shuttle/ShuttleGame.unity index 428debeb..080066c1 100644 Binary files a/Assets/Shuttle/ShuttleGame.unity and b/Assets/Shuttle/ShuttleGame.unity differ