diff --git a/Assets/Fungus/Flowchart/Scripts/Flowchart.cs b/Assets/Fungus/Flowchart/Scripts/Flowchart.cs index daeb85e6..8c2616b4 100644 --- a/Assets/Fungus/Flowchart/Scripts/Flowchart.cs +++ b/Assets/Fungus/Flowchart/Scripts/Flowchart.cs @@ -123,6 +123,11 @@ namespace Fungus [SerializeField] protected int nextItemId = 0; + /** + * Cached list of flowchart objects in the scene for fast lookup + */ + public static List cachedFlowcharts = new List(); + /** * Returns the next id to assign to a new flowchart item. * Item ids increase monotically so they are guaranteed to @@ -135,6 +140,11 @@ namespace Fungus public virtual void OnEnable() { + if (!cachedFlowcharts.Contains(this)) + { + cachedFlowcharts.Add(this); + } + // Assign an item id to any block or command that doesn't have one yet. // This should only happen after loading a legacy Flowchart Block[] blocks = GetComponentsInChildren(); @@ -156,6 +166,11 @@ namespace Fungus } } + public virtual void OnDisable() + { + cachedFlowcharts.Remove(this); + } + protected virtual Block CreateBlockComponent(GameObject parent) { Block block = parent.AddComponent(); @@ -679,14 +694,33 @@ namespace Fungus { string key = match.Value.Substring(2, match.Value.Length - 3); - // Look for matching variable first + // Look for any matching variables in this Flowchart first (public or private) foreach (Variable variable in variables) { if (variable.key == key) { string value = variable.ToString(); subbedText = subbedText.Replace(match.Value, value); - return subbedText; + } + } + + // Now search all public variables in all scene Flowcharts in the scene + foreach (Flowchart flowchart in cachedFlowcharts) + { + if (flowchart == this) + { + // We've already searched this flowchart + continue; + } + + foreach (Variable variable in flowchart.variables) + { + if (variable.scope == VariableScope.Public && + variable.key == key) + { + string value = variable.ToString(); + subbedText = subbedText.Replace(match.Value, value); + } } } @@ -695,7 +729,6 @@ namespace Fungus if (localizedString != null) { subbedText = subbedText.Replace(match.Value, localizedString); - return subbedText; } }