Jump to content

How to not block event loop with function?


talamask
 Share

Recommended Posts

I am making an endless terrain project. Each chunk is generated as canvas texture, and in update() function based on current player position I determine whether I need to create/draw new chunks, leave chunks as they are or delete them. Chunk colors are based on heights generated with simplex noise.

My problem is that each chunk generation takes up to a second and if I do it straight in update() method game freezes until new chunk being generated, which gives me a noticeable freezes of entire game (see attached gif).

If only there could be a way to generate them independently of game loop and add then only when they are ready to be added, like to run that generate function on a separate thread. 

So I have 2 questions:

  1. Is there a built-in methods in Phaser to deal with similar problems?
  2. If not how would you do it then?

fly2.gif

Link to comment
Share on other sites

The chunks look quite tiny, you could get away with generating a whole stack of them in advance. So you're at least 10 'chunks' ahead of where you need to be. Then, in your update, start generating the next chunk, but do it in steps. I.e. progressively generate it, i.e. 20 lines at a time, or similar. So the next chunk isn't done in one hit, but in lots of smaller hits. Get the timing right and it could easily create the chunks as you scroll.

If the generation routine doesn't lend itself to this, either try and find one that does, or perhaps read about using Web Workers (which is outside the scope of Phaser)

Link to comment
Share on other sites

Quote

The chunks look quite tiny, you could get away with generating a whole stack of them in advance. So you're at least 10 'chunks' ahead of where you need to be. Then, in your update, start generating the next chunk, but do it in steps. I.e. progressively generate it, i.e. 20 lines at a time, or similar. So the next chunk isn't done in one hit, but in lots of smaller hits. Get the timing right and it could easily create the chunks as you scroll.

it will not help since at some point I will still have to generate some new chunks and in the end I will notice a freeze. Web workers was also among my ideas, I was hoping there are some more out-of-the-phaser-box ways :)

Link to comment
Share on other sites

21 hours ago, rich said:

No, that's my whole point, you don't generate the whole chunk in one go. You generate it iteratively, in steps. So over maybe 100 frames. Then it won't lag the game.

ok, so I got it working using generators so to go with iterative generation, it became much smoother.

Link to comment
Share on other sites

 Share

  • Recently Browsing   0 members

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