@ -14,207 +14,194 @@ using System.Text.RegularExpressions;
namespace Fungus
{
/ * *
* Interface for Flowchart c omponents which c an be updated when the
* scene loads in the editor . This is used to maintain backwards
* compatibility with earlier versions of Fungus .
* /
/// <summary>
/// Interface for Flowchart c omponents which c an be updated when the
/// scene loads in the editor. This is used to maintain backwards
/// compatibility with earlier versions of Fungus.
/// </summary>
interface IUpdateable
{
void UpdateToVersion ( int oldVersion , int newVersion ) ;
}
/ * *
* Visual scripting c ontroller for the Flowchart programming language .
* Flowchart objects may be edited visually using the Flowchart editor window .
* /
/// <summary>
/// Visual scripting c ontroller for the Flowchart programming language.
/// Flowchart objects may be edited visually using the Flowchart editor window.
/// </summary>
[ExecuteInEditMode]
public class Flowchart : MonoBehaviour , StringSubstituter . ISubstitutionHandler
{
/ * *
* The current version of the Flowchart . Used for updating components .
* /
/// <summary>
/// The current version of the Flowchart. Used for updating components.
/// </summary>
public const int CURRENT_VERSION = 1 ;
/ * *
* The name of the initial block in a new flowchart .
* /
/// <summary>
/// The name of the initial block in a new flowchart .
/// </summary>
public const string DEFAULT_BLOCK_NAME = "New Block" ;
/ * *
* Variable to track flowchart ' s version so components can update to new versions .
* /
/// <summary>
/// Variable to track flowchart's version so components can update to new versions .
/// </summary>
[HideInInspector]
[SerializeField] protected int version = 0 ; // Default to 0 to always trigger an update for older versions of Fungus.
public int Version { set { version = value ; } }
/ * *
* Scroll position of Flowchart editor window .
* /
/// <summary>
/// Scroll position of Flowchart editor window.
/// </summary>
[HideInInspector]
[SerializeField] protected Vector2 scrollPos ;
public Vector2 ScrollPos { get { return scrollPos ; } set { scrollPos = value ; } }
/ * *
* Scroll position of Flowchart variables window .
* /
/// <summary>
/// Scroll position of Flowchart variables window.
/// </summary>
[HideInInspector]
[SerializeField] protected Vector2 variablesScrollPos ;
public Vector2 VariablesScrollPos { get { return variablesScrollPos ; } set { variablesScrollPos = value ; } }
/ * *
* Show the variables pane .
* /
/// <summary>
/// Show the variables pane .
/// </summary>
[HideInInspector]
[SerializeField] protected bool variablesExpanded = true ;
public bool VariablesExpanded { get { return variablesExpanded ; } set { variablesExpanded = value ; } }
/ * *
* Height of c ommand block view in inspector .
* /
/// <summary>
/// Height of c ommand block view in inspector.
/// </summary>
[HideInInspector]
[SerializeField] protected float blockViewHeight = 4 0 0 ;
public float BlockViewHeight { get { return blockViewHeight ; } set { blockViewHeight = value ; } }
/ * *
* Zoom level of Flowchart editor window
* /
/// <summary>
/// Zoom level of Flowchart editor window.
/// </summary>
[HideInInspector]
[SerializeField] protected float zoom = 1f ;
public float Zoom { get { return zoom ; } set { zoom = value ; } }
/ * *
* Scrollable area for Flowchart editor window .
* /
/// <summary>
/// Scrollable area for Flowchart editor window.
/// </summary>
[HideInInspector]
[SerializeField] protected Rect scrollViewRect ;
public Rect ScrollViewRect { get { return scrollViewRect ; } set { scrollViewRect = value ; } }
/ * *
* Currently selected block in the Flowchart editor .
* /
/// <summary>
/// Currently selected block in the Flowchart editor.
/// </summary>
[HideInInspector]
[FormerlySerializedAs("selectedSequence")]
[SerializeField] protected Block selectedBlock ;
public Block SelectedBlock { get { return selectedBlock ; } set { selectedBlock = value ; } }
/ * *
* Currently selected c ommand in the Flowchart editor .
* /
/// <summary>
/// Currently selected c ommand in the Flowchart editor.
/// </summary>
[HideInInspector]
[SerializeField] protected List < Command > selectedCommands = new List < Command > ( ) ;
public List < Command > SelectedCommands { get { return selectedCommands ; } }
/ * *
* The list of variables that can be accessed by the Flowchart .
* /
/// <summary>
/// The list of variables that can be accessed by the Flowchart.
/// </summary>
[HideInInspector]
[SerializeField] protected List < Variable > variables = new List < Variable > ( ) ;
public List < Variable > Variables { get { return variables ; } }
/// <summary>
/// Description text displayed in the Flowchart editor window
/// </summary>
[TextArea(3, 5)]
[Tooltip("Description text displayed in the Flowchart editor window")]
[SerializeField] protected string description = "" ;
public string Description { get { return description ; } }
/ * *
* Slow down execution in the editor to make it easier to visualise program flow .
* /
/// <summary>
/// Slow down execution in the editor to make it easier to visualise program flow.
/// </summary>
[Range(0f, 5f)]
[Tooltip("Adds a pause after each execution step to make it easier to visualise program flow. Editor only, has no effect in platform builds.")]
[SerializeField] protected float stepPause = 0f ;
public float StepPause { get { return stepPause ; } }
/ * *
* Use command c olor when displaying the c ommand list in the inspector .
* /
/// <summary>
/// Use command c olor when displaying the c ommand list in the inspector.
/// </summary>
[Tooltip("Use command color when displaying the command list in the Fungus Editor window")]
[SerializeField] protected bool colorCommands = true ;
public bool ColorCommands { get { return colorCommands ; } }
/ * *
* Hides the Flowchart block and command components in the inspector .
* Deselect to inspect the block and command components that make up the Flowchart .
* /
[Tooltip("Hides the Flowchart block and command components in the inspector")]
/// <summary>
/// Hides the Flowchart block and command components in the inspector .
/// Deselect to inspect the block and command components that make up the Flowchart .
/// </summary>
[Tooltip("Hides the Flowchart block and command components in the inspector. Deselect to inspect the block and command components that make up the Flowchart. ")]
[SerializeField] protected bool hideComponents = true ;
/ * *
* Saves the selected block and commands when saving the scene .
* Helps avoid version control conflicts if you ' ve only changed the active selection .
* /
[Tooltip("Saves the selected block and commands when saving the scene.")]
/// <summary>
/// Saves the selected block and commands when saving the scene. Helps avoid version control conflicts if you've only changed the active selection.
/// </summary>
[Tooltip("Saves the selected block and commands when saving the scene. Helps avoid version control conflicts if you've only changed the active selection.")]
[SerializeField] protected bool saveSelection = true ;
public bool SaveSelection { get { return saveSelection ; } }
/ * *
* Unique identifier for identifying this flowchart in localized string keys .
* /
/// <summary>
/// Unique identifier for identifying this flowchart in localized string keys .
/// </summary>
[Tooltip("Unique identifier for this flowchart in localized string keys. If no id is specified then the name of the Flowchart object will be used.")]
[SerializeField] protected string localizationId = "" ;
public string LocalizationId { get { return localizationId ; } }
/ * *
* Display line numbers in the c ommand list in the Block inspector .
* /
/// <summary>
/// Display line numbers in the c ommand list in the Block inspector.
/// </summary>
[Tooltip("Display line numbers in the command list in the Block inspector.")]
[SerializeField] protected bool showLineNumbers = false ;
public bool ShowLineNumbers { get { return showLineNumbers ; } }
/ * *
* List of commands to hide in the Add Command menu . Use this to restrict the set of commands available when editing a Flowchart .
* /
/// <summary>
/// List of commands to hide in the Add Command menu. Use this to restrict the set of commands available when editing a Flowchart .
/// </summary>
[Tooltip("List of commands to hide in the Add Command menu. Use this to restrict the set of commands available when editing a Flowchart.")]
[SerializeField] protected List < string > hideCommands = new List < string > ( ) ;
/// <summary>
/// Lua Environment to be used by default for all Execute Lua commands in this Flowchart.
/// </summary>
[Tooltip("Lua Environment to be used by default for all Execute Lua commands in this Flowchart")]
[SerializeField] protected LuaEnvironment luaEnvironment ;
public LuaEnvironment _L uaEnvironment { get { return luaEnvironment ; } }
/ * *
* The ExecuteLua c ommand adds a global Lua variable with this name bound to the flowchart prior to executing .
* /
/// <summary>
/// The ExecuteLua c ommand adds a global Lua variable with this name bound to the flowchart prior to executing.
/// </summary>
[Tooltip("The ExecuteLua command adds a global Lua variable with this name bound to the flowchart prior to executing.")]
[SerializeField] protected string luaBindingName = "flowchart" ;
public string LuaBindingName { get { return luaBindingName ; } }
/ * *
* Position in the center of all blocks in the flowchart .
* /
/// <summary>
/// Position in the center of all blocks in the flowchart .
/// </summary>
public Vector2 CenterPosition { set ; get ; }
/ * *
* Cached list of flowchart objects in the scene for fast lookup
* /
/// <summary>
/// Cached list of flowchart objects in the scene for fast lookup.
/// </summary>
public static List < Flowchart > cachedFlowcharts = new List < Flowchart > ( ) ;
protected static bool eventSystemPresent ;
protected StringSubstituter stringSubstituer ;
/ * *
* Returns the next id to assign to a new flowchart item .
* Item ids increase monotically so they are guaranteed to
* be unique within a Flowchart .
* /
/// <summary>
/// Returns the next id to assign to a new flowchart item.
/// Item ids increase monotically so they are guaranteed to
/// be unique within a Flowchart .
/// </summary>
public int NextItemId ( )
{
int maxId = - 1 ;
@ -410,9 +397,9 @@ namespace Fungus
return block ;
}
/ * *
* Create a new block node which you can then add commands to .
* /
/// <summary>
/// Create a new block node which you can then add commands to .
/// </summary>
public virtual Block CreateBlock ( Vector2 position )
{
Block b = CreateBlockComponent ( gameObject ) ;
@ -423,9 +410,9 @@ namespace Fungus
return b ;
}
/ * *
* Returns the named Block in the flowchart , or null if not found .
* /
/// <summary>
/// Returns the named Block in the flowchart, or null if not found .
/// </summary>
public virtual Block FindBlock ( string blockName )
{
Block [ ] blocks = GetComponents < Block > ( ) ;
@ -440,11 +427,9 @@ namespace Fungus
return null ;
}
/ * *
* Execute a child block in the Flowchart .
* You can use this method in a UI event . e . g . to handle a button click .
* Returns true if the Block started execution .
* /
/// <summary>
/// Execute a child block in the Flowchart.
/// You can use this method in a UI event. e.g. to handle a button click.
public virtual void ExecuteBlock ( string blockName )
{
Block block = null ;
@ -469,12 +454,12 @@ namespace Fungus
}
}
/ * *
* Execute a c hild block in the flowchart .
* The block must be in an idle state to be executed .
* This version provides extra options to control how the block is executed .
* Returns true if the Block started execution .
* /
/// <summary>
/// Execute a c hild block in the flowchart.
/// The block must be in an idle state to be executed.
/// This version provides extra options to control how the block is executed .
/// Returns true if the Block started execution.
/// </summary>
public virtual bool ExecuteBlock ( Block block , int commandIndex = 0 , Action onComplete = null )
{
if ( block = = null )
@ -501,9 +486,9 @@ namespace Fungus
return true ;
}
/ * *
* Stop a ll executing Blocks in this Flowchart .
* /
/// <summary>
/// Stop al l executing Blocks in this Flowchart.
/// </summary>
public virtual void StopAllBlocks ( )
{
Block [ ] blocks = GetComponents < Block > ( ) ;
@ -516,10 +501,10 @@ namespace Fungus
}
}
/ * *
* Sends a message to this Flowchart only .
* Any block with a matching MessageReceived event handler will start executing .
* /
/// <summary>
/// Sends a message to this Flowchart only.
/// Any block with a matching MessageReceived event handler will start executing.
/// </summary>
public virtual void SendFungusMessage ( string messageName )
{
MessageReceived [ ] eventHandlers = GetComponents < MessageReceived > ( ) ;
@ -529,10 +514,10 @@ namespace Fungus
}
}
/ * *
* Sends a message to all Flowchart objects in the c urrent scene .
* Any block with a matching MessageReceived event handler will start executing .
* /
/// <summary>
/// Sends a message to all Flowchart objects in the c urrent scene.
/// Any block with a matching MessageReceived event handler will start executing.
/// </summary>
public static void BroadcastFungusMessage ( string messageName )
{
MessageReceived [ ] eventHandlers = UnityEngine . Object . FindObjectsOfType < MessageReceived > ( ) ;
@ -542,9 +527,9 @@ namespace Fungus
}
}
/ * *
* Returns a new variable key that is guaranteed not to clas h with any existing variable in the list .
* /
/// <summary>
/// Returns a new variable key that is guaranteed not to clas h with any existing variable in the list.
/// </summary>
public virtual string GetUniqueVariableKey ( string originalKey , Variable ignoreVariable = null )
{
int suffix = 0 ;
@ -591,9 +576,9 @@ namespace Fungus
}
}
/ * *
* Returns a new Block key that is guaranteed not to clas h with any existing Block in the Flowchart .
* /
/// <summary>
/// Returns a new Block key that is guaranteed not to clas h with any existing Block in the Flowchart.
/// </summary>
public virtual string GetUniqueBlockKey ( string originalKey , Block ignoreBlock = null )
{
int suffix = 0 ;
@ -634,9 +619,9 @@ namespace Fungus
}
}
/ * *
* Returns a new Label key that is guaranteed not to clas h with any existing Label in the Block .
* /
/// <summary>
/// Returns a new Label key that is guaranteed not to clas h with any existing Label in the Block.
/// </summary>
public virtual string GetUniqueLabelKey ( string originalKey , Label ignoreLabel )
{
int suffix = 0 ;
@ -677,14 +662,14 @@ namespace Fungus
}
}
}
/ * *
* Returns the variable with the specified key , or null if the key is not found .
* You will need to cast the returned variable to the c orrect sub - type .
* You can then access the variable ' s value using the Value property . e . g .
* BooleanVariable boolVar = flowchart . GetVariable ( "MyBool" ) as BooleanVariable ;
* boolVar . Value = false ;
* /
/// <summary>
/// Returns the variable with the specified key, or null if the key is not found.
/// You will need to cast the returned variable to the c orrect sub-type.
/// You can then access the variable's value using the Value property. e.g .
/// BooleanVariable boolVar = flowchart.GetVariable("MyBool") as BooleanVariable;
/// boolVar.Value = false ;
/// </summary>
public Variable GetVariable ( string key )
{
foreach ( Variable variable in variables )
@ -698,12 +683,12 @@ namespace Fungus
return null ;
}
/ * *
* Returns the variable with the specified key , or null if the key is not found .
* You can then access the variable ' s value using the Value property . e . g .
* BooleanVariable boolVar = flowchart . GetVariable < BooleanVariable > ( "MyBool" ) ;
* boolVar . Value = false ;
* /
/// <summary>
/// Returns the variable with the specified key, or null if the key is not found.
/// You can then access the variable's value using the Value property. e.g .
/// BooleanVariable boolVar = flowchart.GetVariable<BooleanVariable>("MyBool") ;
/// boolVar.Value = false ;
/// </summary>
public T GetVariable < T > ( string key ) where T : Variable
{
foreach ( Variable variable in variables )
@ -718,10 +703,10 @@ namespace Fungus
return null ;
}
/ * *
* Register a new variable with the Flowchart at runtime .
* The variable should be added as a c omponent on the Flowchart game object .
* /
/// <summary>
/// Register a new variable with the Flowchart at runtime.
/// The variable should be added as a c omponent on the Flowchart game object.
/// </summary>
public void SetVariable < T > ( string key , T newvariable ) where T : Variable
{
foreach ( Variable v in variables )
@ -740,9 +725,9 @@ namespace Fungus
Debug . LogWarning ( "Variable " + key + " not found." ) ;
}
/ * *
* Gets a list of all variables with public scope in this Flowchart .
* /
/// <summary>
/// Gets a list of all variables with public scope in this Flowchart .
/// </summary>
public virtual List < Variable > GetPublicVariables ( )
{
List < Variable > publicVariables = new List < Variable > ( ) ;
@ -757,10 +742,10 @@ namespace Fungus
return publicVariables ;
}
/ * *
* Gets the value of a boolean variable .
* Returns false if the variable key does not exist .
* /
/// <summary>
/// Gets the value of a boolean variable.
/// Returns false if the variable key does not exist.
/// </summary>
public virtual bool GetBooleanVariable ( string key )
{
BooleanVariable variable = GetVariable < BooleanVariable > ( key ) ;
@ -775,10 +760,10 @@ namespace Fungus
}
}
/ * *
* Sets the value of a boolean variable .
* The variable must already be added to the list of variables for this Flowchart .
* /
/// <summary>
/// Sets the value of a boolean variable.
/// The variable must already be added to the list of variables for this Flowchart.
/// </summary>
public virtual void SetBooleanVariable ( string key , bool value )
{
BooleanVariable variable = GetVariable < BooleanVariable > ( key ) ;
@ -788,10 +773,10 @@ namespace Fungus
}
}
/ * *
* Gets the value of an integer variable .
* Returns 0 if the variable key does not exist .
* /
/// <summary>
/// Gets the value of an integer variable.
/// Returns 0 if the variable key does not exist.
/// </summary>
public virtual int GetIntegerVariable ( string key )
{
IntegerVariable variable = GetVariable < IntegerVariable > ( key ) ;
@ -806,10 +791,10 @@ namespace Fungus
}
}
/ * *
* Sets the value of an integer variable .
* The variable must already be added to the list of variables for this Flowchart .
* /
/// <summary>
/// Sets the value of an integer variable.
/// The variable must already be added to the list of variables for this Flowchart.
/// </summary>
public virtual void SetIntegerVariable ( string key , int value )
{
IntegerVariable variable = GetVariable < IntegerVariable > ( key ) ;
@ -819,10 +804,10 @@ namespace Fungus
}
}
/ * *
* Gets the value of a float variable .
* Returns 0 if the variable key does not exist .
* /
/// <summary>
/// Gets the value of a float variable.
/// Returns 0 if the variable key does not exist.
/// </summary>
public virtual float GetFloatVariable ( string key )
{
FloatVariable variable = GetVariable < FloatVariable > ( key ) ;
@ -837,10 +822,10 @@ namespace Fungus
}
}
/ * *
* Sets the value of a float variable .
* The variable must already be added to the list of variables for this Flowchart .
* /
/// <summary>
/// Sets the value of a float variable.
/// The variable must already be added to the list of variables for this Flowchart.
/// </summary>
public virtual void SetFloatVariable ( string key , float value )
{
FloatVariable variable = GetVariable < FloatVariable > ( key ) ;
@ -850,10 +835,10 @@ namespace Fungus
}
}
/ * *
* Gets the value of a string variable .
* Returns the empty string if the variable key does not exist .
* /
/// <summary>
/// Gets the value of a string variable.
/// Returns the empty string if the variable key does not exist.
/// </summary>
public virtual string GetStringVariable ( string key )
{
StringVariable variable = GetVariable < StringVariable > ( key ) ;
@ -868,10 +853,10 @@ namespace Fungus
}
}
/ * *
* Sets the value of a string variable .
* The variable must already be added to the list of variables for this Flowchart .
* /
/// <summary>
/// Sets the value of a string variable.
/// The variable must already be added to the list of variables for this Flowchart.
/// </summary>
public virtual void SetStringVariable ( string key , string value )
{
StringVariable variable = GetVariable < StringVariable > ( key ) ;
@ -881,9 +866,9 @@ namespace Fungus
}
}
/ * *
* Set the block objects to be hidden or visible depending on the hideComponents property .
* /
/// <summary>
/// Set the block objects to be hidden or visible depending on the hideComponents property.
/// </summary>
public virtual void UpdateHideFlags ( )
{
if ( hideComponents )
@ -927,11 +912,17 @@ namespace Fungus
}
/// <summary>
/// Clears the list of selected commands.
/// </summary>
public virtual void ClearSelectedCommands ( )
{
selectedCommands . Clear ( ) ;
}
/// <summary>
/// Adds a command to the list of selected commands.
/// </summary>
public virtual void AddSelectedCommand ( Command command )
{
if ( ! selectedCommands . Contains ( command ) )
@ -940,6 +931,9 @@ namespace Fungus
}
}
/// <summary>
/// Reset the commands and variables in the Flowchart.
/// </summary>
public virtual void Reset ( bool resetCommands , bool resetVariables )
{
if ( resetCommands )
@ -960,9 +954,9 @@ namespace Fungus
}
}
/ * *
* Override this in a Flowchart subclass to filter which commands are shown in the Add Command list .
* /
/// <summary>
/// Override this in a Flowchart subclass to filter which commands are shown in the Add Command list .
/// </summary>
public virtual bool IsCommandSupported ( CommandInfoAttribute commandInfo )
{
foreach ( string key in hideCommands )
@ -978,9 +972,9 @@ namespace Fungus
return true ;
}
/ * *
* Returns true if there are any executing blocks in this Flowchart .
* /
/// <summary>
/// Returns true if there are any executing blocks in this Flowchart.
/// </summary>
public virtual bool HasExecutingBlocks ( )
{
Block [ ] blocks = GetComponents < Block > ( ) ;
@ -994,9 +988,9 @@ namespace Fungus
return false ;
}
/ * *
* Returns a list of all executing blocks in this Flowchart .
* /
/// <summary>
/// Returns a list of all executing blocks in this Flowchart .
/// </summary>
public virtual List < Block > GetExecutingBlocks ( )
{
List < Block > executingBlocks = new List < Block > ( ) ;
@ -1013,11 +1007,11 @@ namespace Fungus
return executingBlocks ;
}
/ * *
* Implementation of StringSubstituter . ISubstitutionHandler which matches any public variable in the Flowchart .
* To perform full variable substitution with a ll substitution handlers in the scene , you should
* use the SubstituteVariables ( ) method instead .
* /
/// <summary>
/// Implementation of StringSubstituter.ISubstitutionHandler which matches any public variable in the Flowchart .
/// To perform full variable substitution with al l substitution handlers in the scene, you should
/// use the SubstituteVariables() method instead .
/// </summary>
[MoonSharp.Interpreter.MoonSharpHidden]
public virtual bool SubstituteStrings ( StringBuilder input )
{
@ -1052,12 +1046,12 @@ namespace Fungus
return modified ;
}
/ * *
* Substitute variables in the input text with the format { $ VarName }
* This will first match with private variables in this Flowchart , and then
* with public variables in all Flowcharts in the scene ( and any component
* in the scene that implements StringSubstituter . ISubstitutionHandler ) .
* /
/// <summary>
/// Substitute variables in the input text with the format {$VarName }
/// This will first match with private variables in this Flowchart, and then
/// with public variables in all Flowcharts in the scene (and any component
/// in the scene that implements StringSubstituter.ISubstitutionHandler) .
/// </summary>
public virtual string SubstituteVariables ( string input )
{
if ( stringSubstituer = = null )