Networking

Networked Actors

NetworkedActor, NetworkedGameObject, and state synchronization.

The engine provides networked variants of the core entity classes that can serialize and sync state over the network.

NetworkedActor

Extends Actor with network sync capabilities.

import { NetworkedActor } from '@mavonengine/core'

class MyNetworkedPlayer extends NetworkedActor {
  $typeName = 'MyNetworkedPlayer'

  updateFromNetwork(data: unknown): void {
    // Apply incoming server state to this actor
  }
}

Abstract members

MemberDescription
$typeNameString identifier used to reconstruct entities on the client
updateFromNetwork(data)Apply a state snapshot from the server

NetworkedGameObject mixin

Use NetworkedGameObjectMixin if your class already extends something else:

import { NetworkedGameObjectMixin, Entity3D } from '@mavonengine/core'

class MyNetworkedEntity extends NetworkedGameObjectMixin(Entity3D) {
  $typeName = 'MyNetworkedEntity'

  updateFromNetwork(data: unknown): void { /* ... */ }
}

NetworkedLivingActor

Combines NetworkedActor with the LivingActor health/damage system for networked entities that can take damage.

import { NetworkedLivingActor } from '@mavonengine/core'

class MyNetworkedEnemy extends NetworkedLivingActor {
  $typeName = 'MyNetworkedEnemy'
  health = 100
  maxHealth = 100

  isDead(): void { /* ... */ }
  takeDamage(amount: number): void { /* ... */ }
  heal(amount: number): void { /* ... */ }
  updateFromNetwork(data: unknown): void { /* ... */ }
}

State sync

syncStateStack

Use syncStateStack to synchronize an actor's state machine stack over the network:

import { syncStateStack } from '@mavonengine/core'

// On the server: serialize state
const stateData = syncStateStack(actor)

// On the client: apply state
actor.updateFromNetwork(stateData)

StateFactory type

The StateFactory type maps type name strings to state constructors, enabling the server to describe states that the client can reconstruct:

import type { StateFactory } from '@mavonengine/core'

const stateFactory: StateFactory = {
  'IdleState': (entity) => new IdleState(entity),
  'WalkState': (entity) => new WalkState(entity),
}

Player

Player (@mavonengine/core/Networking/Entities/Player) is the base class for connected clients. It extends NetworkedLivingActor and adds:

MemberDescription
channelThe client's geckos.io channel, set by the server on connection
connectedAtEpoch ms when the player was created
displayName / setDisplayName(name)Human readable name shown in the dashboard and MCP tools, falls back to the id
connectionLive WebRTC details: address, rttMs, bytesSent, bytesReceived (server only)
lastProcessedSequenceIdThe last client command the server applied, sent in the state sync for reconciliation
trackedEntitiesIds of the entities currently synced to this player

NetworkedEntityFactory

When the client receives an entity it doesn't know yet, it creates it from its $typeName. Register a factory per type on the client:

import NetworkedEntityFactory from '@mavonengine/core/Networking/NetworkedEntityFactory'

NetworkedEntityFactory.instance.register('Player', data =>
  new Character(data.id as string, false, new Vector3(data.position.x, data.position.y, data.position.z)))

// In the state handler
const entity = NetworkedEntityFactory.instance.create(inEntity.$typeName, inEntity)
if (entity)
  Game.instance().world.entities.items.set(inEntity.id, entity)

create() returns null for unregistered types.

NetworkedEntityState

Wraps an EntityState with network metadata for state stack synchronization.