//------------------------------------------------------------------------------------------/ // Methods: Serialization //------------------------------------------------------------------------------------------/ /// <summary> /// Saves the state of the specified story /// </summary> /// <param name="story"></param> private void SaveState(StratusStory story) { if (!stories.ContainsKey(story.name)) { stories.Add(story.name, story); } story.timesRead++; stories[story.name].savedState = story.runtime.state.ToJson(); //Trace.Script($"Saving {story.name}"); }
private void LoadState(StratusStory story) { // If we are constructing the runtime if (!story.runtime) { story.runtime = new Ink.Runtime.Story(story.file.text); OnBindExternalFunctions(story); } story.runtime.state.LoadJson(story.savedState); }
/// <summary> /// Constructs the ink story runtime object from a given text asset file /// </summary> StratusStory ConstructStory(TextAsset storyFile) { StratusStory newStory = new StratusStory(); newStory.file = storyFile; newStory.runtime = new Ink.Runtime.Story(storyFile.text); if (!newStory.runtime) { this.LogError("Failed to load the story"); } // Bind external functions to it OnBindExternalFunctions(newStory); return(newStory); }
void OnRetrieveVariableValueEvent(StratusStory.RetrieveVariableValueEvent e) { switch (e.variable.type) { case StratusStory.Types.Integer: e.variable.intValue = StratusStory.GetVariableValue <int>(story.runtime, e.variable.name); break; case StratusStory.Types.Boolean: e.variable.boolValue = StratusStory.GetVariableValue <bool>(story.runtime, e.variable.name); break; case StratusStory.Types.Float: e.variable.floatValue = StratusStory.GetVariableValue <float>(story.runtime, e.variable.name); break; case StratusStory.Types.String: e.variable.stringValue = StratusStory.GetVariableValue <string>(story.runtime, e.variable.name); break; } }
void OnSetVariableValueEvent(StratusStory.SetVariableValueEvent e) { switch (e.variable.type) { case StratusStory.Types.Integer: StratusStory.SetVariableValue <int>(story.runtime, e.variable.name, e.variable.intValue); break; case StratusStory.Types.Boolean: StratusStory.SetVariableValue <bool>(story.runtime, e.variable.name, e.variable.boolValue); break; case StratusStory.Types.String: StratusStory.SetVariableValue <string>(story.runtime, e.variable.name, e.variable.stringValue); //StratusDebug.Log($"Setting variable {e.variable.name} to {e.variable.stringValue}"); break; case StratusStory.Types.Float: StratusStory.SetVariableValue <float>(story.runtime, e.variable.name, e.variable.floatValue); break; } }
/// <summary> /// Loads a story from file /// </summary> /// <param name="storyFile"></param> private void LoadStory(TextAsset storyFile, bool restart = false, string knot = null) { StratusStory newStory = null; // If this story has already been loaded, use the previous state bool previouslyLoaded = stories.ContainsKey(storyFile.name); if (previouslyLoaded) { if (debug) { StratusDebug.Log($"{storyFile.name} has already been loaded! Using the previous state."); } newStory = stories[storyFile.name]; LoadState(newStory); } // If the story hasn't been loaded yet else { if (debug) { StratusDebug.Log($"{storyFile.name} has not been loaded yet. Constructing a new state."); } newStory = ConstructStory(storyFile); } // Assign the story story = newStory; // If a knot was provided if (knot != null && knot.Length > 0) { if (!story.runtime.canContinue) { if (automaticRestart) { Restart(clearStateOnRestart); } else { StratusDebug.LogError($"The story {story.name} has already been ended, thus we can't jump to the knot!", this); } } JumpToKnot(knot); } else if (restart || automaticRestart) { Restart(clearStateOnRestart); } // Announce that we are loding the story var loadedEvent = new StratusStory.LoadedEvent() { reader = this, story = this.story }; this.gameObject.Dispatch <StratusStory.LoadedEvent>(loadedEvent); StratusScene.Dispatch <StratusStory.LoadedEvent>(loadedEvent); // Invoke any subclass callbacks OnStoryLoaded(story); // Now start the story // If the story was previously loaded, we need not start from a new line this.StartStory(previouslyLoaded && story.started); }
protected abstract void OnBindExternalFunctions(StratusStory story);
protected virtual void OnStoryLoaded(StratusStory story) { }