Browse Source

Substitute variables into a string

Markup format = {$VarName}
master
chrisgregan 10 years ago
parent
commit
120e02ddef
  1. 5
      Assets/Fungus/FungusScript/Scripts/BooleanVariable.cs
  2. 5
      Assets/Fungus/FungusScript/Scripts/FloatVariable.cs
  3. 33
      Assets/Fungus/FungusScript/Scripts/FungusScript.cs
  4. 5
      Assets/Fungus/FungusScript/Scripts/IntegerVariable.cs
  5. 5
      Assets/Fungus/FungusScript/Scripts/StringVariable.cs

5
Assets/Fungus/FungusScript/Scripts/BooleanVariable.cs

@ -19,6 +19,11 @@ namespace Fungus
{
Value = false;
}
public override string ToString()
{
return Value.ToString();
}
}
[System.Serializable]

5
Assets/Fungus/FungusScript/Scripts/FloatVariable.cs

@ -18,6 +18,11 @@ namespace Fungus
{
Value = 0;
}
public override string ToString()
{
return Value.ToString();
}
}
[System.Serializable]

33
Assets/Fungus/FungusScript/Scripts/FungusScript.cs

@ -3,6 +3,7 @@ using System;
using System.Linq;
using System.Collections;
using System.Collections.Generic;
using System.Text.RegularExpressions;
namespace Fungus
{
@ -263,7 +264,7 @@ namespace Fungus
/**
* Gets the value of an integer variable.
* Returns false if the variable key does not exist.
* Returns 0 if the variable key does not exist.
*/
public virtual int GetIntegerVariable(string key)
{
@ -305,7 +306,7 @@ namespace Fungus
/**
* Gets the value of a float variable.
* Returns false if the variable key does not exist.
* Returns 0 if the variable key does not exist.
*/
public virtual float GetFloatVariable(string key)
{
@ -347,7 +348,7 @@ namespace Fungus
/**
* Gets the value of a string variable.
* Returns false if the variable key does not exist.
* Returns the empty string if the variable key does not exist.
*/
public virtual string GetStringVariable(string key)
{
@ -471,6 +472,32 @@ namespace Fungus
}
}
}
public virtual string SubstituteVariables(string text)
{
string subbedText = text;
// Instantiate the regular expression object.
Regex r = new Regex("{\\$.*?}");
// Match the regular expression pattern against a text string.
var results = r.Matches(text);
foreach (Match match in results)
{
string key = match.Value.Substring(2, match.Value.Length - 3);
foreach (Variable variable in variables)
{
if (variable.key == key)
{
string value = variable.ToString();
subbedText = subbedText.Replace(match.Value, value);
break;
}
}
}
return subbedText;
}
}
}

5
Assets/Fungus/FungusScript/Scripts/IntegerVariable.cs

@ -18,6 +18,11 @@ namespace Fungus
{
Value = 0;
}
public override string ToString()
{
return Value.ToString();
}
}
[System.Serializable]

5
Assets/Fungus/FungusScript/Scripts/StringVariable.cs

@ -18,6 +18,11 @@ namespace Fungus
{
Value = "";
}
public override string ToString()
{
return Value.ToString();
}
}
[System.Serializable]

Loading…
Cancel
Save