Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Does it make sense to use Web Workers for a game?

I am working on a game that has AI logic, movement, etc and drawing. Does it make sense to calculate moving and AI logic using Web Workers? But how do I do that -- because the workers need to know so much about the main thread like the positions of certain objects for collisions, amount of bullets, etc. It feels like impossible because the worker is completely separate from the main thread with no access what so ever. I do know that there a postMessage() system, but that feels so ... umm, painful?

For example, I have a rifleman object that represents a rifleman with a sprite, position, health, etc. I want him to patrol. So, how do I do that patrolling code on a worker? It would need pretty much the whole access to that object.

like image 386
Tower Avatar asked Aug 08 '10 15:08

Tower


People also ask

When should you use web workers?

Anyhoo, if you're doing an auto-save and taking 100ms to process data client-side before sending it off to a server, then you should absolutely use a Web Worker. In fact, any 'background' task that the user hasn't asked for, or isn't waiting for, is a good candidate for moving to a Web Worker.

Are web workers useful?

Web Workers help in making use of multi-core processors efficiently, otherwise, JavaScript Application ends up running on the single main thread. If you put this code into the browser console in Developer Tools, you will see that the page becomes unresponsive which is due to the code blocking the main thread.

What web workers can do?

Web workers cannot access the window or document objects. And web workers cannot manipulate the DOM. Instead, the web worker will have to provide the information to the main thread, and the main thread can manipulate the DOM directly. Truly, web workers should primarily be used for heavy and lengthy computation.

Are web workers supported in all browsers?

Web Workers And ES ModulesAll modern browsers support running JavaScript modules via <script type="module" src="file. js"> . All modern browsers apart from Firefox also now support the worker counterpart: new Worker("./worker.


1 Answers

I think it does make sense to use WebWorkers for a game, but yeah, it will mean you will have to keep a game state object which can be converted to valid JSON which you can pass to webworkers. On the brightside, you can also put a lot of intrinsic data inside those webworkers as well.

var gameState = {
   sprites: {
      {
         type: 'rifleman',  // damage, accuracy, speed etc set inside appropriate webworker.
         owner: 'playerA', 
         x: 100,
         y: 100,
         z: 0,
         level: 1, // used to fetch modifiers to dmg, acc, speed etc.

      },
      {
         // each sprite it's own state obj.
      }
   }
}

then you set up a webworker for patrolling and possible events (you can call other webworkers inside a webworker as well and process patrol events)

var patrolWorker = new WebWorker('patrolWorker');
patrolWorker.onmessage = function(e){
   render(e.data); // your render function, can ALSO be a webworker if you like ;)
}
patrolWorker.postMessage(gameState.sprites);

It must become clear by now, that using WebWorkers is actually very much an architectural decision, if you want to use them, it will involve a lot of refactoring. Without the refactoring, I doubt it's useful for you at all.

like image 146
BGerrissen Avatar answered Sep 20 '22 23:09

BGerrissen