Jump to content

updating the game loop


neon
 Share

Recommended Posts

Hello,

 

I have a long lasting loop where I compute something. So basically the game would be blocked. How can I still update all the game (rendering, physics etc.) manually from inside the loop?

 

something like 

 

while(...){

  my_blocking_logic();

 

  gameObject.update();

  gameObject.render();

}

Link to comment
Share on other sites

Having this logic in the game.update function won't work? The update function runs all the time so when your logic is true/false you could update the game accordingly. In general having while and blocking logic isn't considered a good practice.

 

Maybe you could explore some other ways of faking the block, for example when I builded a battleship-kind-of-game when it was the AI turn to calculate an attack I would display a message notifying the user that a move is being calculated.

Link to comment
Share on other sites

You should check out Web Workers. These are, quite literally, the only way to have multiple threads in JavaScript. JS is a run-to-completion language; inside a given function there is literally no way to pre-empt the execution of that function short of killing the JS interpreter. So if you have some loop that runs for two seconds that'll kill the game loop.

 

Stick that calculation in a web worker and it can post the message back to the main app, which can handle it as part of emptying the event queue. You'll be good to go.

Link to comment
Share on other sites

 Share

  • Recently Browsing   0 members

    • No registered users viewing this page.
×
×
  • Create New...