Browse Source

Refactored Flowchart.cs to use the more general GetVariable method

master
Timothy Ng 9 years ago
parent
commit
dc069367ed
  1. 44
      Assets/Fungus/Flowchart/Scripts/Commands/InvokeMethod.cs
  2. 171
      Assets/Fungus/Flowchart/Scripts/Flowchart.cs
  3. 8
      Assets/Tests/Scripting/TestInvoke.cs

44
Assets/Fungus/Flowchart/Scripts/Commands/InvokeMethod.cs

@ -191,18 +191,26 @@ namespace Fungus
switch (item.objValue.typeFullname)
{
case "System.Int32":
objValue = flowChart.GetIntegerVariable(item.variableKey);
break;
case "System.Boolean":
objValue = flowChart.GetBooleanVariable(item.variableKey);
break;
var intvalue = flowChart.GetVariable<IntegerVariable>(item.variableKey);
if (intvalue != null)
objValue = intvalue.value;
break;
case "System.Boolean":
var boolean = flowChart.GetVariable<BooleanVariable>(item.variableKey);
if (boolean != null)
objValue = boolean.value;
break;
case "System.Single":
objValue = flowChart.GetFloatVariable(item.variableKey);
break;
case "System.String":
objValue = flowChart.GetStringVariable(item.variableKey);
break;
case "UnityEngine.Color":
var floatvalue = flowChart.GetVariable<FloatVariable>(item.variableKey);
if (floatvalue != null)
objValue = floatvalue.value;
break;
case "System.String":
var stringvalue = flowChart.GetVariable<StringVariable>(item.variableKey);
if (stringvalue != null)
objValue = stringvalue.value;
break;
case "UnityEngine.Color":
var color = flowChart.GetVariable<ColorVariable>(item.variableKey);
if (color != null)
objValue = color.value;
@ -258,17 +266,17 @@ namespace Fungus
switch (returnType)
{
case "System.Int32":
flowChart.SetIntegerVariable(key, (int)value);
break;
flowChart.GetVariable<IntegerVariable>(key).value = (int)value;
break;
case "System.Boolean":
flowChart.SetBooleanVariable(key, (bool)value);
flowChart.GetVariable<BooleanVariable>(key).value = (bool)value;
break;
case "System.Single":
flowChart.SetFloatVariable(key, (float)value);
break;
flowChart.GetVariable<FloatVariable>(key).value = (float)value;
break;
case "System.String":
flowChart.SetStringVariable(key, (string)value);
break;
flowChart.GetVariable<StringVariable>(key).value = (string)value;
break;
case "UnityEngine.Color":
flowChart.GetVariable<ColorVariable>(key).value = (UnityEngine.Color)value;
break;

171
Assets/Fungus/Flowchart/Scripts/Flowchart.cs

@ -606,7 +606,8 @@ namespace Fungus
}
}
return null;
Debug.LogWarning("Variable " + key + " not found.");
return null;
}
/**
@ -626,174 +627,6 @@ namespace Fungus
return publicVariables;
}
/**
* Gets the value of a boolean variable.
* Returns false if the variable key does not exist.
*/
public virtual bool GetBooleanVariable(string key)
{
foreach (Variable v in variables)
{
if (v != null && v.key == key)
{
BooleanVariable variable = v as BooleanVariable;
if (variable != null)
{
return variable.value;
}
}
}
Debug.LogWarning("Boolean variable " + key + " not found.");
return false;
}
/**
* Sets the value of a boolean variable.
* The variable must already be added to the list of variables for this Flowchart.
*/
public virtual void SetBooleanVariable(string key, bool value)
{
foreach (Variable v in variables)
{
if (v != null && v.key == key)
{
BooleanVariable variable = v as BooleanVariable;
if (variable != null)
{
variable.value = value;
return;
}
}
}
Debug.LogWarning("Boolean variable " + key + " not found.");
}
/**
* Gets the value of an integer variable.
* Returns 0 if the variable key does not exist.
*/
public virtual int GetIntegerVariable(string key)
{
foreach (Variable v in variables)
{
if (v != null && v.key == key)
{
IntegerVariable variable = v as IntegerVariable;
if (variable != null)
{
return variable.value;
}
}
}
Debug.LogWarning("Integer variable " + key + " not found.");
return 0;
}
/**
* Sets the value of an integer variable.
* The variable must already be added to the list of variables for this Flowchart.
*/
public virtual void SetIntegerVariable(string key, int value)
{
foreach (Variable v in variables)
{
if (v != null && v.key == key)
{
IntegerVariable variable = v as IntegerVariable;
if (variable != null)
{
variable.value = value;
return;
}
}
}
Debug.LogWarning("Integer variable " + key + " not found.");
}
/**
* Gets the value of a float variable.
* Returns 0 if the variable key does not exist.
*/
public virtual float GetFloatVariable(string key)
{
foreach (Variable v in variables)
{
if (v != null && v.key == key)
{
FloatVariable variable = v as FloatVariable;
if (variable != null)
{
return variable.value;
}
}
}
Debug.LogWarning("Float variable " + key + " not found.");
return 0f;
}
/**
* Sets the value of a float variable.
* The variable must already be added to the list of variables for this Flowchart.
*/
public virtual void SetFloatVariable(string key, float value)
{
foreach (Variable v in variables)
{
if (v != null && v.key == key)
{
FloatVariable variable = v as FloatVariable;
if (variable != null)
{
variable.value = value;
return;
}
}
}
Debug.LogWarning("Float variable " + key + " not found.");
}
/**
* Gets the value of a string variable.
* Returns the empty string if the variable key does not exist.
*/
public virtual string GetStringVariable(string key)
{
foreach (Variable v in variables)
{
if (v != null && v.key == key)
{
StringVariable variable = v as StringVariable;
if (variable != null)
{
return variable.value;
}
}
}
Debug.LogWarning("String variable " + key + " not found.");
return "";
}
/**
* Sets the value of a string variable.
* The variable must already be added to the list of variables for this Flowchart.
*/
public virtual void SetStringVariable(string key, string value)
{
foreach (Variable v in variables)
{
if (v != null && v.key == key)
{
StringVariable variable = v as StringVariable;
if (variable != null)
{
variable.value = value;
return;
}
}
}
Debug.LogWarning("String variable " + key + " not found.");
}
/**
* Set the block objects to be hidden or visible depending on the hideComponents property.
*/

8
Assets/Tests/Scripting/TestInvoke.cs

@ -95,10 +95,10 @@ namespace Fungus
}
// Check Fungus variables are populated with expected values
if (flowchart.GetBooleanVariable("BoolVar") != true ||
flowchart.GetIntegerVariable("IntVar") != 5 ||
flowchart.GetFloatVariable("FloatVar") != 22.1f ||
flowchart.GetStringVariable("StringVar") != "a string")
if (flowchart.GetVariable<BooleanVariable>("BoolVar").value != true ||
flowchart.GetVariable<IntegerVariable>("IntVar").value != 5 ||
flowchart.GetVariable<FloatVariable>("FloatVar").value != 22.1f ||
flowchart.GetVariable<StringVariable>("StringVar").value != "a string")
{
IntegrationTest.Fail("Fungus variables do not match expected values");
return;

Loading…
Cancel
Save