inkle / inkle/ink

I can't save the story corretly

Open
#579 1 comment 0 reactions 0 assignees View on GitHub
Dominant language
C#
Stars
4.9k
Forks
540
PR merge metrics
No merged PRs in 30d

Description

Hi all,
I can't the story correctly.
When I load the story that I saved before, I always go to the end of the story.
I have updated the version of ink.
Someone can help me?

using UnityEngine;
using System;
using System.Collections;
using System.Collections.Generic;
using Ink.Runtime;
using UnityEngine.UI;
using DG.Tweening;

using Debug = UnityEngine.Debug;

public class GameState : MainState {

// The json compiled ink story
public TextAsset storyJSON;
// The ink runtime story object
private Story story;

public ContentManager contentManager;

public ContentView contentViewPrefab;
public ChoiceGroupView choiceGroupViewPrefab;
public ChevronButtonView chevronViewPrefab;
public EmptyView emptyViewPrefab;

public Transform contentParent;
public ChoiceGroupContainerView choiceContainerView;

public ScrollToBottomButtonView scrollToBottomButton;

public bool hasMadeAChoice = false;

public SettingsView settingsView;
public SettingsButton settingsButton;
public bool deleteStorySaved = false;

private void Awake () {
contentManager.enabled = false;
settingsView.Hide();
settingsButton.Hide();
}

public override void Enter () {
base.Enter ();
contentManager.enabled = true;
settingsButton.FadeIn();

if(storyJSON == null) {
Debug.LogWarning("Drag a valid story JSON file into the StoryReader component.");
enabled = false;
}

//delete saved story
if (deleteStorySaved)
{

PlayerPrefs.DeleteKey("inkSaveState");

}

//load saved story
if (PlayerPrefs.HasKey("inkSaveState") == true)
{
string storyJonsSaved = PlayerPrefs.GetString("inkSaveState");
//Debug.Log("--"+storyJons+"---");
story.state.LoadJson(storyJonsSaved);
}else
{
story = new Story(storyJSON.text);

}

//story = new Story(storyJons);
StartCoroutine(OnAdvanceStory());
//PlayerPrefs.SetString("ciao", "prova");
}

public void Clear () {
StopAllCoroutines();
ClearContent();
choiceContainerView.Clear();
contentManager.enabled = false;
settingsButton.Hide();
settingsView.Hide();
}

private void ClearContent () {
for (int i = contentParent.childCount-1; i >= 0; i--) {
Destroy(contentParent.GetChild(i).gameObject);
}
}

IEnumerator OnAdvanceStory () {

if (story.canContinue) {
ChoiceGroupView choiceView = null;
ChevronButtonView chevronView = null;
int cont = 0;
while (story.canContinue) {

cont++;
string content = story.Continue().Trim();
if (content == "")
{
content = "\n";
}

ContentView contentView = CreateContentView(content);
Debug.Log("conta " + cont);

if (cont >= 2) //save strory
{
string savedState = story.state.ToJson();
PlayerPrefs.SetString("inkSaveState", savedState);
changeAudio(0);
}

if (!story.canContinue) {
if(story.currentChoices.Count > 0) {
choiceView = CreateChoiceGroupView(story.currentChoices);
} else {
chevronView = CreateChevronView();
}
}
while(contentView.textTyper.typing)
yield return null;

if(story.canContinue)
yield return new WaitForSeconds(Mathf.Min(1.0f, contentView.textTyper.targetText.Length * 0.01f));
}
if(story.currentChoices.Count > 0) {
yield return new WaitForSeconds(1f);
choiceView.RenderChoices();
yield return new WaitForSeconds(0.5f);
} else {
chevronView.Render();
yield return new WaitForSeconds(2);
}
} else {
yield return new WaitForSeconds(2);
CreateChevronView();
}
}

public void ChooseChoiceIndex (int choiceIndex) {

DestroyEmpties();
story.ChooseChoiceIndex(choiceIndex);
hasMadeAChoice = true;
StartCoroutine(OnAdvanceStory());
}

private void DestroyEmpties () {
EmptyView[] emptyViews = contentParent.GetComponentsInChildren();
for (int i = emptyViews.Length-1; i >= 0; i--) {
Destroy(emptyViews[i].gameObject);
}
}

public void ClickChevronButton () {
Complete();
}

ContentView CreateContentView (string content) {
ContentView contentView = Instantiate(contentViewPrefab);
contentView.transform.SetParent(contentParent, false);
contentView.LayoutText(content);
return contentView;
}

// Creates a textbox showing the the line of text
/*
void CreateContentView2(string text)
{
Text storyText = Instantiate(textPrefab) as Text;
storyText.text = text;
storyText.transform.SetParent(canvas.transform, false);
}
*/

ChoiceGroupView CreateChoiceGroupView (IList choices) {
ChoiceGroupView choiceGroupView = Instantiate(choiceGroupViewPrefab);
choiceGroupView.transform.SetParent(choiceContainerView.transform, false);
choiceGroupView.LayoutChoices(choices);
CreateEmptyView(choiceGroupView.rectTransform.sizeDelta.y);
return choiceGroupView;
}

ChevronButtonView CreateChevronView () {
ChevronButtonView chevronView = Instantiate(chevronViewPrefab);
chevronView.transform.SetParent(choiceContainerView.transform, false);
CreateEmptyView(chevronView.rectTransform.sizeDelta.y);
return chevronView;
}

EmptyView CreateEmptyView (float height) {
EmptyView emptyView = Instantiate(emptyViewPrefab);
emptyView.transform.SetParent(contentParent, false);
emptyView.layoutElement.preferredHeight = height;
return emptyView;
}
/*
void setAudio()
{
backGroundAudio.audioSource.clip = backGroundAudio.audioClips[0];
backGroundAudio.NormalMode();
//subroutine
}
*/

void changeBackground()
{
Debug.Log("sono backgrodu");
Image images = contentManager.GetComponentInChildren();
images.sprite = Sprite.Create(contentManager.imagesBackGround[0], new Rect(0.0f, 0.0f, contentManager.imagesBackGround[0].width, contentManager.imagesBackGround[0].height), new Vector2(0.5f, 0.5f), 100.0f);


//get background

/*
Image[] images = PlayerBarExample.GetComponentsInChildren();
Image face = images[0];
foreach (Image image in images)
{
if (image.gameObject.CompareTag("face"))
face = image;
}
}
*/
}

void changeAudio(int audioClip)
{
Main.Instance.backgroundAmbienceController.QuietMode();
Main.Instance.backgroundAmbienceController.changeAudioClip(audioClip);
Main.Instance.backgroundAmbienceController.HightMode();
/* vecchio modo
Debug.Log("sto cambiando il sound");
Main.Instance.backgroundAmbienceController.audioSource.volume = 0;
IEnumerator fadeSound1 = Main.Instance.backgroundAmbienceController.fadeOut(Main.Instance.backgroundAmbienceController.audioSource, 0.3f);
StartCoroutine(fadeSound1);
Main.Instance.backgroundAmbienceController.changeAudioClip();
IEnumerator fadeSound2 = Main.Instance.backgroundAmbienceController.fadeIn(Main.Instance.backgroundAmbienceController.audioSource, 2.5f);
StartCoroutine(fadeSound2);
*/

}

public string getStoryLoaded()
{
string storySaved = "";
if (PlayerPrefs.HasKey("inkSaveState") == true)
{
storySaved = PlayerPrefs.GetString("inkSaveState");
//inkStory.state.LoadJson(savedState);
}
Debug.Log("sono la storia: " + storySaved);
return storySaved;
}

public void saveStory()
{
Debug.Log("save story game state");
string savedState = story.state.ToJson();

PlayerPrefs.SetString("inkSaveState", savedState);
}

/*
* PlayerPrefs.DeleteKey("inkSaveState");
_inkStory.ResetState()C
*/

/*
private saveStory()
{
var savedState = _inkStory.state.ToJson();
PlayerPrefs.SetString("inkSaveState", savedState);
}
*/

}

Contributor guide

No contributing guide indexed for this repository

Research direction

Start in the GameState.Enter method and trace the inkSaveState load path alongside saveStory and OnAdvanceStory. Reproduce loading a saved story, then verify that the Ink story state is initialized and restored correctly before the story advances; done means reopening a saved story resumes at its saved position.

Written by the indexing model from the issue text.

Assessment

Tech stack
csharp, unity
Domain
game-dev
Issue type
Bug
Difficulty
2/5
Estimated time
1-3 hours
Activity status
Stale
Clarity
Mostly clear
Newbie friendliness
35/100

Get new issues in your inbox

A short digest of beginner-friendly GitHub issues.