y-lohse / inkjs

A javascript port of inkle's ink scripting language.
http://www.inklestudios.com/ink/
MIT License
506 stars 103 forks source link

Missing API: cannot bind callback to story.onError #1033

Closed hsandt closed 1 year ago

hsandt commented 1 year ago

Describe the bug

This is a repost of https://github.com/inkle/ink/issues/873 for the inkjs repo. It's not as much a bug as a missing API.

The doc says how to bind onError in C#:

https://github.com/inkle/ink/blob/master/Documentation/RunningYourInk.md

_inkStory = new Story(inkAsset.text);

_inkStory.onError += (msg, type) => {
    if( type == Ink.ErrorType.Warning )
        Debug.LogWarning(msg);
    else
        Debug.LogError(msg);
};

but not in Javascript. I tried to modify main.js to display the error directly on the page:

    // Create ink story from the content using inkjs
    var story = new inkjs.Story(storyContent);

    story.onError += (msg, type) => {
        if( type == Ink.ErrorType.Warning ) {
            var paragraphElement = document.createElement('p');
            paragraphElement.innerHTML = msg;
            storyContainer.appendChild(paragraphElement);
        } else {
            var paragraphElement = document.createElement('p');
            paragraphElement.innerHTML = msg;
            storyContainer.appendChild(paragraphElement);
        }
    };

but I got:

Uncaught TypeError: this.onError is not a function at s.value (ink.js:1:118736) at s.value (ink.js:1:115775) at s.value (ink.js:1:115489) at continueStory (main.js:74:39) at main.js:58:5 at main.js:394:3

Maybe there is no binding in JS, only C#? But then why the console log error message in browser?

To Reproduce

Try to insert story.onError += (msg, type) => {} after story definition.

Expected behavior

I expect an equivalent to onError to exist in JS bindings.

Ink files

This happens in JS not the ink script itself, so copy the JS suggested above into main.js instead.

Environment

smwhr commented 1 year ago

You're trying to use a += operator on a function. It effectively transforms that function into a string. Then when onError is called, it errors (a string can't be called)

Try

story.onError = (msg, type) => {

instead of +=