Question

I am thinking of developing a game in pure JavaScript and html5, without using any third party plugins. The problem I am facing is that I cannot find a way to separate different "modules" of the game into separate threads, like the render job, game logic, asset loading and so on.

Web Workers seem to be able to separate code into different threads, but the problem with them is the limited information I can pass between them. For example, for the render job, I need to pass the whole "world", with all the entities, meshes, textures and so on for every update of the game, because worker threads cannot share memory. It can be optimized, like sending the static objects only on initialization (meshes, textures) and then send only the state of an object on update (it's world transform) but it still isn't desirable.

Is there a way to send large data between them or have them share some objects? Or is there a different method entirely of achieving true multithreading? I know there are easier ways of achieving this using plugins/ gears but I need to use only methods available in open web;

Was it helpful?

Solution

JavaScript's Web Worker is a better concurrent programming model in some way. For example, it's event driven and there's no shared object. This means you can't get into grid lock (because there's no lock at all) and an object can't get into invalid state by being modified by two threads at the same time.

The problem is you can't easily stack traditional game system on the top of this model. So you need to design the game system in a new way to adopt this programming model, and I think this might be costly.

OTHER TIPS

You probably want to look into Web Workers, I have no experience with them but have heard of them. Might lead you into the right direction.

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

web-workers are the closest thing to multithreading in js.
At the moment.
Full stop.

Sans Web Workers there's no real way to do multithreading in javascript that I know of.

You could use some logic to prioritize how much time should be spent doing certain tasks, though. You could make functions that store information in states (so loops could be exited and then picked back up at a later time). Each loop just knows how much time it has before it saves it's state and exits the function.

So the for loop would have a time condition, too...

//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 definitely prioritize your code this way. But, there are downsides. Saving your state doesn't lead to clean easy to follow code (especially with loops within loops). It also isn't as fast, since you're constantly checking time and saving states.

I recommend a functional approach for composing the aspects of your game. See e.g. http://www.flapjax-lang.org.

Licensed under: CC-BY-SA with attribution
Not affiliated with StackOverflow
scroll top