You signed in with another tab or window. Reload to refresh your session.You signed out in another tab or window. Reload to refresh your session.You switched accounts on another tab or window. Reload to refresh your session.Dismiss alert
Node.js uses a single-threaded, event-driven architecture. That means everything in your onGameTick, event handlers, and custom logic runs on the same thread as the Haxball game engine.
If your code in onGameTick becomes computationally expensive (analytics, or complex decision logic), you can block the main thread and cause frame delay or lag.
✅ Solution: Use Workers to offload computation
Node.js provides the worker_threads module to let you run CPU-heavy logic in a separate thread.
Instead of doing all your processing inside onGameTick, send just the necessary data (player position, ball position, etc.) to a Worker. This keeps the game loop responsive while still allowing complex logic to run in parallel.
🧪 Example: Detecting proximity to the ball
This example shows how to send data from onGameTick to a worker thread that checks if the player is close to the ball.
room.js (Main Thread)
import{Worker}from"worker_threads";consttickWorker=newWorker("./tickWorker.js");room.onGameTick=function(){constplayers=room.getPlayerList();if(players.length===0)return;constball=room.getBallPosition();for(constplayerofplayers){if(!player.position)continue;tickWorker.postMessage({
player,
ball,});}};tickWorker.on("message",(message)=>{// Handle messages from the worker threadif(message.type==="proximityAlert"){const{ playerName, playerId }=message;room.sendAnnouncement(`@${playerName}, you are close to the ball!`,playerId,0x00ff00,"bold",2);}});
documentationImprovements or additions to documentation
1 participant
Heading
Bold
Italic
Quote
Code
Link
Numbered list
Unordered list
Task list
Attach files
Mention
Reference
Menu
reacted with thumbs up emoji reacted with thumbs down emoji reacted with laugh emoji reacted with hooray emoji reacted with confused emoji reacted with heart emoji reacted with rocket emoji reacted with eyes emoji
Uh oh!
There was an error while loading. Please reload this page.
-
💭 Context
Node.js uses a single-threaded, event-driven architecture. That means everything in your
onGameTick
, event handlers, and custom logic runs on the same thread as the Haxball game engine.If your code in
onGameTick
becomes computationally expensive (analytics, or complex decision logic), you can block the main thread and cause frame delay or lag.✅ Solution: Use Workers to offload computation
Node.js provides the
worker_threads
module to let you run CPU-heavy logic in a separate thread.Instead of doing all your processing inside
onGameTick
, send just the necessary data (player position, ball position, etc.) to aWorker
. This keeps the game loop responsive while still allowing complex logic to run in parallel.🧪 Example: Detecting proximity to the ball
This example shows how to send data from
onGameTick
to a worker thread that checks if the player is close to the ball.room.js (Main Thread)
tickWorker.js (Worker Thread)
Beta Was this translation helpful? Give feedback.
All reactions