bberak / react-game-engine

A lightweight Game Engine for the web (React) 🕹⚡🎮
MIT License
413 stars 24 forks source link

Called the GameEngine's stop() function but nothing happens #11

Closed JiboStore closed 1 year ago

JiboStore commented 4 years ago

Expect: the entire simulation to be paused Reality: systems are still being called, entities are still ticking

const.js let gameEngineRef = React.createRef()

game.js

render() {
  return (
    <GameEngine ref={Const.gameEngineRef} entities={...} systems={...}/>
  );
}

insideoneofentities.js

if (conditionFulfilled) {
    console.log(Const.gameEngineRef); // everything looks ok, and current is the gameEngine, current.stop() also has the function impl (that call the timer to stop)
    Const.gameEngineRef.current.stop(); // nothing happens
}
bberak commented 4 years ago

Hey @JiboStore,

Thanks for creating this issue. That is definitely not the expected behaviour - I'll try debug this and get back to you. Just a heads up - I'm a bit busy with with this week.

JiboStore commented 4 years ago

Hi @bberak Thank you very much for your reply!

Actually I figured out that it actually works if I call stop() from the component that return itself. Previously I am exposing the ref globally and get the entity and/or system to call the ref.stop() which doesn't work. But if I `dispatch({type: 'stop-game'}) from entity and/or system and then inside the component that return I listened to the event:

handleEvent(e) { if ( e.type == 'stop-game' ) { ref.stop(); } }

it will work.

I am not sure of the intended use though..

To be honest, I didn't quite understand reading your repo code lol.. I am quite new to javascript (although I have game-dev background on C++ and OpenGLES)

bberak commented 4 years ago

Hey @JiboStore, the way you've got it working now is the intended usage.. Apologies that the docs are quite sparse in many areas,. Totally understand that it looks quite foreign - plus it doesn't help that you need to have some knowledge of React to follow along.

Hope your project is going well in any case!