Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Issue with serialization? #754

Closed
Alex-Sand-0 opened this issue Apr 27, 2022 · 1 comment
Closed

Issue with serialization? #754

Alex-Sand-0 opened this issue Apr 27, 2022 · 1 comment

Comments

@Alex-Sand-0
Copy link

Alex-Sand-0 commented Apr 27, 2022

Hi,
I am struggling trying to save and load my story, but the story I reload fails with some weird error.
More specifically, I do this:

        var story = Compile();
        var json = story.ToJson();  // serialize 
        var story2 = new Story(json); // deserialize
        var json2 = story2.ToJson(); // serialize again
        var story3 = new Story(json2); // deserialize again
  • "story" and "story2" can be run with no issue. Running "story3" produces an error.
  • I also observe that json and json2 are different

I included a minimal example below.
Any idea of what I am doing wrong? Thanks for your help :)

class Program
    {


        const string rawink = @"== mytestknot == 
    -> hello
    
    = hello 
        Hi, {hello>1:still} no issue there. 
            // not that the problem diseapear if I remove {hello>1:still}

        + bla
        -> DONE    

";

        static Story Compile()
        {
            var compiler = new Ink.Compiler(rawink);
            var story = compiler.Compile();
            return story;
        }

        static void Check(Story story)
        {
            story.ChoosePathString("mytestknot");
            try
            {
                var str1 = story.ContinueMaximally();
                Console.WriteLine(str1);
            }
            catch (Exception e)
            {
                Console.WriteLine("Failed");
                Console.WriteLine(e);
            }
        }

        static void Main(string[] args)
        {

            var story = Compile();
            var json = story.ToJson();  // serialize 
            var story2 = new Story(json); // deserialize
            var json2 = story2.ToJson(); // serialize again
            var story3 = new Story(json2); // deserialize again

            // the json do not match ?!
            Console.WriteLine($"json==json2  {json == json2}");


            // no more changes in the jsons
            var json3 = story3.ToJson();
            Console.WriteLine($"json2==json3  {json2 == json3}");

            // chack that no story has errors
            Console.WriteLine( $" any errors? { story.hasError} { story2.hasError} { story3.hasError}" );

            // initial story works
            Console.WriteLine("inital story");
            Check(story);
            Check(story);

            // story deserialized from first json works
            Console.WriteLine("story deserialized from story.ToJson");
            Check(story2);
            Check(story2);

            // story deserialized from json2 fails
            Console.WriteLine("story3");
            Check(story3); // This one throws an exception
            Check(story3);

            Console.WriteLine();
            Console.WriteLine(json);
            Console.WriteLine(json2);
            Console.WriteLine("DONE");

        }



    }

Here are the two jsons:
{"inkVersion":20,"root":[[["done",{"#n":"g-0"}],null],"done",{"mytestknot":[{"->":".^.hello"},{"hello":[["^Hi, ","ev",{"CNT?":".^.^"},1,">","/ev",[{"->":".^.b","c":true},{"b":["^still",{"->":".^.^.^.7"},null]}],"nop","^ no issue there.","\n",["ev",{"^->":"mytestknot.hello.0.10.$r1"},{"temp=":"$r"},"str",{"->":".^.s"},[{"#n":"$r1"}],"/str","/ev",{"*":".^.^.c-0","flg":2},{"s":["^bla",{"->":"$r","var":true},null]}],{"c-0":["ev",{"^->":"mytestknot.hello.0.c-0.$r2"},"/ev",{"temp=":"$r"},{"->":".^.^.10.s"},[{"#n":"$r2"}],"\n","done",null]}],{"#f":1}]}]}],"listDefs":{}}

{"inkVersion":20,"root":[[["done",{"#n":"g-0"}],null],"done",{"mytestknot":[{"->":".^.hello.0"},{"hello":[["^Hi, ","ev",{"CNT?":"^.^"},1,">","/ev",[{"->":".^.b.0","c":true},{"b":["^still",{"->":".^.^.^.7"},null]}],"nop","^ no issue there.","\n",["ev",{"^->":"mytestknot.hello.0.10.$r1"},{"temp=":"$r"},"str",{"->":".^.s.0"},[{"#n":"$r1"}],"/str","/ev",{"*":".^.^.c-0","flg":2},{"s":["^bla",{"->":"$r","var":true},null]}],{"c-0":["ev",{"^->":"mytestknot.hello.0.c-0.$r2"},"/ev",{"temp=":"$r"},{"->":".^.^.10.s.0"},[{"#n":"$r2"}],"\n","done",null]}],{"#f":1}]}]}],"listDefs":{}}

Edit:

  • I use Ink V1.0.0
  • I had forgotten to include the error message i get:

Ink.Runtime.StoryException: Ink had 1 error. It is strongly suggested that you assign an error handler to story.onError. The first issue was: RUNTIME ERROR: (mytestknot.hello.0.2): Read count for target ( - on ) unknown.
at Ink.Runtime.Story.ContinueInternal(Single millisecsLimitAsync)
at Ink.Runtime.Story.ContinueMaximally()
at inkbug.Program.Check(Story story) in C:\Users\33616\sources\inkbug\Program.cs:line 35

@Alex-Sand-0
Copy link
Author

Ok, so I figured out what I was doing wrong,
I should save only the state of the story, and not the story itself, as proposed in the doc: https://github.com/inkle/ink/blob/master/Documentation/RunningYourInk.md#saving-and-loading

However, the behavior I reported in the previous post still looks buggy. Is it really the intended behavior? Maybe it is worth documenting explicitly that it does not work?

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

No branches or pull requests

1 participant