From 120e02ddefe0603f2016a54ae488319c1094cbb7 Mon Sep 17 00:00:00 2001 From: chrisgregan Date: Fri, 5 Dec 2014 17:13:18 +0000 Subject: [PATCH] Substitute variables into a string Markup format = {$VarName} --- .../FungusScript/Scripts/BooleanVariable.cs | 5 +++ .../FungusScript/Scripts/FloatVariable.cs | 5 +++ .../FungusScript/Scripts/FungusScript.cs | 33 +++++++++++++++++-- .../FungusScript/Scripts/IntegerVariable.cs | 5 +++ .../FungusScript/Scripts/StringVariable.cs | 5 +++ 5 files changed, 50 insertions(+), 3 deletions(-) diff --git a/Assets/Fungus/FungusScript/Scripts/BooleanVariable.cs b/Assets/Fungus/FungusScript/Scripts/BooleanVariable.cs index 731a9aa4..a37801d0 100644 --- a/Assets/Fungus/FungusScript/Scripts/BooleanVariable.cs +++ b/Assets/Fungus/FungusScript/Scripts/BooleanVariable.cs @@ -19,6 +19,11 @@ namespace Fungus { Value = false; } + + public override string ToString() + { + return Value.ToString(); + } } [System.Serializable] diff --git a/Assets/Fungus/FungusScript/Scripts/FloatVariable.cs b/Assets/Fungus/FungusScript/Scripts/FloatVariable.cs index 95070811..8da24b63 100644 --- a/Assets/Fungus/FungusScript/Scripts/FloatVariable.cs +++ b/Assets/Fungus/FungusScript/Scripts/FloatVariable.cs @@ -18,6 +18,11 @@ namespace Fungus { Value = 0; } + + public override string ToString() + { + return Value.ToString(); + } } [System.Serializable] diff --git a/Assets/Fungus/FungusScript/Scripts/FungusScript.cs b/Assets/Fungus/FungusScript/Scripts/FungusScript.cs index 06332c82..faffc429 100644 --- a/Assets/Fungus/FungusScript/Scripts/FungusScript.cs +++ b/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; + } } } \ No newline at end of file diff --git a/Assets/Fungus/FungusScript/Scripts/IntegerVariable.cs b/Assets/Fungus/FungusScript/Scripts/IntegerVariable.cs index 019962f6..fdd86366 100644 --- a/Assets/Fungus/FungusScript/Scripts/IntegerVariable.cs +++ b/Assets/Fungus/FungusScript/Scripts/IntegerVariable.cs @@ -18,6 +18,11 @@ namespace Fungus { Value = 0; } + + public override string ToString() + { + return Value.ToString(); + } } [System.Serializable] diff --git a/Assets/Fungus/FungusScript/Scripts/StringVariable.cs b/Assets/Fungus/FungusScript/Scripts/StringVariable.cs index 5a5d34c0..c5c8160a 100644 --- a/Assets/Fungus/FungusScript/Scripts/StringVariable.cs +++ b/Assets/Fungus/FungusScript/Scripts/StringVariable.cs @@ -18,6 +18,11 @@ namespace Fungus { Value = ""; } + + public override string ToString() + { + return Value.ToString(); + } } [System.Serializable]