@ -1,7 +1,5 @@
/ * *
// This code is part of the Fungus library (http://fungusgames.com) maintained by Chris Gregan (http://twitter.com/gofungus).
* This code is part of the Fungus library ( http : //fungusgames.com) maintained by Chris Gregan (http://twitter.com/gofungus).
// It is released for free under the MIT open source license (https://github.com/snozbot/fungus/blob/master/LICENSE)
* It is released for free under the MIT open source license ( https : //github.com/snozbot/fungus/blob/master/LICENSE)
* /
using UnityEngine ;
using UnityEngine ;
using UnityEngine.EventSystems ;
using UnityEngine.EventSystems ;
@ -14,185 +12,207 @@ using System.Text.RegularExpressions;
namespace Fungus
namespace Fungus
{
{
/ * *
/// <summary>
* Interface for Flowchart c omponents which c an be updated when the
/// Interface for Flowchart c omponents which c an be updated when the
* scene loads in the editor . This is used to maintain backwards
/// scene loads in the editor. This is used to maintain backwards
* compatibility with earlier versions of Fungus .
/// compatibility with earlier versions of Fungus.
* /
/// </summary>
interface IUpdateable
interface IUpdateable
{
{
void UpdateToVersion ( int oldVersion , int newVersion ) ;
void UpdateToVersion ( int oldVersion , int newVersion ) ;
}
}
/ * *
/// <summary>
* Visual scripting c ontroller for the Flowchart programming language .
/// Visual scripting c ontroller for the Flowchart programming language.
* Flowchart objects may be edited visually using the Flowchart editor window .
/// Flowchart objects may be edited visually using the Flowchart editor window.
* /
/// </summary>
[ExecuteInEditMode]
[ExecuteInEditMode]
public class Flowchart : MonoBehaviour , StringSubstituter . ISubstitutionHandler
public class Flowchart : MonoBehaviour , StringSubstituter . ISubstitutionHandler
{
{
/ * *
/// <summary>
* The current version of the Flowchart . Used for updating components .
/// The current version of the Flowchart. Used for updating components.
* /
/// </summary>
public const int CURRENT_VERSION = 1 ;
public const int CURRENT_VERSION = 1 ;
/ * *
/// <summary>
* The name of the initial block in a new flowchart .
/// The name of the initial block in a new flowchart .
* /
/// </summary>
public const string DEFAULT_BLOCK_NAME = "New Block" ;
public const string DEFAULT_BLOCK_NAME = "New Block" ;
/ * *
/// <summary>
* Variable to track flowchart ' s version so components can update to new versions .
/// Variable to track flowchart's version so components can update to new versions .
* /
/// </summary>
[HideInInspector]
[HideInInspector]
public int version = 0 ; // Default to 0 to always trigger an update for older versions of Fungus.
[SerializeField] protected int version = 0 ; // Default to 0 to always trigger an update for older versions of Fungus.
public int Version { set { version = value ; } }
/ * *
/// <summary>
* Scroll position of Flowchart editor window .
/// Scroll position of Flowchart editor window.
* /
/// </summary>
[HideInInspector]
[HideInInspector]
public Vector2 scrollPos ;
[SerializeField] protected Vector2 scrollPos ;
public Vector2 ScrollPos { get { return scrollPos ; } set { scrollPos = value ; } }
/ * *
/// <summary>
* Scroll position of Flowchart variables window .
/// Scroll position of Flowchart variables window.
* /
/// </summary>
[HideInInspector]
[HideInInspector]
public Vector2 variablesScrollPos ;
[SerializeField] protected Vector2 variablesScrollPos ;
public Vector2 VariablesScrollPos { get { return variablesScrollPos ; } set { variablesScrollPos = value ; } }
/ * *
/// <summary>
* Show the variables pane .
/// Show the variables pane .
* /
/// </summary>
[HideInInspector]
[HideInInspector]
public bool variablesExpanded = true ;
[SerializeField] protected bool variablesExpanded = true ;
public bool VariablesExpanded { get { return variablesExpanded ; } set { variablesExpanded = value ; } }
/ * *
/// <summary>
* Height of c ommand block view in inspector .
/// Height of c ommand block view in inspector.
* /
/// </summary>
[HideInInspector]
[HideInInspector]
public float blockViewHeight = 4 0 0 ;
[SerializeField] protected float blockViewHeight = 4 0 0 ;
public float BlockViewHeight { get { return blockViewHeight ; } set { blockViewHeight = value ; } }
/ * *
/// <summary>
* Zoom level of Flowchart editor window
/// Zoom level of Flowchart editor window.
* /
/// </summary>
[HideInInspector]
[HideInInspector]
public float zoom = 1f ;
[SerializeField] protected float zoom = 1f ;
public float Zoom { get { return zoom ; } set { zoom = value ; } }
/ * *
/// <summary>
* Scrollable area for Flowchart editor window .
/// Scrollable area for Flowchart editor window.
* /
/// </summary>
[HideInInspector]
[HideInInspector]
public Rect scrollViewRect ;
[SerializeField] protected Rect scrollViewRect ;
public Rect ScrollViewRect { get { return scrollViewRect ; } set { scrollViewRect = value ; } }
/ * *
/// <summary>
* Currently selected block in the Flowchart editor .
/// Currently selected block in the Flowchart editor.
* /
/// </summary>
[HideInInspector]
[HideInInspector]
[FormerlySerializedAs("selectedSequence")]
[FormerlySerializedAs("selectedSequence")]
public Block selectedBlock ;
[SerializeField] protected Block selectedBlock ;
public Block SelectedBlock { get { return selectedBlock ; } set { selectedBlock = value ; } }
/ * *
/// <summary>
* Currently selected c ommand in the Flowchart editor .
/// Currently selected c ommand in the Flowchart editor.
* /
/// </summary>
[HideInInspector]
[HideInInspector]
public List < Command > selectedCommands = new List < Command > ( ) ;
[SerializeField] protected List < Command > selectedCommands = new List < Command > ( ) ;
public List < Command > SelectedCommands { get { return selectedCommands ; } }
/ * *
/// <summary>
* The list of variables that can be accessed by the Flowchart .
/// The list of variables that can be accessed by the Flowchart.
* /
/// </summary>
[HideInInspector]
[HideInInspector]
public List < Variable > variables = new List < Variable > ( ) ;
[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)]
[TextArea(3, 5)]
[Tooltip("Description text displayed in the Flowchart editor window")]
[Tooltip("Description text displayed in the Flowchart editor window")]
public string description = "" ;
[SerializeField] protected string description = "" ;
public string Description { get { return description ; } }
/ * *
/// <summary>
* Slow down execution in the editor to make it easier to visualise program flow .
/// Slow down execution in the editor to make it easier to visualise program flow.
* /
/// </summary>
[Range(0f, 5f)]
[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.")]
[Tooltip("Adds a pause after each execution step to make it easier to visualise program flow. Editor only, has no effect in platform builds.")]
public float stepPause = 0f ;
[SerializeField] protected float stepPause = 0f ;
public float StepPause { get { return stepPause ; } }
/ * *
/// <summary>
* Use command c olor when displaying the c ommand list in the inspector .
/// 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")]
[Tooltip("Use command color when displaying the command list in the Fungus Editor window")]
public bool colorCommands = true ;
[SerializeField] protected bool colorCommands = true ;
public bool ColorCommands { get { return colorCommands ; } }
/ * *
* Hides the Flowchart block and command components in the inspector .
/// <summary>
* Deselect to inspect the block and command components that make up the Flowchart .
/// 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>
public bool hideComponents = true ;
[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 .
/// <summary>
* Helps avoid version control conflicts if you ' ve only changed the active selection .
/// 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.")]
[Tooltip("Saves the selected block and commands when saving the scene. Helps avoid version control conflicts if you've only changed the active selection.")]
public bool saveSelection = true ;
[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.")]
[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.")]
public string localizationId = "" ;
[SerializeField] protected string localizationId = "" ;
public string LocalizationId { get { return localizationId ; } }
/ * *
/// <summary>
* Display line numbers in the c ommand list in the Block inspector .
/// 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.")]
[Tooltip("Display line numbers in the command list in the Block inspector.")]
public bool showLineNumbers = false ;
[SerializeField] protected bool showLineNumbers = false ;
public bool ShowLineNumbers { get { return showLineNumbers ; } }
/ * *
/// <summary>
* List of commands to hide in the Add Command menu . Use this to restrict the set of commands available when editing a Flowchart .
/// 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.")]
[Tooltip("List of commands to hide in the Add Command menu. Use this to restrict the set of commands available when editing a Flowchart.")]
public List < string > hideCommands = new List < string > ( ) ;
[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")]
[Tooltip("Lua Environment to be used by default for all Execute Lua commands in this Flowchart")]
public LuaEnvironment luaEnvironment ;
[SerializeField] protected LuaEnvironment luaEnvironment ;
public LuaEnvironment _L uaEnvironment { get { return luaEnvironment ; } }
/ * *
/// <summary>
* The ExecuteLua c ommand adds a global Lua variable with this name bound to the flowchart prior to executing .
/// 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.")]
[Tooltip("The ExecuteLua command adds a global Lua variable with this name bound to the flowchart prior to executing.")]
public string luaBindingName = "flowchart" ;
[SerializeField] protected string luaBindingName = "flowchart" ;
public string LuaBindingName { get { return luaBindingName ; } }
/ * *
/// <summary>
* Position in the center of all blocks in the flowchart .
/// Position in the center of all blocks in the flowchart.
* /
/// </summary>
[NonSerialized]
public Vector2 CenterPosition { set ; get ; }
public Vector2 centerPosition = Vector2 . zero ;
/ * *
/// <summary>
* Cached list of flowchart objects in the scene for fast lookup
/// Cached list of flowchart objects in the scene for fast lookup.
* /
/// </summary>
public static List < Flowchart > cachedFlowcharts = new List < Flowchart > ( ) ;
public static List < Flowchart > cachedFlowcharts = new List < Flowchart > ( ) ;
protected static bool eventSystemPresent ;
protected static bool eventSystemPresent ;
protected StringSubstituter stringSubstituer ;
protected StringSubstituter stringSubstituer ;
/ * *
/// <summary>
* Returns the next id to assign to a new flowchart item .
/// Returns the next id to assign to a new flowchart item.
* Item ids increase monotically so they are guaranteed to
/// Item ids increase monotically so they are guaranteed to
* be unique within a Flowchart .
/// be unique within a Flowchart .
* /
/// </summary>
public int NextItemId ( )
public int NextItemId ( )
{
{
int maxId = - 1 ;
int maxId = - 1 ;
Block [ ] blocks = GetComponents < Block > ( ) ;
Block [ ] blocks = GetComponents < Block > ( ) ;
foreach ( Block block in blocks )
foreach ( Block block in blocks )
{
{
maxId = Math . Max ( maxId , block . i temId) ;
maxId = Math . Max ( maxId , block . I temId) ;
}
}
Command [ ] commands = GetComponents < Command > ( ) ;
Command [ ] commands = GetComponents < Command > ( ) ;
foreach ( Command command in commands )
foreach ( Command command in commands )
{
{
maxId = Math . Max ( maxId , command . i temId) ;
maxId = Math . Max ( maxId , command . I temId) ;
}
}
return maxId + 1 ;
return maxId + 1 ;
}
}
@ -292,23 +312,23 @@ namespace Fungus
Block [ ] blocks = GetComponents < Block > ( ) ;
Block [ ] blocks = GetComponents < Block > ( ) ;
foreach ( Block block in blocks )
foreach ( Block block in blocks )
{
{
if ( block . i temId = = - 1 | |
if ( block . I temId = = - 1 | |
usedIds . Contains ( block . i temId) )
usedIds . Contains ( block . I temId) )
{
{
block . i temId = NextItemId ( ) ;
block . I temId = NextItemId ( ) ;
}
}
usedIds . Add ( block . i temId) ;
usedIds . Add ( block . I temId) ;
}
}
Command [ ] commands = GetComponents < Command > ( ) ;
Command [ ] commands = GetComponents < Command > ( ) ;
foreach ( Command command in commands )
foreach ( Command command in commands )
{
{
if ( command . i temId = = - 1 | |
if ( command . I temId = = - 1 | |
usedIds . Contains ( command . i temId) )
usedIds . Contains ( command . I temId) )
{
{
command . i temId = NextItemId ( ) ;
command . I temId = NextItemId ( ) ;
}
}
usedIds . Add ( command . i temId) ;
usedIds . Add ( command . I temId) ;
}
}
}
}
@ -337,7 +357,7 @@ namespace Fungus
bool found = false ;
bool found = false ;
foreach ( Block block in blocks )
foreach ( Block block in blocks )
{
{
if ( block . c ommandList. Contains ( command ) )
if ( block . C ommandList. Contains ( command ) )
{
{
found = true ;
found = true ;
break ;
break ;
@ -355,7 +375,7 @@ namespace Fungus
bool found = false ;
bool found = false ;
foreach ( Block block in blocks )
foreach ( Block block in blocks )
{
{
if ( block . e ventHandler = = eventHandler )
if ( block . _ E ventHandler = = eventHandler )
{
{
found = true ;
found = true ;
break ;
break ;
@ -375,29 +395,28 @@ namespace Fungus
return block ;
return block ;
}
}
/ * *
/// <summary>
* Create a new block node which you can then add commands to .
/// Create a new block node which you can then add commands to .
* /
/// </summary>
public virtual Block CreateBlock ( Vector2 position )
public virtual Block CreateBlock ( Vector2 position )
{
{
Block b = CreateBlockComponent ( gameObject ) ;
Block b = CreateBlockComponent ( gameObject ) ;
b . nodeRect . x = position . x ;
b . _ NodeRect = new Rect ( position . x , position . y , 0 , 0 ) ;
b . nodeRect . y = position . y ;
b . BlockName = GetUniqueBlockKey ( b . BlockName , b ) ;
b . blockName = GetUniqueBlockKey ( b . blockName , b ) ;
b . ItemId = NextItemId ( ) ;
b . itemId = NextItemId ( ) ;
return b ;
return b ;
}
}
/ * *
/// <summary>
* Returns the named Block in the flowchart , or null if not found .
/// Returns the named Block in the flowchart, or null if not found .
* /
/// </summary>
public virtual Block FindBlock ( string blockName )
public virtual Block FindBlock ( string blockName )
{
{
Block [ ] blocks = GetComponents < Block > ( ) ;
Block [ ] blocks = GetComponents < Block > ( ) ;
foreach ( Block block in blocks )
foreach ( Block block in blocks )
{
{
if ( block . b lockName = = blockName )
if ( block . B lockName = = blockName )
{
{
return block ;
return block ;
}
}
@ -406,17 +425,15 @@ namespace Fungus
return null ;
return null ;
}
}
/ * *
/// <summary>
* Execute a child block in the Flowchart .
/// Execute a child block in the Flowchart.
* You can use this method in a UI event . e . g . to handle a button click .
/// You can use this method in a UI event. e.g. to handle a button click.
* Returns true if the Block started execution .
* /
public virtual void ExecuteBlock ( string blockName )
public virtual void ExecuteBlock ( string blockName )
{
{
Block block = null ;
Block block = null ;
foreach ( Block b in GetComponents < Block > ( ) )
foreach ( Block b in GetComponents < Block > ( ) )
{
{
if ( b . b lockName = = blockName )
if ( b . B lockName = = blockName )
{
{
block = b ;
block = b ;
break ;
break ;
@ -435,12 +452,12 @@ namespace Fungus
}
}
}
}
/ * *
/// <summary>
* Execute a c hild block in the flowchart .
/// Execute a c hild block in the flowchart.
* The block must be in an idle state to be executed .
/// The block must be in an idle state to be executed.
* This version provides extra options to control how the block is executed .
/// This version provides extra options to control how the block is executed .
* Returns true if the Block started execution .
/// Returns true if the Block started execution.
* /
/// </summary>
public virtual bool ExecuteBlock ( Block block , int commandIndex = 0 , Action onComplete = null )
public virtual bool ExecuteBlock ( Block block , int commandIndex = 0 , Action onComplete = null )
{
{
if ( block = = null )
if ( block = = null )
@ -467,9 +484,9 @@ namespace Fungus
return true ;
return true ;
}
}
/ * *
/// <summary>
* Stop a ll executing Blocks in this Flowchart .
/// Stop al l executing Blocks in this Flowchart.
* /
/// </summary>
public virtual void StopAllBlocks ( )
public virtual void StopAllBlocks ( )
{
{
Block [ ] blocks = GetComponents < Block > ( ) ;
Block [ ] blocks = GetComponents < Block > ( ) ;
@ -482,10 +499,10 @@ namespace Fungus
}
}
}
}
/ * *
/// <summary>
* Sends a message to this Flowchart only .
/// Sends a message to this Flowchart only.
* Any block with a matching MessageReceived event handler will start executing .
/// Any block with a matching MessageReceived event handler will start executing.
* /
/// </summary>
public virtual void SendFungusMessage ( string messageName )
public virtual void SendFungusMessage ( string messageName )
{
{
MessageReceived [ ] eventHandlers = GetComponents < MessageReceived > ( ) ;
MessageReceived [ ] eventHandlers = GetComponents < MessageReceived > ( ) ;
@ -495,10 +512,10 @@ namespace Fungus
}
}
}
}
/ * *
/// <summary>
* Sends a message to all Flowchart objects in the c urrent scene .
/// Sends a message to all Flowchart objects in the c urrent scene.
* Any block with a matching MessageReceived event handler will start executing .
/// Any block with a matching MessageReceived event handler will start executing.
* /
/// </summary>
public static void BroadcastFungusMessage ( string messageName )
public static void BroadcastFungusMessage ( string messageName )
{
{
MessageReceived [ ] eventHandlers = UnityEngine . Object . FindObjectsOfType < MessageReceived > ( ) ;
MessageReceived [ ] eventHandlers = UnityEngine . Object . FindObjectsOfType < MessageReceived > ( ) ;
@ -508,9 +525,9 @@ namespace Fungus
}
}
}
}
/ * *
/// <summary>
* Returns a new variable key that is guaranteed not to clas h with any existing variable in the list .
/// 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 )
public virtual string GetUniqueVariableKey ( string originalKey , Variable ignoreVariable = null )
{
{
int suffix = 0 ;
int suffix = 0 ;
@ -537,12 +554,12 @@ namespace Fungus
{
{
if ( variable = = null | |
if ( variable = = null | |
variable = = ignoreVariable | |
variable = = ignoreVariable | |
variable . k ey = = null )
variable . K ey = = null )
{
{
continue ;
continue ;
}
}
if ( variable . k ey. Equals ( key , StringComparison . CurrentCultureIgnoreCase ) )
if ( variable . K ey. Equals ( key , StringComparison . CurrentCultureIgnoreCase ) )
{
{
collision = true ;
collision = true ;
suffix + + ;
suffix + + ;
@ -557,9 +574,9 @@ namespace Fungus
}
}
}
}
/ * *
/// <summary>
* Returns a new Block key that is guaranteed not to clas h with any existing Block in the Flowchart .
/// 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 )
public virtual string GetUniqueBlockKey ( string originalKey , Block ignoreBlock = null )
{
{
int suffix = 0 ;
int suffix = 0 ;
@ -580,12 +597,12 @@ namespace Fungus
foreach ( Block block in blocks )
foreach ( Block block in blocks )
{
{
if ( block = = ignoreBlock | |
if ( block = = ignoreBlock | |
block . b lockName = = null )
block . B lockName = = null )
{
{
continue ;
continue ;
}
}
if ( block . b lockName. Equals ( key , StringComparison . CurrentCultureIgnoreCase ) )
if ( block . B lockName. Equals ( key , StringComparison . CurrentCultureIgnoreCase ) )
{
{
collision = true ;
collision = true ;
suffix + + ;
suffix + + ;
@ -600,9 +617,9 @@ namespace Fungus
}
}
}
}
/ * *
/// <summary>
* Returns a new Label key that is guaranteed not to clas h with any existing Label in the Block .
/// 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 )
public virtual string GetUniqueLabelKey ( string originalKey , Label ignoreLabel )
{
{
int suffix = 0 ;
int suffix = 0 ;
@ -614,13 +631,13 @@ namespace Fungus
baseKey = "New Label" ;
baseKey = "New Label" ;
}
}
Block block = ignoreLabel . p arentBlock;
Block block = ignoreLabel . P arentBlock;
string key = baseKey ;
string key = baseKey ;
while ( true )
while ( true )
{
{
bool collision = false ;
bool collision = false ;
foreach ( Command command in block . c ommandList)
foreach ( Command command in block . C ommandList)
{
{
Label label = command as Label ;
Label label = command as Label ;
if ( label = = null | |
if ( label = = null | |
@ -629,7 +646,7 @@ namespace Fungus
continue ;
continue ;
}
}
if ( label . k ey. Equals ( key , StringComparison . CurrentCultureIgnoreCase ) )
if ( label . K ey. Equals ( key , StringComparison . CurrentCultureIgnoreCase ) )
{
{
collision = true ;
collision = true ;
suffix + + ;
suffix + + ;
@ -644,18 +661,18 @@ namespace Fungus
}
}
}
}
/ * *
/// <summary>
* Returns the variable with the specified key , or null if the key is not found .
/// 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 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 .
/// You can then access the variable's value using the Value property. e.g .
* BooleanVariable boolVar = flowchart . GetVariable ( "MyBool" ) as BooleanVariable ;
/// BooleanVariable boolVar = flowchart.GetVariable("MyBool") as BooleanVariable;
* boolVar . Value = false ;
/// boolVar.Value = false ;
* /
/// </summary>
public Variable GetVariable ( string key )
public Variable GetVariable ( string key )
{
{
foreach ( Variable variable in variables )
foreach ( Variable variable in variables )
{
{
if ( variable ! = null & & variable . k ey = = key )
if ( variable ! = null & & variable . K ey = = key )
{
{
return variable ;
return variable ;
}
}
@ -664,17 +681,17 @@ namespace Fungus
return null ;
return null ;
}
}
/ * *
/// <summary>
* Returns the variable with the specified key , or null if the key is not found .
/// 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 .
/// You can then access the variable's value using the Value property. e.g .
* BooleanVariable boolVar = flowchart . GetVariable < BooleanVariable > ( "MyBool" ) ;
/// BooleanVariable boolVar = flowchart.GetVariable<BooleanVariable>("MyBool") ;
* boolVar . Value = false ;
/// boolVar.Value = false ;
* /
/// </summary>
public T GetVariable < T > ( string key ) where T : Variable
public T GetVariable < T > ( string key ) where T : Variable
{
{
foreach ( Variable variable in variables )
foreach ( Variable variable in variables )
{
{
if ( variable ! = null & & variable . k ey = = key )
if ( variable ! = null & & variable . K ey = = key )
{
{
return variable as T ;
return variable as T ;
}
}
@ -684,15 +701,15 @@ namespace Fungus
return null ;
return null ;
}
}
/ * *
/// <summary>
* Register a new variable with the Flowchart at runtime .
/// Register a new variable with the Flowchart at runtime.
* The variable should be added as a c omponent on the Flowchart game object .
/// 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
public void SetVariable < T > ( string key , T newvariable ) where T : Variable
{
{
foreach ( Variable v in variables )
foreach ( Variable v in variables )
{
{
if ( v ! = null & & v . k ey = = key )
if ( v ! = null & & v . K ey = = key )
{
{
T variable = v as T ;
T variable = v as T ;
if ( variable ! = null )
if ( variable ! = null )
@ -706,15 +723,15 @@ namespace Fungus
Debug . LogWarning ( "Variable " + key + " not found." ) ;
Debug . LogWarning ( "Variable " + key + " not found." ) ;
}
}
/ * *
/// <summary>
* Gets a list of all variables with public scope in this Flowchart .
/// Gets a list of all variables with public scope in this Flowchart .
* /
/// </summary>
public virtual List < Variable > GetPublicVariables ( )
public virtual List < Variable > GetPublicVariables ( )
{
{
List < Variable > publicVariables = new List < Variable > ( ) ;
List < Variable > publicVariables = new List < Variable > ( ) ;
foreach ( Variable v in variables )
foreach ( Variable v in variables )
{
{
if ( v ! = null & & v . s cope = = VariableScope . Public )
if ( v ! = null & & v . S cope = = VariableScope . Public )
{
{
publicVariables . Add ( v ) ;
publicVariables . Add ( v ) ;
}
}
@ -723,17 +740,17 @@ namespace Fungus
return publicVariables ;
return publicVariables ;
}
}
/ * *
/// <summary>
* Gets the value of a boolean variable .
/// Gets the value of a boolean variable.
* Returns false if the variable key does not exist .
/// Returns false if the variable key does not exist.
* /
/// </summary>
public virtual bool GetBooleanVariable ( string key )
public virtual bool GetBooleanVariable ( string key )
{
{
BooleanVariable variable = GetVariable < BooleanVariable > ( key ) ;
BooleanVariable variable = GetVariable < BooleanVariable > ( key ) ;
if ( variable ! = null )
if ( variable ! = null )
{
{
return GetVariable < BooleanVariable > ( key ) . v alue;
return GetVariable < BooleanVariable > ( key ) . V alue;
}
}
else
else
{
{
@ -741,30 +758,30 @@ namespace Fungus
}
}
}
}
/ * *
/// <summary>
* Sets the value of a boolean variable .
/// Sets the value of a boolean variable.
* The variable must already be added to the list of variables for this Flowchart .
/// The variable must already be added to the list of variables for this Flowchart.
* /
/// </summary>
public virtual void SetBooleanVariable ( string key , bool value )
public virtual void SetBooleanVariable ( string key , bool value )
{
{
BooleanVariable variable = GetVariable < BooleanVariable > ( key ) ;
BooleanVariable variable = GetVariable < BooleanVariable > ( key ) ;
if ( variable ! = null )
if ( variable ! = null )
{
{
variable . v alue = value ;
variable . V alue = value ;
}
}
}
}
/ * *
/// <summary>
* Gets the value of an integer variable .
/// Gets the value of an integer variable.
* Returns 0 if the variable key does not exist .
/// Returns 0 if the variable key does not exist.
* /
/// </summary>
public virtual int GetIntegerVariable ( string key )
public virtual int GetIntegerVariable ( string key )
{
{
IntegerVariable variable = GetVariable < IntegerVariable > ( key ) ;
IntegerVariable variable = GetVariable < IntegerVariable > ( key ) ;
if ( variable ! = null )
if ( variable ! = null )
{
{
return GetVariable < IntegerVariable > ( key ) . v alue;
return GetVariable < IntegerVariable > ( key ) . V alue;
}
}
else
else
{
{
@ -772,30 +789,30 @@ namespace Fungus
}
}
}
}
/ * *
/// <summary>
* Sets the value of an integer variable .
/// Sets the value of an integer variable.
* The variable must already be added to the list of variables for this Flowchart .
/// The variable must already be added to the list of variables for this Flowchart.
* /
/// </summary>
public virtual void SetIntegerVariable ( string key , int value )
public virtual void SetIntegerVariable ( string key , int value )
{
{
IntegerVariable variable = GetVariable < IntegerVariable > ( key ) ;
IntegerVariable variable = GetVariable < IntegerVariable > ( key ) ;
if ( variable ! = null )
if ( variable ! = null )
{
{
variable . v alue = value ;
variable . V alue = value ;
}
}
}
}
/ * *
/// <summary>
* Gets the value of a float variable .
/// Gets the value of a float variable.
* Returns 0 if the variable key does not exist .
/// Returns 0 if the variable key does not exist.
* /
/// </summary>
public virtual float GetFloatVariable ( string key )
public virtual float GetFloatVariable ( string key )
{
{
FloatVariable variable = GetVariable < FloatVariable > ( key ) ;
FloatVariable variable = GetVariable < FloatVariable > ( key ) ;
if ( variable ! = null )
if ( variable ! = null )
{
{
return GetVariable < FloatVariable > ( key ) . v alue;
return GetVariable < FloatVariable > ( key ) . V alue;
}
}
else
else
{
{
@ -803,30 +820,30 @@ namespace Fungus
}
}
}
}
/ * *
/// <summary>
* Sets the value of a float variable .
/// Sets the value of a float variable.
* The variable must already be added to the list of variables for this Flowchart .
/// The variable must already be added to the list of variables for this Flowchart.
* /
/// </summary>
public virtual void SetFloatVariable ( string key , float value )
public virtual void SetFloatVariable ( string key , float value )
{
{
FloatVariable variable = GetVariable < FloatVariable > ( key ) ;
FloatVariable variable = GetVariable < FloatVariable > ( key ) ;
if ( variable ! = null )
if ( variable ! = null )
{
{
variable . v alue = value ;
variable . V alue = value ;
}
}
}
}
/ * *
/// <summary>
* Gets the value of a string variable .
/// Gets the value of a string variable.
* Returns the empty string if the variable key does not exist .
/// Returns the empty string if the variable key does not exist.
* /
/// </summary>
public virtual string GetStringVariable ( string key )
public virtual string GetStringVariable ( string key )
{
{
StringVariable variable = GetVariable < StringVariable > ( key ) ;
StringVariable variable = GetVariable < StringVariable > ( key ) ;
if ( variable ! = null )
if ( variable ! = null )
{
{
return GetVariable < StringVariable > ( key ) . v alue;
return GetVariable < StringVariable > ( key ) . V alue;
}
}
else
else
{
{
@ -834,22 +851,22 @@ namespace Fungus
}
}
}
}
/ * *
/// <summary>
* Sets the value of a string variable .
/// Sets the value of a string variable.
* The variable must already be added to the list of variables for this Flowchart .
/// The variable must already be added to the list of variables for this Flowchart.
* /
/// </summary>
public virtual void SetStringVariable ( string key , string value )
public virtual void SetStringVariable ( string key , string value )
{
{
StringVariable variable = GetVariable < StringVariable > ( key ) ;
StringVariable variable = GetVariable < StringVariable > ( key ) ;
if ( variable ! = null )
if ( variable ! = null )
{
{
variable . v alue = value ;
variable . V alue = value ;
}
}
}
}
/ * *
/// <summary>
* 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.
* /
/// </summary>
public virtual void UpdateHideFlags ( )
public virtual void UpdateHideFlags ( )
{
{
if ( hideComponents )
if ( hideComponents )
@ -893,11 +910,17 @@ namespace Fungus
}
}
/// <summary>
/// Clears the list of selected commands.
/// </summary>
public virtual void ClearSelectedCommands ( )
public virtual void ClearSelectedCommands ( )
{
{
selectedCommands . Clear ( ) ;
selectedCommands . Clear ( ) ;
}
}
/// <summary>
/// Adds a command to the list of selected commands.
/// </summary>
public virtual void AddSelectedCommand ( Command command )
public virtual void AddSelectedCommand ( Command command )
{
{
if ( ! selectedCommands . Contains ( command ) )
if ( ! selectedCommands . Contains ( command ) )
@ -906,6 +929,9 @@ namespace Fungus
}
}
}
}
/// <summary>
/// Reset the commands and variables in the Flowchart.
/// </summary>
public virtual void Reset ( bool resetCommands , bool resetVariables )
public virtual void Reset ( bool resetCommands , bool resetVariables )
{
{
if ( resetCommands )
if ( resetCommands )
@ -926,9 +952,9 @@ namespace Fungus
}
}
}
}
/ * *
/// <summary>
* Override this in a Flowchart subclass to filter which commands are shown in the Add Command list .
/// Override this in a Flowchart subclass to filter which commands are shown in the Add Command list .
* /
/// </summary>
public virtual bool IsCommandSupported ( CommandInfoAttribute commandInfo )
public virtual bool IsCommandSupported ( CommandInfoAttribute commandInfo )
{
{
foreach ( string key in hideCommands )
foreach ( string key in hideCommands )
@ -944,9 +970,9 @@ namespace Fungus
return true ;
return true ;
}
}
/ * *
/// <summary>
* Returns true if there are any executing blocks in this Flowchart .
/// Returns true if there are any executing blocks in this Flowchart.
* /
/// </summary>
public virtual bool HasExecutingBlocks ( )
public virtual bool HasExecutingBlocks ( )
{
{
Block [ ] blocks = GetComponents < Block > ( ) ;
Block [ ] blocks = GetComponents < Block > ( ) ;
@ -960,9 +986,9 @@ namespace Fungus
return false ;
return false ;
}
}
/ * *
/// <summary>
* Returns a list of all executing blocks in this Flowchart .
/// Returns a list of all executing blocks in this Flowchart .
* /
/// </summary>
public virtual List < Block > GetExecutingBlocks ( )
public virtual List < Block > GetExecutingBlocks ( )
{
{
List < Block > executingBlocks = new List < Block > ( ) ;
List < Block > executingBlocks = new List < Block > ( ) ;
@ -979,11 +1005,11 @@ namespace Fungus
return executingBlocks ;
return executingBlocks ;
}
}
/ * *
/// <summary>
* Implementation of StringSubstituter . ISubstitutionHandler which matches any public variable in the Flowchart .
/// 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
/// To perform full variable substitution with al l substitution handlers in the scene, you should
* use the SubstituteVariables ( ) method instead .
/// use the SubstituteVariables() method instead .
* /
/// </summary>
[MoonSharp.Interpreter.MoonSharpHidden]
[MoonSharp.Interpreter.MoonSharpHidden]
public virtual bool SubstituteStrings ( StringBuilder input )
public virtual bool SubstituteStrings ( StringBuilder input )
{
{
@ -1004,8 +1030,8 @@ namespace Fungus
if ( variable = = null )
if ( variable = = null )
continue ;
continue ;
if ( variable . s cope = = VariableScope . Public & &
if ( variable . S cope = = VariableScope . Public & &
variable . k ey = = key )
variable . K ey = = key )
{
{
string value = variable . ToString ( ) ;
string value = variable . ToString ( ) ;
input . Replace ( match . Value , value ) ;
input . Replace ( match . Value , value ) ;
@ -1018,12 +1044,12 @@ namespace Fungus
return modified ;
return modified ;
}
}
/ * *
/// <summary>
* Substitute variables in the input text with the format { $ VarName }
/// Substitute variables in the input text with the format {$VarName }
* This will first match with private variables in this Flowchart , and then
/// This will first match with private variables in this Flowchart, and then
* with public variables in all Flowcharts in the scene ( and any component
/// with public variables in all Flowcharts in the scene (and any component
* in the scene that implements StringSubstituter . ISubstitutionHandler ) .
/// in the scene that implements StringSubstituter.ISubstitutionHandler) .
* /
/// </summary>
public virtual string SubstituteVariables ( string input )
public virtual string SubstituteVariables ( string input )
{
{
if ( stringSubstituer = = null )
if ( stringSubstituer = = null )
@ -1032,7 +1058,7 @@ namespace Fungus
}
}
// Use the string builder from StringSubstituter for efficiency.
// Use the string builder from StringSubstituter for efficiency.
StringBuilder sb = stringSubstituer . s tringBuilder;
StringBuilder sb = stringSubstituer . _ S tringBuilder;
sb . Length = 0 ;
sb . Length = 0 ;
sb . Append ( input ) ;
sb . Append ( input ) ;
@ -1053,8 +1079,8 @@ namespace Fungus
if ( variable = = null )
if ( variable = = null )
continue ;
continue ;
if ( variable . s cope = = VariableScope . Private & &
if ( variable . S cope = = VariableScope . Private & &
variable . k ey = = key )
variable . K ey = = key )
{
{
string value = variable . ToString ( ) ;
string value = variable . ToString ( ) ;
sb . Replace ( match . Value , value ) ;
sb . Replace ( match . Value , value ) ;
@ -1076,5 +1102,4 @@ namespace Fungus
}
}
}
}
}
}
}
}