Browse Source

Replaced Values system with new Variables class

- New commands SetInteger(), SetFloat(), SetBoolean().
- Simplified save game system.
master
chrisgregan 11 years ago
parent
commit
c8bee1eaf1
  1. 2
      Assets/Fungus/Legacy/PageCommands.cs
  2. BIN
      Assets/Fungus/Prefabs/ParallaxSprite.prefab
  3. 10
      Assets/Fungus/Scripts/Button.cs
  4. 4
      Assets/Fungus/Scripts/Commands/DialogCommands.cs
  5. 94
      Assets/Fungus/Scripts/Commands/GameCommands.cs
  6. 95
      Assets/Fungus/Scripts/Game.cs
  7. 126
      Assets/Fungus/Scripts/GameController.cs
  8. 2
      Assets/Fungus/Scripts/SceneLoader.cs
  9. 87
      Assets/Fungus/Scripts/StringTable.cs
  10. 2
      Assets/Fungus/Scripts/StringsParser.cs
  11. 175
      Assets/Fungus/Scripts/Variables.cs
  12. 2
      Assets/Fungus/Scripts/Variables.cs.meta
  13. BIN
      Assets/FungusExample/Scenes/Example.unity
  14. 12
      Assets/FungusExample/Scripts/AudioRoom.cs
  15. 2
      Assets/FungusExample/Scripts/ButtonRoom.cs
  16. 4
      Assets/FungusExample/Scripts/DialogRoom.cs

2
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;

BIN
Assets/Fungus/Prefabs/ParallaxSprite.prefab

Binary file not shown.

10
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;
}

4
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)

94
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();
}
}
}
}

95
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<string, int> values = new Dictionary<string, int>();
[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<string, int>)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, string>;
}
}
string sceneName = GetString("Fungus.Scene");
Variables.SetSaveName(saveName);
string sceneName = Variables.GetString("_scene");
if (sceneName.Length > 0)
{
LoadScene(sceneName, true);
MoveToScene(sceneName);
}
}
}

126
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;

2
Assets/Fungus/Scripts/SceneLoader.cs

@ -82,7 +82,7 @@ namespace Fungus
Game game = Game.GetInstance();
if (game != null)
{
game.SaveGame("Fungus.Save");
Variables.Save();
}
}

87
Assets/Fungus/Scripts/StringTable.cs

@ -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<string, string> stringDict = new Dictionary<string, string>();
/**
* 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;
}
}
}

2
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

175
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;
}
}
}

2
Assets/Fungus/Scripts/StringTable.cs.meta → Assets/Fungus/Scripts/Variables.cs.meta

@ -1,5 +1,5 @@
fileFormatVersion: 2
guid: 43c44334e66c44af1a071056e4fde1f9
guid: 6552eb7d3f623479097c813cc27dcf92
MonoImporter:
serializedVersion: 2
defaultReferences: []

BIN
Assets/FungusExample/Scenes/Example.unity

Binary file not shown.

12
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);
}

2
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()

4
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);

Loading…
Cancel
Save