diff --git a/Assets/Fungus/Legacy/PageCommands.cs b/Assets/Fungus/Legacy/PageCommands.cs index 89d2e94e..57c5c19d 100644 --- a/Assets/Fungus/Legacy/PageCommands.cs +++ b/Assets/Fungus/Legacy/PageCommands.cs @@ -149,7 +149,7 @@ namespace Fungus public override void Execute(CommandQueue commandQueue, Action onComplete) { - string subbedText = Game.stringTable.SubstituteStrings(chooseText); + string subbedText = Variables.SubstituteStrings(chooseText); IDialog dialog = Game.GetInstance().GetDialog(); PageController pageController = dialog as PageController; diff --git a/Assets/Fungus/Prefabs/ParallaxSprite.prefab b/Assets/Fungus/Prefabs/ParallaxSprite.prefab index 977746e4..ffab647b 100644 Binary files a/Assets/Fungus/Prefabs/ParallaxSprite.prefab and b/Assets/Fungus/Prefabs/ParallaxSprite.prefab differ diff --git a/Assets/Fungus/Scripts/Button.cs b/Assets/Fungus/Scripts/Button.cs index 9d885cac..3ea8cc59 100644 --- a/Assets/Fungus/Scripts/Button.cs +++ b/Assets/Fungus/Scripts/Button.cs @@ -23,9 +23,9 @@ namespace Fungus public bool autoHide = true; /** - * Automatically hides the button when the specified game value is set (i.e. not equal to zero). + * Automatically hides the button when the named boolean variable is set using SetBoolean() */ - public string hideOnSetValue; + public string hideOnBoolean; /** * Sound effect to play when button is clicked. @@ -96,9 +96,9 @@ namespace Fungus } } - // Hide the button if the specified game value is non-zero - if (hideOnSetValue.Length > 0 && - Game.GetInstance().GetGameValue(hideOnSetValue) != 0) + // Hide the button if the specified boolean variable is true + if (hideOnBoolean.Length > 0 && + Variables.GetBoolean(hideOnBoolean)) { targetAlpha = 0f; } diff --git a/Assets/Fungus/Scripts/Commands/DialogCommands.cs b/Assets/Fungus/Scripts/Commands/DialogCommands.cs index 675cf3e3..cf3c6535 100644 --- a/Assets/Fungus/Scripts/Commands/DialogCommands.cs +++ b/Assets/Fungus/Scripts/Commands/DialogCommands.cs @@ -24,7 +24,7 @@ namespace Fungus public override void Execute(CommandQueue commandQueue, Action onComplete) { - string subbedText = Game.stringTable.SubstituteStrings(sayText); + string subbedText = Variables.SubstituteStrings(sayText); IDialog sayDialog = Game.GetInstance().GetDialog(); sayDialog.Say(subbedText, onComplete); @@ -50,7 +50,7 @@ namespace Fungus { IDialog characterDialog = Game.GetInstance().GetDialog(); - string subbedText = Game.stringTable.FormatLinkText(Game.stringTable.SubstituteStrings(optionText)); + string subbedText = Variables.FormatLinkText(Variables.SubstituteStrings(optionText)); characterDialog.AddOption(subbedText, optionAction); if (onComplete != null) diff --git a/Assets/Fungus/Scripts/Commands/GameCommands.cs b/Assets/Fungus/Scripts/Commands/GameCommands.cs index e6a67dda..5755543b 100644 --- a/Assets/Fungus/Scripts/Commands/GameCommands.cs +++ b/Assets/Fungus/Scripts/Commands/GameCommands.cs @@ -176,99 +176,5 @@ namespace Fungus // This command resets the command queue so no need to call onComplete } } - - /** - * Sets a globally accessible integer value - */ - public class SetValue : CommandQueue.Command - { - Action onExecute; - - public SetValue(string key, int value) - { - onExecute = delegate { - Game.GetInstance().SetGameValue(key, value); - }; - } - - public override void Execute(CommandQueue commandQueue, Action onComplete) - { - if (onExecute != null) - onExecute(); - - if (onComplete != null) - onComplete(); - } - } - - /** - * Sets a globally accessible string value - */ - public class SetString : CommandQueue.Command - { - Action onExecute; - - public SetString(string key, string value) - { - onExecute = delegate { - Game.stringTable.SetString(key, value); - }; - } - - public override void Execute(CommandQueue commandQueue, Action onComplete) - { - if (onExecute != null) - onExecute(); - - if (onComplete != null) - onComplete(); - } - } - - /** - * Save the current game state to persistant storage. - */ - public class Save : CommandQueue.Command - { - Action onExecute; - - public Save(string saveName) - { - onExecute = delegate { - Game.GetInstance().SaveGame(saveName); - }; - } - - public override void Execute(CommandQueue commandQueue, Action onComplete) - { - onExecute(); - - if (onComplete != null) - onComplete(); - } - } - - /** - * Load the game state from persistant storage. - */ - public class Load : CommandQueue.Command - { - Action onExecute; - - public Load(string saveName) - { - onExecute = delegate { - Game.GetInstance().LoadGame(saveName); - }; - } - - public override void Execute(CommandQueue commandQueue, Action onComplete) - { - onExecute(); - - if (onComplete != null) - onComplete(); - } - } } } \ No newline at end of file diff --git a/Assets/Fungus/Scripts/Game.cs b/Assets/Fungus/Scripts/Game.cs index 29967367..231dde2a 100644 --- a/Assets/Fungus/Scripts/Game.cs +++ b/Assets/Fungus/Scripts/Game.cs @@ -31,6 +31,13 @@ namespace Fungus */ public bool showLinks = true; + /** + * Deletes all previously stored variables on launch. + * Disable this if you want to have game variables persist between game launches. + * It is then up to you to provide an in-game option to reset game variables. + */ + public bool deleteVariables = true; + /** * Time for fade transition to complete when moving to a different Room. */ @@ -56,15 +63,6 @@ namespace Fungus */ public Texture2D loadingImage; - /** - * Global dictionary of integer values for storing game state. - */ - [HideInInspector] - static public Dictionary values = new Dictionary(); - - [HideInInspector] - static public StringTable stringTable = new StringTable(); - [HideInInspector] public CommandQueue commandQueue; @@ -125,6 +123,11 @@ namespace Fungus commandQueue.AddCommand(new Command.MoveToRoom(activeRoom)); commandQueue.Execute(); } + + if (deleteVariables) + { + Variables.DeleteAll(); + } } public virtual void Update() @@ -170,30 +173,6 @@ namespace Fungus return false; } - /** - * Sets a globally accessible game state value. - * @param key The key of the value. - * @param value The integer value to store. - */ - public void SetGameValue(string key, int value) - { - values[key] = value; - } - - /** - * Gets a globally accessible game state value. - * @param key The key of the value. - * @return The integer value for the specified key, or 0 if the key does not exist. - */ - public int GetGameValue(string key) - { - if (values.ContainsKey(key)) - { - return values[key]; - } - return 0; - } - /** * Loads a new scene and displays an optional loading image. * This is useful for splitting a large Fungus game across multiple scene files to reduce peak memory usage. @@ -211,27 +190,10 @@ namespace Fungus * Only the values, string table and current scene are stored. * @param saveName The name of the saved game data. */ + [Obsolete("Use Variables.Save() instead.")] public void SaveGame(string saveName) { - // Store loaded scene name in save data - SetString("Fungus.Scene", Application.loadedLevelName); - - var b = new BinaryFormatter(); - var m = new MemoryStream(); - - // Save values - { - b.Serialize(m, values); - string s = Convert.ToBase64String(m.GetBuffer()); - PlayerPrefs.SetString(saveName + ".values", s); - } - - // Save string table - { - b.Serialize(m, stringTable.stringDict); - string s = Convert.ToBase64String(m.GetBuffer()); - PlayerPrefs.SetString(saveName + ".stringTable", s); - } + Variables.Save(); } /** @@ -240,34 +202,13 @@ namespace Fungus * Each scene in your game should contain the necessary code to restore the current game state based on saved data. * @param saveName The name of the saved game data. */ - public void LoadGame(string saveName) + public void LoadGame(string saveName = "_fungus") { - // Load values - { - var data = PlayerPrefs.GetString(saveName + ".values"); - if (!string.IsNullOrEmpty(data)) - { - var b = new BinaryFormatter(); - var m = new MemoryStream(Convert.FromBase64String(data)); - values = (Dictionary)b.Deserialize(m); - } - } - - // Load string table - { - var data = PlayerPrefs.GetString(saveName + ".stringTable"); - if (!string.IsNullOrEmpty(data)) - { - var b = new BinaryFormatter(); - var m = new MemoryStream(Convert.FromBase64String(data)); - stringTable.stringDict = b.Deserialize(m) as Dictionary; - } - } - - string sceneName = GetString("Fungus.Scene"); + Variables.SetSaveName(saveName); + string sceneName = Variables.GetString("_scene"); if (sceneName.Length > 0) { - LoadScene(sceneName, true); + MoveToScene(sceneName); } } } diff --git a/Assets/Fungus/Scripts/GameController.cs b/Assets/Fungus/Scripts/GameController.cs index 4ffbd2f7..75927b6c 100644 --- a/Assets/Fungus/Scripts/GameController.cs +++ b/Assets/Fungus/Scripts/GameController.cs @@ -88,20 +88,33 @@ namespace Fungus * Save the current game state to persistant storage. * @param saveName The name of the saved game data. */ - public static void Save(string saveName = "Fungus.Save") + [Obsolete("Use Save() instead and use Variables.SetSaveName() if you need to set the save name.")] + public static void Save(string saveName) + { + GameController.Save(); + } + + /** + * Saves the current game variables to persistant storage. + */ + public static void Save() { CommandQueue commandQueue = Game.GetInstance().commandQueue; - commandQueue.AddCommand(new Command.Save(saveName)); + commandQueue.AddCommand(new Command.Call(delegate { + Variables.Save(); + })); } /** * Load the current game state from persistant storage. * @param saveName The name of the saved game data. */ - public static void Load(string saveName = "Fungus.Save") + public static void Load(string saveName = "_fungus") { CommandQueue commandQueue = Game.GetInstance().commandQueue; - commandQueue.AddCommand(new Command.Load(saveName)); + commandQueue.AddCommand(new Command.Call(delegate { + Game.GetInstance().LoadGame(saveName); + })); } #endregion @@ -313,10 +326,10 @@ namespace Fungus * @param key The name of the value to set * @param value The value to set */ + [Obsolete("Use SetInteger() instead.")] public static void SetValue(string key, int value) { - CommandQueue commandQueue = Game.GetInstance().commandQueue; - commandQueue.AddCommand(new Command.SetValue(key, value)); + SetInteger(key, value); } /** @@ -324,9 +337,10 @@ namespace Fungus * This method returns immediately but it queues an asynchronous command for later execution. * @param key The name of the value to set */ + [Obsolete("Use SetInteger() instead")] public static void SetValue(string key) { - SetValue(key, 1); + SetInteger(key, 1); } /** @@ -334,9 +348,10 @@ namespace Fungus * This method returns immediately but it queues an asynchronous command for later execution. * @param key The key of the value. */ + [Obsolete("Use Variables.DeleteKey() instead")] public static void ClearValue(string key) { - SetValue(key, 0); + Variables.DeleteKey(key); } /** @@ -345,9 +360,21 @@ namespace Fungus * @param key The name of the value * @return The integer value for this key, or 0 if not previously set. */ + [Obsolete("Use Variables.GetInteger() instead")] public static int GetValue(string key) { - return Game.GetInstance().GetGameValue(key); + return Variables.GetInteger(key); + } + + /** + * Gets a globally accessible string value. + * @param key The name of the value + * @return The string value for this key, or the empty string if not previously set. + */ + [Obsolete("Use Variables.GetString() instead")] + public static string GetString(string key) + { + return Variables.GetString(key); } /** @@ -355,31 +382,66 @@ namespace Fungus * @param key The name of the value to check. * @return Returns true if the value is not equal to zero. */ + [Obsolete("Use Variables.GetInteger() or Variables.HasKey() instead")] public static bool HasValue(string key) { - return GetValue(key) != 0; + return Variables.GetInteger(key) != 0; } /** - * Sets a globally accessible string value. + * Sets a globally accessible boolean value. * This method returns immediately but it queues an asynchronous command for later execution. * @param key The name of the value to set - * @param value The string value to set + * @param value The boolean value to set */ - public static void SetString(string key, string value) + public static void SetBoolean(string key, bool value) { CommandQueue commandQueue = Game.GetInstance().commandQueue; - commandQueue.AddCommand(new Command.SetString(key, value)); + commandQueue.AddCommand(new Command.Call(delegate { + Variables.SetBoolean(key, value); + })); } /** - * Gets a globally accessible string value. - * @param key The name of the value - * @return The string value for this key, or the empty string if not previously set. + * Sets a globally accessible integer value. + * This method returns immediately but it queues an asynchronous command for later execution. + * @param key The name of the value to set + * @param value The integer value to set */ - public static string GetString(string key) + public static void SetInteger(string key, int value) + { + CommandQueue commandQueue = Game.GetInstance().commandQueue; + commandQueue.AddCommand(new Command.Call(delegate { + Variables.SetInteger(key, value); + })); + } + + /** + * Sets a globally accessible float value. + * This method returns immediately but it queues an asynchronous command for later execution. + * @param key The name of the value to set + * @param value The flaot value to set + */ + public static void SetFloat(string key, float value) + { + CommandQueue commandQueue = Game.GetInstance().commandQueue; + commandQueue.AddCommand(new Command.Call(delegate { + Variables.SetFloat(key, value); + })); + } + + /** + * Sets a globally accessible string value. + * This method returns immediately but it queues an asynchronous command for later execution. + * @param key The name of the value to set + * @param value The string value to set + */ + public static void SetString(string key, string value) { - return Game.stringTable.GetString(key); + CommandQueue commandQueue = Game.GetInstance().commandQueue; + commandQueue.AddCommand(new Command.Call(delegate { + Variables.SetString(key, value); + })); } #endregion @@ -579,7 +641,7 @@ namespace Fungus * This method returns immediately but it queues an asynchronous command for later execution. * @param page A Page object which defines the screen rect to use when rendering story text. */ - [Obsolete("Pages are deprecated. Please use the new Dialog system instead.")] + [Obsolete("Pages are obsolete. Please use the new Dialog system instead.")] public static void SetPage(Page page, PageController.Layout pageLayout = PageController.Layout.FullSize) { CommandQueue commandQueue = Game.GetInstance().commandQueue; @@ -591,7 +653,7 @@ namespace Fungus * This method returns immediately but it queues an asynchronous command for later execution. * @param screenRect A ScreenRect object which defines a rect in normalized screen space coordinates. */ - [Obsolete("Pages are deprecated. Please use the new Dialog system instead.")] + [Obsolete("Pages are obsolete. Please use the new Dialog system instead.")] public static void SetPageRect(PageController.ScreenRect screenRect, PageController.Layout pageLayout = PageController.Layout.FullSize) { CommandQueue commandQueue = Game.GetInstance().commandQueue; @@ -609,7 +671,7 @@ namespace Fungus * @param y2 Page rect bottom coordinate in normalized screen space coords [0..1] * @param pageLayout Layout mode for positioning page within the rect. */ - [Obsolete("Pages are deprecated. Please use the new Dialog system instead.")] + [Obsolete("Pages are obsolete. Please use the new Dialog system instead.")] public static void SetPageRect(float x1, float y1, float x2, float y2, PageController.Layout pageLayout = PageController.Layout.FullSize) { PageController.ScreenRect screenRect = new PageController.ScreenRect(); @@ -627,7 +689,7 @@ namespace Fungus * @param scaleY Scales the height of the Page [0..1]. 1 = full screen height. * @param pageLayout Controls how the Page is positioned and sized based on the displayed content. */ - [Obsolete("Pages are deprecated. Please use the new Dialog system instead.")] + [Obsolete("Pages are obsolete. Please use the new Dialog system instead.")] public static void SetPageTop(float scaleX, float scaleY, PageController.Layout pageLayout) { PageController.ScreenRect screenRect = PageController.CalcScreenRect(new Vector2(scaleX, scaleY), PageController.PagePosition.Top); @@ -638,7 +700,7 @@ namespace Fungus * Display story page at the top of the screen. * This method returns immediately but it queues an asynchronous command for later execution. */ - [Obsolete("Pages are deprecated. Please use the new Dialog system instead.")] + [Obsolete("Pages are obsolete. Please use the new Dialog system instead.")] public static void SetPageTop() { Vector2 pageScale = Game.GetInstance().pageController.defaultPageScale; @@ -652,7 +714,7 @@ namespace Fungus * @param scaleY Scales the height of the Page [0..1]. 1 = full screen height. * @param pageLayout Controls how the Page is positioned and sized based on the displayed content. */ - [Obsolete("Pages are deprecated. Please use the new Dialog system instead.")] + [Obsolete("Pages are obsolete. Please use the new Dialog system instead.")] public static void SetPageMiddle(float scaleX, float scaleY, PageController.Layout pageLayout) { PageController.ScreenRect screenRect = PageController.CalcScreenRect(new Vector2(scaleX, scaleY), PageController.PagePosition.Middle); @@ -663,7 +725,7 @@ namespace Fungus * Display story page at the middle of the screen. * This method returns immediately but it queues an asynchronous command for later execution. */ - [Obsolete("Pages are deprecated. Please use the new Dialog system instead.")] + [Obsolete("Pages are obsolete. Please use the new Dialog system instead.")] public static void SetPageMiddle() { Vector2 pageScale = Game.GetInstance().pageController.defaultPageScale; @@ -677,7 +739,7 @@ namespace Fungus * @param scaleY Scales the height of the Page [0..1]. 1 = full screen height. * @param pageLayout Controls how the Page is positioned and sized based on the displayed content. */ - [Obsolete("Pages are deprecated. Please use the new Dialog system instead.")] + [Obsolete("Pages are obsolete. Please use the new Dialog system instead.")] public static void SetPageBottom(float scaleX, float scaleY, PageController.Layout pageLayout) { PageController.ScreenRect screenRect = PageController.CalcScreenRect(new Vector2(scaleX, scaleY), PageController.PagePosition.Bottom); @@ -688,7 +750,7 @@ namespace Fungus * Display story page at the bottom of the screen. * This method returns immediately but it queues an asynchronous command for later execution. */ - [Obsolete("Pages are deprecated. Please use the new Dialog system instead.")] + [Obsolete("Pages are obsolete. Please use the new Dialog system instead.")] public static void SetPageBottom() { Vector2 pageScale = Game.GetInstance().pageController.defaultPageScale; @@ -700,7 +762,7 @@ namespace Fungus * This method returns immediately but it queues an asynchronous command for later execution. * @param pageStyle The style object to make active */ - [Obsolete("Pages are deprecated. Please use the new Dialog system instead.")] + [Obsolete("Pages are obsolete. Please use the new Dialog system instead.")] public static void SetPageStyle(PageStyle pageStyle) { CommandQueue commandQueue = Game.GetInstance().commandQueue; @@ -710,7 +772,7 @@ namespace Fungus /** * Obsolete: Use SetHeader() instead. */ - [Obsolete("Pages are deprecated. Please use the new Dialog system instead.")] + [Obsolete("Pages are obsolete. Please use the new Dialog system instead.")] public static void Title(string titleText) { SetHeader(titleText); @@ -722,7 +784,7 @@ namespace Fungus * This method returns immediately but it queues an asynchronous command for later execution. * @param footerText The text to display as the header of the Page. */ - [Obsolete("Pages are deprecated. Please use the new Dialog system instead.")] + [Obsolete("Pages are obsolete. Please use the new Dialog system instead.")] public static void SetHeader(string headerText) { CommandQueue commandQueue = Game.GetInstance().commandQueue; @@ -735,7 +797,7 @@ namespace Fungus * This method returns immediately but it queues an asynchronous command for later execution. * @param footerText The text to display as the footer of the Page. */ - [Obsolete("Pages are deprecated. Please use the new Dialog system instead.")] + [Obsolete("Pages are obsolete. Please use the new Dialog system instead.")] public static void SetFooter(string footerText) { CommandQueue commandQueue = Game.GetInstance().commandQueue; diff --git a/Assets/Fungus/Scripts/SceneLoader.cs b/Assets/Fungus/Scripts/SceneLoader.cs index ef44f669..93760200 100644 --- a/Assets/Fungus/Scripts/SceneLoader.cs +++ b/Assets/Fungus/Scripts/SceneLoader.cs @@ -82,7 +82,7 @@ namespace Fungus Game game = Game.GetInstance(); if (game != null) { - game.SaveGame("Fungus.Save"); + Variables.Save(); } } diff --git a/Assets/Fungus/Scripts/StringTable.cs b/Assets/Fungus/Scripts/StringTable.cs deleted file mode 100644 index 29f035d9..00000000 --- a/Assets/Fungus/Scripts/StringTable.cs +++ /dev/null @@ -1,87 +0,0 @@ -using UnityEngine; -using System.Collections; -using System.Collections.Generic; -using System.Text.RegularExpressions; - -namespace Fungus -{ - /** - * Stores long or frequently repeated strings in a dictionary. - * Strings can then be retrieved using a short key string. - */ - public class StringTable - { - public Dictionary stringDict = new Dictionary(); - - /** - * Removes all strings from the string table. - */ - public void ClearStringTable() - { - stringDict.Clear(); - } - - /** - * Retrieves a string from the table by key. - */ - public string GetString(string key) - { - if (stringDict.ContainsKey(key)) - { - return stringDict[key]; - } - return ""; - } - - /** - * Adds or updates a string in the table. - */ - public void SetString(string key, string value) - { - stringDict[key] = value; - } - - /** - * Replace keys in the input string with the string table entry. - * Example format: "This {string_key} string" - */ - public string SubstituteStrings(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 stringKey = match.Value.Substring(1, match.Value.Length - 2); - string stringValue = GetString(stringKey); - - subbedText = subbedText.Replace(match.Value, stringValue); - } - - return subbedText; - } - - /** - * Chops a string at the first new line character encountered. - * This is useful for link / button strings that must fit on a single line. - */ - public string FormatLinkText(string text) - { - string trimmed; - if (text.Contains("\n")) - { - trimmed = text.Substring(0, text.IndexOf("\n")); - } - else - { - trimmed = text; - } - - return trimmed; - } - } -} \ No newline at end of file diff --git a/Assets/Fungus/Scripts/StringsParser.cs b/Assets/Fungus/Scripts/StringsParser.cs index df812dc8..d266dd0f 100644 --- a/Assets/Fungus/Scripts/StringsParser.cs +++ b/Assets/Fungus/Scripts/StringsParser.cs @@ -88,7 +88,7 @@ namespace Fungus // Trim off last newline blockBuffer = blockBuffer.TrimEnd( '\r', '\n', ' ', '\t'); - Game.stringTable.SetString(blockTag, blockBuffer); + Variables.SetString(blockTag, blockBuffer); } // Prepare to parse next block diff --git a/Assets/Fungus/Scripts/Variables.cs b/Assets/Fungus/Scripts/Variables.cs new file mode 100644 index 00000000..11e30af0 --- /dev/null +++ b/Assets/Fungus/Scripts/Variables.cs @@ -0,0 +1,175 @@ +using UnityEngine; +using System.Collections; +using System.Text.RegularExpressions; + +namespace Fungus +{ + /** + * Peristent data storage class for tracking game state. + * Provides a basic save game system. + */ + public class Variables + { + static string saveName = "_fungus"; + + /** + * Sets a name to prefix before all keys used with Set & Get methods. + * You can use this to support multiple save data profiles, (e.g. for multiple users or a list of checkpoints). + */ + static public void SetSaveName(string _saveName) + { + saveName = _saveName; + } + + /** + * Save the variable state to persistent storage. + * The currently loaded scene name is stored so that Game.LoadGame() will automatically move to the appropriate scene. + */ + static public void Save() + { + SetString("_scene", Application.loadedLevelName); + PlayerPrefs.Save(); + } + + /** + * Deletes all stored variables. + */ + static public void DeleteAll() + { + PlayerPrefs.DeleteAll(); + } + + /** + * Deletes a single stored variable. + */ + static public void DeleteKey(string key) + { + PlayerPrefs.DeleteKey(AddPrefix(key)); + } + + /** + * Returns the float variable associated with the key. + */ + static public float GetFloat(string key) + { + return PlayerPrefs.GetFloat(AddPrefix(key)); + } + + /** + * Returns the integer variable associated with the key. + */ + static public int GetInteger(string key) + { + return PlayerPrefs.GetInt(AddPrefix(key)); + } + + /** + * Returns the boolean variable associated with the key. + */ + static public bool GetBoolean(string key) + { + return (bool)(PlayerPrefs.GetInt(AddPrefix(key)) != 0); + } + + /** + * Returns the string variable associated with the key. + */ + static public string GetString(string key) + { + return PlayerPrefs.GetString(AddPrefix(key)); + } + + /** + * Returns true if a variable has been stored with this key. + */ + static public bool HasKey(string key) + { + return PlayerPrefs.HasKey(AddPrefix(key)); + } + + /** + * Stores a float variable using the key. + */ + static public void SetFloat(string key, float value) + { + PlayerPrefs.SetFloat(AddPrefix(key), value); + } + + /** + * Stores an integer variable using the key. + */ + static public void SetInteger(string key, int value) + { + PlayerPrefs.SetInt(AddPrefix(key), value); + } + + /** + * Stores a boolean variable using the key. + */ + static public void SetBoolean(string key, bool value) + { + PlayerPrefs.SetInt(AddPrefix(key), value ? 1 : 0); + } + + /** + * Stores a string variable using the key. + */ + static public void SetString(string key, string value) + { + PlayerPrefs.SetString(AddPrefix(key), value); + } + + /** + * Replace keys in the input string with the string table entry. + * Example format: "This {string_key} string" + */ + static public string SubstituteStrings(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 stringKey = match.Value.Substring(1, match.Value.Length - 2); + string stringValue = GetString(stringKey); + + subbedText = subbedText.Replace(match.Value, stringValue); + } + + return subbedText; + } + + /** + * Chops a string at the first new line character encountered. + * This is useful for link / button strings that must fit on a single line. + */ + static public string FormatLinkText(string text) + { + string trimmed; + if (text.Contains("\n")) + { + trimmed = text.Substring(0, text.IndexOf("\n")); + } + else + { + trimmed = text; + } + + return trimmed; + } + + static string AddPrefix(string key) + { + if (saveName.Length == 0) + { + return key; + } + + return saveName + "." + key; + } + } +} \ No newline at end of file diff --git a/Assets/Fungus/Scripts/StringTable.cs.meta b/Assets/Fungus/Scripts/Variables.cs.meta similarity index 78% rename from Assets/Fungus/Scripts/StringTable.cs.meta rename to Assets/Fungus/Scripts/Variables.cs.meta index 740610b3..f9a33b1a 100644 --- a/Assets/Fungus/Scripts/StringTable.cs.meta +++ b/Assets/Fungus/Scripts/Variables.cs.meta @@ -1,5 +1,5 @@ fileFormatVersion: 2 -guid: 43c44334e66c44af1a071056e4fde1f9 +guid: 6552eb7d3f623479097c813cc27dcf92 MonoImporter: serializedVersion: 2 defaultReferences: [] diff --git a/Assets/FungusExample/Scenes/Example.unity b/Assets/FungusExample/Scenes/Example.unity index caec8d76..fe13dda6 100644 Binary files a/Assets/FungusExample/Scenes/Example.unity and b/Assets/FungusExample/Scenes/Example.unity differ diff --git a/Assets/FungusExample/Scripts/AudioRoom.cs b/Assets/FungusExample/Scripts/AudioRoom.cs index a14c8ccb..98eabf5f 100644 --- a/Assets/FungusExample/Scripts/AudioRoom.cs +++ b/Assets/FungusExample/Scripts/AudioRoom.cs @@ -11,11 +11,11 @@ namespace Fungus.Example void OnEnter() { - if (HasValue("music")) + if (Variables.GetBoolean("music")) { AddOption("Stop the music", StopGameMusic); - if (HasValue("quiet") == false) + if (Variables.GetBoolean("quiet") == false) { AddOption("Shhh! Make it quieter", MakeQuiet); } @@ -34,15 +34,15 @@ namespace Fungus.Example { PlayMusic(musicClip); SetMusicVolume(1f); - SetValue("music"); + SetBoolean("music", true); Call(OnEnter); } void StopGameMusic() { StopMusic(); - ClearValue("music"); - ClearValue("quiet"); + SetBoolean("music", false); + SetBoolean("quiet", false); Call(OnEnter); } @@ -54,7 +54,7 @@ namespace Fungus.Example void MakeQuiet() { - SetValue("quiet"); + SetBoolean("quiet", true); SetMusicVolume(0.25f, 1f); Call(OnEnter); } diff --git a/Assets/FungusExample/Scripts/ButtonRoom.cs b/Assets/FungusExample/Scripts/ButtonRoom.cs index 1666881c..1d390784 100644 --- a/Assets/FungusExample/Scripts/ButtonRoom.cs +++ b/Assets/FungusExample/Scripts/ButtonRoom.cs @@ -51,7 +51,7 @@ namespace Fungus.Example PlaySound(effectClip); // The music button has been configured to automatically hide when this value is set - SetValue("PlayedSound"); + SetBoolean("PlayedSound", true); } void OnQuestionClicked() diff --git a/Assets/FungusExample/Scripts/DialogRoom.cs b/Assets/FungusExample/Scripts/DialogRoom.cs index 9f16a002..b2a3f9da 100644 --- a/Assets/FungusExample/Scripts/DialogRoom.cs +++ b/Assets/FungusExample/Scripts/DialogRoom.cs @@ -38,7 +38,7 @@ namespace Fungus.Example void GoToSleep() { // Check to see if a game value has been set - if (HasValue("spawned")) + if (Variables.GetBoolean("spawned")) { Say("I am feeling rather sleepy after all that spawning!"); Say("Yawn! Good night world!"); @@ -62,7 +62,7 @@ namespace Fungus.Example Say("Wow - look at all these spores! COOL!"); // Sets a global value flag which we check above in GoToSleep - SetValue("spawned"); + SetBoolean("spawned", true); AddOption("So tired. I sleep now.", GoToSleep); AddOption("No way! More spores!", ProduceSpores);