Browse Source

Refactored Flowchart.cs to use the more general GetVariable method

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

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

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

169
Assets/Fungus/Flowchart/Scripts/Flowchart.cs

@ -606,6 +606,7 @@ namespace Fungus
} }
} }
Debug.LogWarning("Variable " + key + " not found.");
return null; return null;
} }
@ -626,174 +627,6 @@ namespace Fungus
return publicVariables; 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. * 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 // Check Fungus variables are populated with expected values
if (flowchart.GetBooleanVariable("BoolVar") != true || if (flowchart.GetVariable<BooleanVariable>("BoolVar").value != true ||
flowchart.GetIntegerVariable("IntVar") != 5 || flowchart.GetVariable<IntegerVariable>("IntVar").value != 5 ||
flowchart.GetFloatVariable("FloatVar") != 22.1f || flowchart.GetVariable<FloatVariable>("FloatVar").value != 22.1f ||
flowchart.GetStringVariable("StringVar") != "a string") flowchart.GetVariable<StringVariable>("StringVar").value != "a string")
{ {
IntegrationTest.Fail("Fungus variables do not match expected values"); IntegrationTest.Fail("Fungus variables do not match expected values");
return; return;

Loading…
Cancel
Save