From 7ddc29bc6dc96701c0feb44bbdef52dbf8ffb817 Mon Sep 17 00:00:00 2001 From: Jonathon Manning Date: Sat, 20 Feb 2016 09:29:34 +1100 Subject: [PATCH 1/3] Allow resetting the dialogue system [#14] --- .../Yarn Spinner/Code/DialogueRunner.cs | 12 ++++++- YarnSpinner/Dialogue.cs | 31 +++++++++++++++++-- 2 files changed, 40 insertions(+), 3 deletions(-) diff --git a/Unity/Assets/Yarn Spinner/Code/DialogueRunner.cs b/Unity/Assets/Yarn Spinner/Code/DialogueRunner.cs index 4426bf9c3..ec4af7c13 100644 --- a/Unity/Assets/Yarn Spinner/Code/DialogueRunner.cs +++ b/Unity/Assets/Yarn Spinner/Code/DialogueRunner.cs @@ -125,7 +125,7 @@ public void StartDialogue (string startNode) StartCoroutine (RunDialogue (startNode)); } - IEnumerator RunDialogue (string startNode) + IEnumerator RunDialogue (string startNode = "Start") { // Mark that we're in conversation. isDialogueRunning = true; @@ -169,6 +169,16 @@ IEnumerator RunDialogue (string startNode) // out of a conversation (ie letterboxing going away, etc) isDialogueRunning = false; } + + public void Clear() { + + if (isDialogueRunning) { + throw new System.InvalidOperationException("You cannot clear the dialogue system while a dialogue is running."); + } + + dialogue.UnloadAll(); + } + } // Scripts that can act as the UI for the conversation should subclass this diff --git a/YarnSpinner/Dialogue.cs b/YarnSpinner/Dialogue.cs index 8e2cac73b..0c9a9d3a3 100644 --- a/YarnSpinner/Dialogue.cs +++ b/YarnSpinner/Dialogue.cs @@ -186,7 +186,7 @@ public int LoadString(string text, bool showTokens=false, bool showParseTree=fal throw new InvalidOperationException ("Attempted to load a Yarn program, but no nodes were loaded"); } - program = loader.Compile (); + return nodesLoaded; } @@ -195,7 +195,6 @@ public int LoadString(string text, bool showTokens=false, bool showParseTree=fal // you'll get a line, command, or set of options. public IEnumerable Run(string startNode = DEFAULT_START) { - stopExecuting = false; if (LogDebugMessage == null) { throw new YarnException ("LogDebugMessage must be set before running"); @@ -205,6 +204,18 @@ public int LoadString(string text, bool showTokens=false, bool showParseTree=fal throw new YarnException ("LogErrorMessage must be set before running"); } + if (program == null && loader.nodes.Count > 0) { + program = loader.Compile(); + } + + if (program == null) { + LogErrorMessage ("Dialogue.Run was called, but no program was loaded. Stopping."); + yield break; + } + + stopExecuting = false; + + var vm = new VirtualMachine (this, program); vm.SetNode (startNode); @@ -245,6 +256,22 @@ public int LoadString(string text, bool showTokens=false, bool showParseTree=fal + } + + public IEnumerable visitedNodes { + get { + return visitedNodeNames; + } + } + + // Unloads ALL nodes. + public void UnloadAll(bool clearVisitedNodes = true) { + if (clearVisitedNodes) + visitedNodeNames.Clear(); + + program = null; + loader.Clear (); + } public String Compile() { From 267c38d04b8b4a3a860e4d93ce3608e8a2818a58 Mon Sep 17 00:00:00 2001 From: Jonathon Manning Date: Sat, 20 Feb 2016 09:58:09 +1100 Subject: [PATCH 2/3] Add a way to check if a node exists or not. [closes #15] --- .../Yarn Spinner/Code/DialogueRunner.cs | 4 ++ YarnSpinner/Dialogue.cs | 6 ++- YarnSpinnerTests/Test.cs | 37 ++++++++++++++++++- 3 files changed, 44 insertions(+), 3 deletions(-) diff --git a/Unity/Assets/Yarn Spinner/Code/DialogueRunner.cs b/Unity/Assets/Yarn Spinner/Code/DialogueRunner.cs index ec4af7c13..b53bfb183 100644 --- a/Unity/Assets/Yarn Spinner/Code/DialogueRunner.cs +++ b/Unity/Assets/Yarn Spinner/Code/DialogueRunner.cs @@ -179,6 +179,10 @@ public void Clear() { dialogue.UnloadAll(); } + public bool NodeExists(string nodeName) { + return dialogue.NodeExists(nodeName); + } + } // Scripts that can act as the UI for the conversation should subclass this diff --git a/YarnSpinner/Dialogue.cs b/YarnSpinner/Dialogue.cs index 0c9a9d3a3..9a5f5ef33 100644 --- a/YarnSpinner/Dialogue.cs +++ b/YarnSpinner/Dialogue.cs @@ -275,9 +275,13 @@ public void UnloadAll(bool clearVisitedNodes = true) { } public String Compile() { - var program = loader.Compile(); + program = loader.Compile(); return program.DumpCode (library); } + + public bool NodeExists(string nodeName) { + return program.nodes.ContainsKey(nodeName); + } // The standard, built-in library of functions and operators. diff --git a/YarnSpinnerTests/Test.cs b/YarnSpinnerTests/Test.cs index 10c70bbe0..6cfca431d 100644 --- a/YarnSpinnerTests/Test.cs +++ b/YarnSpinnerTests/Test.cs @@ -8,6 +8,7 @@ public class Test { Yarn.MemoryVariableStore storage = new Yarn.MemoryVariableStore(); + Yarn.Dialogue dialogue; [SetUp()] public void Init() @@ -15,13 +16,45 @@ public void Init() var newWorkingDir = System.IO.Path.Combine (Environment.CurrentDirectory, "Tests"); Environment.CurrentDirectory = newWorkingDir; + + dialogue = new Yarn.Dialogue (storage); + + dialogue.LogDebugMessage = delegate(string message) { + + Console.WriteLine (message); + + }; + + dialogue.LogErrorMessage = delegate(string message) { + Console.ForegroundColor = ConsoleColor.Red; + Console.WriteLine (message); + Console.ResetColor (); + }; + + dialogue.library.RegisterFunction ("assert", 1, delegate(Yarn.Value[] parameters) { + if (parameters[0].AsBool == false) + Assert.Fail ("Assertion failed"); + }); } [Test ()] - public void TestCase () + public void TestNodeExists () { + + dialogue.LoadFile ("Ship.json"); + + dialogue.Compile (); + + Assert.True (dialogue.NodeExists ("Sally")); + + // Test clearing everything + dialogue.UnloadAll (); + + // Load an empty node + dialogue.LoadString("// Test, this is empty"); + dialogue.Compile (); - var d = new Yarn.Dialogue (storage); + Assert.False (dialogue.NodeExists ("Sally")); } From aaeb77a644151380dfaeb2c93ce30939db8a0cb5 Mon Sep 17 00:00:00 2001 From: Jonathon Manning Date: Sat, 20 Feb 2016 10:28:08 +1100 Subject: [PATCH 3/3] Add a way to stop dialogue from Unity. [closes #16] --- Unity/Assets/Yarn Spinner/Code/DialogueRunner.cs | 4 ++++ YarnSpinner/Dialogue.cs | 4 ++++ 2 files changed, 8 insertions(+) diff --git a/Unity/Assets/Yarn Spinner/Code/DialogueRunner.cs b/Unity/Assets/Yarn Spinner/Code/DialogueRunner.cs index b53bfb183..bbbb9b1d6 100644 --- a/Unity/Assets/Yarn Spinner/Code/DialogueRunner.cs +++ b/Unity/Assets/Yarn Spinner/Code/DialogueRunner.cs @@ -179,6 +179,10 @@ public void Clear() { dialogue.UnloadAll(); } + public void Stop() { + dialogue.Stop(); + } + public bool NodeExists(string nodeName) { return dialogue.NodeExists(nodeName); } diff --git a/YarnSpinner/Dialogue.cs b/YarnSpinner/Dialogue.cs index 9a5f5ef33..dfc2a1f09 100644 --- a/YarnSpinner/Dialogue.cs +++ b/YarnSpinner/Dialogue.cs @@ -258,6 +258,10 @@ public int LoadString(string text, bool showTokens=false, bool showParseTree=fal } + public void Stop() { + stopExecuting = true; + } + public IEnumerable visitedNodes { get { return visitedNodeNames;