inkle / ink

inkle's open source scripting language for writing interactive narrative.
http://www.inklestudios.com/ink
MIT License
3.97k stars 482 forks source link

How to bind story.onError in js? #873

Closed hsandt closed 7 months ago

hsandt commented 9 months ago

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?

Selsynn commented 9 months ago

I do not think it's possible to bind to error in js, but I never tried it. You should definitively post the question on the inkjs repo: https://github.com/y-lohse/inkjs

floriancargoet commented 7 months ago

Your were almost there. inkjs expects story.onError to be a function be it doesn't pre-exist. You just have to define it. Replace your

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

with

 story.onError = (msg, type) => {
floriancargoet commented 7 months ago

@hsandt Right after posting this, I noticed you already got an answer on the inkjs issue, sorry. Maybe you could close this one?