public DialogueVariables(string globalFilePath) { string inkFileContents = File.ReadAllText(globalFilePath); Ink.Compiler compiler = new Ink.Compiler(inkFileContents); Story globalVariblesStory = compiler.Compile(); variables = new Dictionary <string, Ink.Runtime.Object>(); foreach (string name in globalVariblesStory.variablesState) { Ink.Runtime.Object value = globalVariblesStory.variablesState.GetVariableWithName(name); variables.Add(name, value); Debug.Log("initialize global dialogue variable: " + name + "=" + value); } }
public async void StoryTrainingIntro() { if (running) { DialogueOverlay.TerminateCurrentStory = true; } var compiler = new Ink.Compiler(File.ReadAllText("Assets/Resources/Stories/Training.ink")); var story = compiler.Compile(); story.variablesState["IsFirstMet"] = isFirstMet; var shouldIntroduceMechanisms = Context.Player.ShouldOneShot("Training Mode: Should Introduce Mechanisms"); story.variablesState["ShouldIntroduceMechanisms"] = shouldIntroduceMechanisms; story.variablesState["SignedIn"] = signedIn; story.variablesState["Rating"] = rating; running = true; await DialogueOverlay.Show(story); running = false; if (shouldIntroduceMechanisms && (int)story.variablesState["IntroducedMechanisms"] == 0) { Context.Player.ClearOneShot("Training Mode: Should Introduce Mechanisms"); } }
CommandLineTool(string[] args) { // Set console's output encoding to UTF-8 Console.OutputEncoding = System.Text.Encoding.UTF8; if (ProcessArguments(args) == false) { ExitWithUsageInstructions(); } if (opts.inputFile == null) { ExitWithUsageInstructions(); } string inputString = null; string workingDirectory = Directory.GetCurrentDirectory(); if (opts.outputFile == null) { opts.outputFile = Path.ChangeExtension(opts.inputFile, ".ink.json"); } if (!Path.IsPathRooted(opts.outputFile)) { opts.outputFile = Path.Combine(workingDirectory, opts.outputFile); } try { string fullFilename = opts.inputFile; if (!Path.IsPathRooted(fullFilename)) { fullFilename = Path.Combine(workingDirectory, fullFilename); } // Make the working directory the directory for the root ink file, // so that relative paths for INCLUDE files are correct. workingDirectory = Path.GetDirectoryName(fullFilename); Directory.SetCurrentDirectory(workingDirectory); // Now make the input file relative to the working directory, // but just getting the file's actual name. opts.inputFile = Path.GetFileName(fullFilename); inputString = File.ReadAllText(opts.inputFile); } catch { Console.WriteLine("Could not open file '" + opts.inputFile + "'"); Environment.Exit(ExitCodeError); } var inputIsJson = opts.inputFile.EndsWith(".json", StringComparison.InvariantCultureIgnoreCase); if (inputIsJson && opts.stats) { Console.WriteLine("Cannot show stats for .json, only for .ink"); Environment.Exit(ExitCodeError); } Parsed.Story parsedStory = null; Runtime.Story story = null; Compiler compiler = null; // Loading a normal ink file (as opposed to an already compiled json file) if (!inputIsJson) { compiler = new Compiler(inputString, new Compiler.Options { sourceFilename = opts.inputFile, pluginNames = pluginNames, countAllVisits = opts.countAllVisits, errorHandler = OnError, fileHandler = this }); // Only want stats, don't need to code-gen if (opts.stats) { parsedStory = compiler.Parse(); // Print any errors PrintAllMessages(); // Generate stats, then print as JSON var stats = Ink.Stats.Generate(compiler.parsedStory); var writer = new Runtime.SimpleJson.Writer(); writer.WriteObjectStart(); writer.WriteProperty("words", stats.words); writer.WriteProperty("knots", stats.knots); writer.WriteProperty("stitches", stats.stitches); writer.WriteProperty("functions", stats.functions); writer.WriteProperty("choices", stats.choices); writer.WriteProperty("gathers", stats.gathers); writer.WriteProperty("diverts", stats.diverts); writer.WriteObjectEnd(); Console.WriteLine(writer.ToString()); return; } // Full compile else { story = compiler.Compile(); } } // Opening up a compiled json file for playing else { story = new Runtime.Story(inputString); // No purpose for loading an already compiled file other than to play it opts.playMode = true; } PrintAllMessages(); if (story == null || _errors.Count > 0) { Environment.Exit(ExitCodeError); } // Play mode if (opts.playMode) { _playing = true; // Always allow ink external fallbacks story.allowExternalFunctionFallbacks = true; var player = new CommandLinePlayer(story, false, compiler, opts.keepOpenAfterStoryFinish); //Capture a CTRL+C key combo so we can restore the console's foreground color back to normal when exiting Console.CancelKeyPress += OnExit; try { player.Begin(); } catch (Runtime.StoryException e) { if (e.Message.Contains("Missing function binding")) { OnError(e.Message, ErrorType.Error); PrintAllMessages(); } else { throw e; } } catch (System.Exception e) { string storyPath = "<END>"; var path = story.state.currentPathString; if (path != null) { storyPath = path.ToString(); } throw new System.Exception(e.Message + " (Internal story path: " + storyPath + ")", e); } } // Compile mode else { var jsonStr = story.ToJson(); try { File.WriteAllText(opts.outputFile, jsonStr, System.Text.Encoding.UTF8); } catch { Console.WriteLine("Could not write to output file '" + opts.outputFile + "'"); Environment.Exit(ExitCodeError); } } }