From 6770c742c7a1354517c691d0761766e94e17cbbb Mon Sep 17 00:00:00 2001 From: Joseph Humfrey Date: Thu, 9 Mar 2017 16:56:09 +0000 Subject: [PATCH] Ability to pass arguments to knots (or functions) from the game. --- ink-engine-runtime/Story.cs | 11 +++++++---- ink-engine-runtime/StoryState.cs | 12 +++++++----- 2 files changed, 14 insertions(+), 9 deletions(-) diff --git a/ink-engine-runtime/Story.cs b/ink-engine-runtime/Story.cs index 7a02187c..a5a96ce5 100644 --- a/ink-engine-runtime/Story.cs +++ b/ink-engine-runtime/Story.cs @@ -1099,14 +1099,17 @@ bool PerformLogicAndFlowControl(Runtime.Object contentObj) /// /// /// A dot-separted path string, as specified above. - public void ChoosePathString(string path) + /// Optional set of arguments to pass, if path is to a knot that takes them. + public void ChoosePathString (string path, params object [] arguments) { - ChoosePath (new Path(path)); + state.PassArgumentsToEvaluationStack (arguments); + ChoosePath (new Path (path)); } + - internal void ChoosePath(Path path) + internal void ChoosePath(Path p) { - state.SetChosenPath (path); + state.SetChosenPath (p); // Take a note of newly visited containers for read counts etc VisitChangedContainersDueToDivert (); diff --git a/ink-engine-runtime/StoryState.cs b/ink-engine-runtime/StoryState.cs index 640a9bae..4639a494 100755 --- a/ink-engine-runtime/StoryState.cs +++ b/ink-engine-runtime/StoryState.cs @@ -810,20 +810,22 @@ internal void StartExternalFunctionEvaluation (Container funcContainer, params o callStack = new CallStack (funcContainer); callStack.currentElement.type = PushPopType.Function; - // Change the callstack the variableState is looking at to be - // this temporary function evaluation one. We'll restore it afterwards - variablesState.callStack = callStack; - // By setting ourselves in external function evaluation mode, // we're saying it's okay to end the flow without a Done or End, // but with a ~ return instead. _isExternalFunctionEvaluation = true; + PassArgumentsToEvaluationStack (arguments); + } + + internal void PassArgumentsToEvaluationStack (params object [] arguments) + { + // Pass arguments onto the evaluation stack if (arguments != null) { for (int i = 0; i < arguments.Length; i++) { if (!(arguments [i] is int || arguments [i] is float || arguments [i] is string)) { - throw new System.ArgumentException ("ink arguments when calling EvaluateFunction must be int, float or string"); + throw new System.ArgumentException ("ink arguments when calling EvaluateFunction / ChoosePathStringWithParameters must be int, float or string"); } PushEvaluationStack (Runtime.Value.Create (arguments [i]));