Browse Source

Variable substitution now also checks public variables

Also fixed a bug where only the first variable token in the string was
being replaced.
master
chrisgregan 10 years ago
parent
commit
3d6734c759
  1. 39
      Assets/Fungus/Flowchart/Scripts/Flowchart.cs

39
Assets/Fungus/Flowchart/Scripts/Flowchart.cs

@ -123,6 +123,11 @@ namespace Fungus
[SerializeField] [SerializeField]
protected int nextItemId = 0; protected int nextItemId = 0;
/**
* Cached list of flowchart objects in the scene for fast lookup
*/
public static List<Flowchart> cachedFlowcharts = new List<Flowchart>();
/** /**
* 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
@ -135,6 +140,11 @@ namespace Fungus
public virtual void OnEnable() 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. // Assign an item id to any block or command that doesn't have one yet.
// This should only happen after loading a legacy Flowchart // This should only happen after loading a legacy Flowchart
Block[] blocks = GetComponentsInChildren<Block>(); Block[] blocks = GetComponentsInChildren<Block>();
@ -156,6 +166,11 @@ namespace Fungus
} }
} }
public virtual void OnDisable()
{
cachedFlowcharts.Remove(this);
}
protected virtual Block CreateBlockComponent(GameObject parent) protected virtual Block CreateBlockComponent(GameObject parent)
{ {
Block block = parent.AddComponent<Block>(); Block block = parent.AddComponent<Block>();
@ -679,14 +694,33 @@ namespace Fungus
{ {
string key = match.Value.Substring(2, match.Value.Length - 3); 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) foreach (Variable variable in variables)
{ {
if (variable.key == key) if (variable.key == key)
{ {
string value = variable.ToString(); string value = variable.ToString();
subbedText = subbedText.Replace(match.Value, value); 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) if (localizedString != null)
{ {
subbedText = subbedText.Replace(match.Value, localizedString); subbedText = subbedText.Replace(match.Value, localizedString);
return subbedText;
} }
} }

Loading…
Cancel
Save