From c8bee1eaf13d36d34993b0be743737d31b2b29d7 Mon Sep 17 00:00:00 2001 From: chrisgregan Date: Wed, 25 Jun 2014 16:20:25 +0100 Subject: [PATCH] Replaced Values system with new Variables class - New commands SetInteger(), SetFloat(), SetBoolean(). - Simplified save game system. --- Assets/Fungus/Legacy/PageCommands.cs | 2 +- Assets/Fungus/Prefabs/ParallaxSprite.prefab | Bin 7920 -> 8104 bytes Assets/Fungus/Scripts/Button.cs | 10 +- .../Fungus/Scripts/Commands/DialogCommands.cs | 4 +- .../Fungus/Scripts/Commands/GameCommands.cs | 94 ---------- Assets/Fungus/Scripts/Game.cs | 95 ++-------- Assets/Fungus/Scripts/GameController.cs | 126 +++++++++---- Assets/Fungus/Scripts/SceneLoader.cs | 2 +- Assets/Fungus/Scripts/StringTable.cs | 87 --------- Assets/Fungus/Scripts/StringsParser.cs | 2 +- Assets/Fungus/Scripts/Variables.cs | 175 ++++++++++++++++++ ...{StringTable.cs.meta => Variables.cs.meta} | 2 +- Assets/FungusExample/Scenes/Example.unity | Bin 95752 -> 96152 bytes Assets/FungusExample/Scripts/AudioRoom.cs | 12 +- Assets/FungusExample/Scripts/ButtonRoom.cs | 2 +- Assets/FungusExample/Scripts/DialogRoom.cs | 4 +- 16 files changed, 307 insertions(+), 310 deletions(-) delete mode 100644 Assets/Fungus/Scripts/StringTable.cs create mode 100644 Assets/Fungus/Scripts/Variables.cs rename Assets/Fungus/Scripts/{StringTable.cs.meta => Variables.cs.meta} (78%) 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 977746e4d89cab8bac0891f01ce79bced1242941..ffab647b92eb26b9453fce8a6a65939790053176 100644 GIT binary patch delta 440 zcmexhyTV?YfkE~x1B3hu1_lOBAo~N5W-!q+)iX#ln<#C<$g_DO-*Lvx;!JEzHespB zCHX~0X$%F4MTt2%i50=gi8-kZ96%isAZ{2xsY3) zQEKu;ZYlfx{M^(O|I!kOZjda{5nuq(4PircSAi9K<{{L|Y&K^xWM$-nxXXasgn6<( z9~*a2etwC6QA%nN1Iy%t(!!G$2(e5~;nQIh2C9=_o6IVzH2JWIHKW61Q&9y*jmiET zDvX+w9Ys_awI)v#5}v$SR7_DD*%b~7V0S}Y0W=gSr!)DLs633{X6S~$=78%CqIzm sm>es+d-7Z$eL$9DvVxqRP=g&%84xtsLofrwSoAnt=WNq2I+m^`E2AD|AC+gNH^3tGlER50J5(@@q+a4tQC`UWIHFx h?Us-{p163@{`r&t(k$!C#T9g0{{l~QrQ3i 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 caec8d76ade382d01c508d4ededc05ec9d4e60fb..fe13dda6c5f4dee22534d0e43bafa91ab249f69a 100644 GIT binary patch delta 3644 zcmY+{eN>ZG90%~mzEG4*OIaRNG=vFi%M_VxHa!z2K~74y%#6yH zogQoaf#>wz{-~T$jng@uL-ukwr&3fbGczmekZ(-OvKOlFvpo;q$8)Z1pXc7+@7~{i zp1ZB{lDcbHttFOxn%I^uB1*>V=ppSqL(lTgBZ-3kOSO=1>>j^jadVrQ98R~h&AHOv zy1~BA?R-dhqcmIiz?dT>nT6FkOrW{mYdKDC&c zVqP9i6|5y=V%JJy*VdF2vT3nv6UyRaX23Irvn3g9X3y0Uk1KV4ON-lSZ>In7FI)It zUD)&N{)ba~)&{V^FJEbywBFrfZ=;TumQ7AaZF^gNlilqk6^fA~q%XA8CS_yFMIa9YeGtJ~%d3 z2MfVg!{o_~Xg^BPXow7nM88O_y0|Q(bR5xEZtT)xUs*;NXa)W)C8WwpMB`CXZ@ym%dBVtQ8%}e z@K+7x0Rz$fTxAZyqA!?Fl-3cC_ex4v)w-ZWlL3= za`3~j5R4zZ1tu;7gGa|6{`<+wMi{?MAHn)y{Me6SS$FbdQU9k>dlq+=EmBquJ6tI9GfAE%|J$Cuog ztvtDEVBR@IHdNs^X-jHh127C9J-Dne&0Wd~E`#}D;{6eO{#AI?(UBy zKeaSEmngtJIyL4KOZ^t*WNKk_Pkb`JN|Uj|Y_K-O7UPR;8EgQ?P0&YmK+SyR4ELT~ zRp*Bpxo11QXzR+*0-|;V!W?5Q1EyaXPkc>EoC)*7hn??s0} zsdc;&xUXxGvi!SGpR{=wEANjzupw9_#>t;{dts$Zl=1e#LNI=d-?_e|f$o*!iO;{d znsw# z@}oG?djC2-vVM-4$){t}&Oe)?%ar@>fO&B0STOdgRDLHc4C9lmhiU4S<$n-nsmJrr zm;cD8XV>?lBmWtF6lO*=@$ivvqye!XjNi{=uqcd=cO2$gLFB^Ns5HUfR*$#_VDT>? zsJ~eL>%h{L%31#o>x4Dpx?Gt{@0&Lb!R#<`oY*eItoJDsUxD?*_$q8AokwrOdBctE z@Bza>JrLE6wMSqBl8LL1H=QRc*=m^BwxOREN1k|HC$;Uc0E|yO>!^A25R5ej0W?&e|-_^5WH(*{Q7?j$#bKl$Sc|aNPEbJVNkM|wSjG6NB&cXaJDV{GD^v4s! z?KglMkU%c$d6)~E#0UHd)(2}tzhP-VLuW2-F*gu>jDhmanq9lbEAHEV?uoeIZ0+b; zhqHpS#42BZN7SLb;Z#-q9Ugq&;uEW3{V*Jcv{q_KFwOeWmBbb@7UqZDfj;ubY+8+K zTQ3av-00%8Fztgx4tzfty{oU)|Gdo$9N=bH(44ZpYGdSuM0Q3U*^$h0RZ0E|B7a4s zJC*HV@|)Z=W=mk&-~j_OMh>L2g9$-P5z_=U`OFZp>X>dUek2j41z8R&jO?Dwx)Yu^ zF;(z|Jfa;Az@=QC>}(?*c$ ztD9ML!9El6BUMUdXED2!5p>UCmPlw0du?>a)fLP;Cg^rCL(r4Y)RD^zSx!PQyb<$I z#s=54`ybSx)HJfw+5Cf6|kXuj?`c33M{ OXmt};t}_lcE&YF2w`?N- delta 3238 zcmY+`drTBZ90%~E}ZsU{&`F!)7ozq%}cgF zJ`@wiVr;@zs}+4=m` z6=%Wu)JN=$ctV{m<=T1 z{G=+S##0_@py`-;0LGW#H<*4N(K;@rTd-Ea=Y=X|2v7JDY#wfW&FDo`$|Y6^>wy_z zj>&I_1@%@AD(d zkXFeS{1;7ZgeFkFde;W8M`&J0U~WbA(ZMk_8hO&Eb-NP{!igzW%0w8^MsD&QHw%*t z;quhKhgo1ov~!gC-$N@09TWp0d+PwHDd>4Wi;KLyh+luz_gP8d_EqQ&a-ux!D4aRq_hPergr&VTSg1q&Gsn627 zVSMY0FbyWi*YOgp24=(*9V>BQUgxR-82@S=gz4Xr_dNt_h2g9*&mLEYfrCJP2aYaS zzS@u|KWrYux?p_ZF_?dnJV^f|54{P-zag1XpQPs`^GAxAC=1vJG+-uArA5ewY2US< zDYHjP4$KJS6U>DTz-&80Azc-5-WkRvL=U)EQ=EGsQ=dXq&b zbtaj=rDSqojj&2CNG^<4$P>(i>0o?2xz{4u45Sz$W#a~!twZGBhE1J`}myrC7wm;dms8-@nGY=4-k31|iKAD$zy z`1j=l9)-2R_)Yl3v-Cnrmkmfi!b~u}-C0-<%=X!EoOmM4e+|~3FW2>LpKfVh1LM}o zdRe#CEla)$$6@6#KFAYTH_V72jyGbuV&hg#Hc>Ci>^P>2_e~aCkx4DuF~ql*zXWK? zB}&EpxK#d9m<7fMDTBG;JmSm$AuJWfPvpwacgwn9_8?M>t1#VKqK|R!*sSJ0TSFi} z;5C>P#t--_tQ?t?qq9{?@M*zxc^{1bTi_Wie!V>D=debY5q%w%89ArhF|;16J~*#E75RFTF3eKGMJ#~Y{@(rhe8B64WU0_`>9EP;TcK+i(as^HR z`I)%GOr`b@>+E;cJ4;dD2E$&Oot+X!Wkl#cyQ`n9eOO-(>*Ep(-RZekhq4%*|DW6n z5)&1y*QLee&Rj(&cNU}&7kaS@%4UTM@%44ATPZ#)v1PThj79tW2g#}&D*ylh 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);