JavaScript multithreading for game development

I am thinking of developing the game in pure JavaScript and html5, without using any third-party plugins. The problem I am facing is that I can’t find a way to split the different “modules” of the game into separate threads, such as rendering work, game logic, loading assets, etc.

Web workers seem to be able to split the code into different threads, but the problem with them is the limited information that I can pass between them. For example, for a rendering job, I need to transfer the whole "world" with all the entities, meshes, textures, etc. For each game update, because worker threads cannot share memory. It can be optimized, for example, send static objects only during initialization (grids, textures), and then send only the state of the object when updating (this is a world transformation), but it is still undesirable.

Is there a way to send big data between them or share them with some objects? Or is there another way to fully achieve true multithreading? I know that there are simpler ways to achieve this using plugins / gears, but I need to use only available methods on an open network;

+4
source share
5 answers

JavaScript Web Worker is the best parallel programming model of its kind. For example, this event is managed and there is no shared object. This means that you cannot get into the grid lock (because there is no lock at all), and the object cannot get into an invalid state, being simultaneously modified by two threads.

The problem is that you cannot easily fold a traditional game system at the top of this model. Therefore, you need to redesign the game system in order to adopt this programming model, and I think it can be expensive.

+2
source

You probably want to look at Web Workers, I have no experience with them, but they heard about them. May lead you in the right direction.

http://ejohn.org/blog/web-workers/

+4
source

web workers are the closest thing to multithreading in js.
Presently.
Full stop.

0
source

Sans Web Workers there is no real javascript multithreading method that I know of.

You can use some logic to prioritize how much time you need to spend on certain tasks. You can create functions that store information in states (so that loops can be output and then assembled back later). Each cycle simply knows how much time it has before it saves its state and exits the function.

So the for loop will also have a time condition ...

//Loop over some iterator, exit if time is up for (i = 1; i < max || timeNotUp(); i++) { } //If exiting normally if (i == max) { //do somthing } //otherwise, save state else { this.i = i; this.otherStuff = otherStuff; } 

You can prioritize your code this way. But there are disadvantages. Saving state does not lead to simple code execution (especially with loops inside loops). It is also not so fast, as you constantly check the time and save the state.

0
source

I recommend using a functional approach to formulate aspects of your game. See http://www.flapjax-lang.org .

0
source

All Articles