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

Trying to load monogatari inside a react project - works on first load, fails on every next load #139

Open
blurymind opened this issue Apr 22, 2021 · 3 comments

Comments

@blurymind
Copy link

blurymind commented Apr 22, 2021

When I try to do it like this

  useEffect(() => {
    monogatari.settings(settings);
    monogatari.preferences(preferences);
    monogatari.storage(storage);
    //// Script
    setScript(monogatari);

    ///  doesnt make a differentce
    const { $_ready, $_ } = Monogatari;
    //// 1. Outside the $_ready function:
    // $_ready(() => {
    //   // 2. Inside the $_ready function:
    //   // monogatari.init("#monogatari").then(() => {
    //   //   console.log("INIT");
    //   //   // 3. Inside the init function:
    //   // });
    // });

    monogatari.init("#monogatari").then(() => {
      console.log("INIT");
      // 3. Inside the init function:
    });

    // unmount
    return () => {
     };
  }, []);

My game starts fine on the first refresh of the page. I am guessing its because its using some onload event internally to initiate things.

But when I use react-router-dom, navigate to a different route then back to the one where the game is, it fails to load and gives me errors

monogatari.js:1810 Uncaught (in promise) TypeError: Cannot set property 'innerHTML' of null
    at r.value (monogatari.js:1810)
    at monogatari.js:1713

or this

index.js:1 TypeError: Cannot read property 'props' of undefined
    at i.value (monogatari.js:1830)
    at monogatari.js:1713
console.<computed> @ index.js:1
(anonymous) @ monogatari.js:1713
Promise.catch (async)
(anonymous) @ monogatari.js:1713
Promise.then (async)
value @ monogatari.js:1713
(anonymous) @ monogatari.js:1713
Promise.then (async)
callback @ monogatari.js:1713
value @ monogatari.js:854
value @ monogatari.js:1713
(anonymous) @ monogatari.js:1713
(anonymous) @ monogatari.js:826
5monogatari.js:1830 Uncaught (in promise) TypeError: Cannot read property 'props' of undefined
    at i.value (monogatari.js:1830)
    at monogatari.js:1713

since the code is minified its kinda hard to tell

Another observation is that $_ready only runs on the first load, every other time its not called at all (navigating away from the page and back to it). I am assuming that it keeps running when I navigate away somehow

I am guessing that after the first load, before leaving the component (see //unmount), I am supposed to somehow reset the engine? I couldnt find any documentation on that

@blurymind blurymind changed the title Trying to load monogatari inside a react project Trying to load monogatari inside a react project - works on first load, fails on every next load Apr 22, 2021
@Hyuchia
Copy link
Member

Hyuchia commented Apr 22, 2021

Hey there, $_ready only running once is the right behavior, it listens to the window load event which in a SPA indeed only happens the first time it's loaded.

You can't really "reset" monogatari in that context since you can't run monogatari.init() more than twice. You would need to add monogatari in a component that persists navigation changes, once you have a single instance, then you would be able to reset the game as needed.

@blurymind
Copy link
Author

blurymind commented Apr 23, 2021 via email

@blurymind
Copy link
Author

blurymind commented Apr 23, 2021 via email

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

2 participants