I'm working on a multiplayer game, and today I implemented basic "spatial hashing" in my entity-component-system.
Before implementing that algorithm, I was sending players actions to all connected players :
public void onPlayerMovement()(auto ref PlayerMovementMessage message, ClientSocket socket) { this.m_gameServer.sendToAll(message); } My code is now :
public void onPlayerMovement()(auto ref PlayerMovementMessage message, ClientSocket socket) { auto entity = this.m_gameServer.findEntity(socket); auto nearbyEntities = this.m_gameServer.world.getNearbyEntities(entity.get!PositionComponent().bucket) .filter!(e => e.hasComponent!PlayerComponent()).array(); if(nearbyEntities.length) { this.m_gameServer.sendTo(nearbyEntities, message); } } As you can see im now calling this.m_gameServer.world.getNearbyEntities() from SpatialHashingSystem.d to get nearby players.
onPlayerMovement function is called by class GameServer.d in a new thread, so it might be called before SpatialHashingSystem.d executes his algorithm, therefore calling getNearbyEntities() directly returns invalid data.
What can I do at this point ? Event system ? isnt that bad for a real time game ?
Thanks