Skip to main content

HyperscapeService API Reference

Complete API reference for the HyperscapeService class in @hyperscape/plugin-hyperscape.

Overview

HyperscapeService is the core service that manages WebSocket connection to the Hyperscape game server. It provides:
  • Real-time game state synchronization
  • Command execution (movement, combat, inventory, etc.)
  • Event broadcasting to registered handlers
  • Automatic reconnection on disconnect
  • Movement completion tracking

Class: HyperscapeService

Constructor

Parameters:
  • runtime (optional): ElizaOS runtime instance
Note: Use HyperscapeService.start(runtime) instead of direct construction.

Static Methods

start(runtime: IAgentRuntime): Promise<Service>

Start the service for a given runtime. Returns existing instance if already started. Parameters:
  • runtime: ElizaOS runtime instance
Returns: Promise resolving to HyperscapeService instance Example:

Connection Methods

connect(serverUrl: string): Promise<void>

Connect to Hyperscape server via WebSocket. Parameters:
  • serverUrl: WebSocket URL (e.g., ws://localhost:5555/ws)
Throws: Error if connection fails Example:

disconnect(): Promise<void>

Disconnect from server (intentional disconnect, won’t auto-reconnect). Example:

isConnected(): boolean

Check if currently connected to server. Returns: true if connected, false otherwise Example:

setAuthToken(authToken: string, privyUserId?: string): void

Set authentication tokens for future connections. Parameters:
  • authToken: JWT authentication token
  • privyUserId (optional): Privy user ID
Example:

State Access Methods

getPlayerEntity(): PlayerEntity | null

Get the current player entity. Returns: PlayerEntity or null if not spawned Example:

getNearbyEntities(): Entity[]

Get all nearby entities (players, NPCs, resources, items). Returns: Array of Entity objects Example:

getGameState(): GameStateCache

Get complete game state snapshot. Returns: GameStateCache object Example:

getQuestState(): QuestData[]

Get current quest list. Returns: Array of QuestData objects Example:

getWorldMap(): WorldMapData | undefined

Get world map data (towns and POIs). Returns: WorldMapData or undefined if not loaded Example:

getLocalChatMessages(): Array<{from, fromId, text, timestamp, distance}>

Get recent chat messages from nearby players (within 50m). Returns: Array of chat message objects (newest first, max 10) Example:

Movement Methods

executeMove(command: MoveToCommand): Promise<void>

Execute movement command. Parameters:
  • command.target: Target position [x, y, z]
  • command.runMode (optional): Run instead of walk (default: false)
  • command.cancel (optional): Cancel current movement (default: false)
Example:
Note: Long-distance moves (>180 units) are automatically clamped to prevent server rejection.

waitForMovementComplete(timeoutMs?: number): Promise<void>

Wait for current movement to complete. Resolves immediately if not moving. Parameters:
  • timeoutMs (optional): Maximum wait time in milliseconds (default: 15000)
Returns: Promise that resolves when movement completes or timeout expires Example:

isMoving: boolean

Read-only property indicating if character is currently moving. Example:

Combat Methods

executeAttack(command: AttackEntityCommand): Promise<void>

Execute attack command. Parameters:
  • command.targetEntityId: Entity ID to attack
  • command.combatStyle (optional): Combat style (melee, ranged, magic)
Example:

executeChangeAttackStyle(newStyle: string): Promise<void>

Change attack style. Parameters:
  • newStyle: New combat style (attack, strength, defense, ranged, magic)
Example:

executeTogglePrayer(prayerId: string): Promise<void>

Toggle a prayer on/off. Parameters:
  • prayerId: Prayer ID to toggle
Example:

Inventory Methods

executeUseItem(command: UseItemCommand): Promise<void>

Use an item from inventory. Parameters:
  • command.itemId: Item type ID
  • command.slot (optional): Specific inventory slot
Example:

executeEquipItem(command: EquipItemCommand): Promise<void>

Equip an item from inventory. Parameters:
  • command.itemId: Item type ID
  • command.slot (optional): Specific inventory slot
Example:

executePickupItem(itemId: string): Promise<void>

Pick up an item from the ground. Parameters:
  • itemId: Entity ID of the ground item
Example:

executeDropItem(itemId: string, quantity?: number, slot?: number): Promise<void>

Drop an item from inventory to the ground. Parameters:
  • itemId: Item type ID
  • quantity (optional): How many to drop (default: 1)
  • slot (optional): Specific inventory slot
Example:

Resource Gathering Methods

executeGatherResource(command: GatherResourceCommand): Promise<void>

Gather from a resource (tree, rock, fishing spot). Parameters:
  • command.resourceEntityId: Entity ID of the resource
Example:

Banking Methods

openBank(bankId: string): Promise<void>

Open a bank session. Parameters:
  • bankId: Entity ID of the bank
Example:

bankDeposit(itemId: string, quantity: number): Promise<void>

Deposit items into bank. Parameters:
  • itemId: Item type ID
  • quantity: How many to deposit
Example:

bankDepositAll(): Promise<void>

Deposit all inventory items into bank. Example:

bankWithdraw(itemId: string, quantity: number): Promise<void>

Withdraw items from bank. Parameters:
  • itemId: Item type ID
  • quantity: How many to withdraw
Example:

closeBank(): Promise<void>

Close the current bank session. Example:
Complete Banking Example:

Social Methods

executeChatMessage(command: ChatMessageCommand): Promise<void>

Send a chat message. Parameters:
  • command.message: Message text
Example:

playEmote(emoteName: string): Promise<void>

Play an emote animation. Parameters:
  • emoteName: Emote name (e.g., ‘wave’, ‘dance’, ‘cry’)
Example:

Duel Methods

executeDuelChallenge(command: {targetPlayerId: string}): Promise<void>

Challenge another player to a duel. Parameters:
  • command.targetPlayerId: Character ID of player to challenge
Example:

executeDuelChallengeResponse(command: {challengeId: string, accept: boolean}): Promise<void>

Respond to a duel challenge. Parameters:
  • command.challengeId: Challenge ID from incoming challenge
  • command.accept: Accept (true) or decline (false)
Example:

getPendingDuelChallenge(): PendingDuelChallenge | null

Get current pending duel challenge (if any). Returns: PendingDuelChallenge or null Example:

Quest Methods

requestQuestList(): void

Request quest list from server. Response arrives via questList packet. Example:

requestQuestDetail(questId: string): void

Request detailed quest information. Parameters:
  • questId: Quest ID
Example:

sendQuestAccept(questId: string): void

Accept a quest. Parameters:
  • questId: Quest ID
Example:

sendQuestComplete(questId: string): void

Complete a quest (must be in ready_to_complete status). Parameters:
  • questId: Quest ID
Example:

Interaction Methods

interactWithEntity(entityId: string, interactionType: string): void

Interact with a world entity (chest, NPC, etc.). Parameters:
  • entityId: Entity ID
  • interactionType: Type of interaction
Example:

Event Handling

onGameEvent(eventType: EventType, handler: (data: unknown) => void | Promise<void>): void

Register an event handler. Parameters:
  • eventType: Event type to listen for
  • handler: Callback function
Event Types:
  • ENTITY_JOINED - Entity entered nearby area
  • ENTITY_UPDATED - Entity state changed
  • ENTITY_LEFT - Entity left nearby area
  • INVENTORY_UPDATED - Inventory changed
  • SKILLS_UPDATED - Skill levels/XP changed
  • CHAT_MESSAGE - Chat message received
  • COMBAT_DAMAGE_DEALT - Damage dealt in combat
  • DUEL_FIGHT_START - Duel fight started
  • DUEL_COMPLETED - Duel finished
Example:

offGameEvent(eventType: EventType, handler: Function): void

Unregister an event handler. Parameters:
  • eventType: Event type
  • handler: Handler function to remove
Example:

getLastRemovedEntity(): Entity | null

Get the last removed entity (for ENTITY_LEFT handlers). Returns: Entity or null Note: This is cleared after reading, so call it immediately in your ENTITY_LEFT handler. Example:

Autonomous Behavior Methods

startAutonomousBehavior(): void

Start autonomous behavior (full ElizaOS decision loop). Example:

stopAutonomousBehavior(): void

Stop autonomous behavior. Example:

isAutonomousBehaviorRunning(): boolean

Check if autonomous behavior is running. Returns: true if running, false otherwise Example:

setAutonomousBehaviorEnabled(enabled: boolean): void

Enable or disable autonomous behavior. Parameters:
  • enabled: Enable (true) or disable (false)
Example:

getBehaviorManager(): AutonomousBehaviorManager | null

Get the autonomous behavior manager. Returns: AutonomousBehaviorManager or null Example:

Goal Management Methods

syncGoalToServer(): void

Sync current goal to server for dashboard display. Example:

unlockGoal(): void

Unlock the current goal, allowing autonomous behavior to change it. Example:

syncAgentThought(type: string, content: string): void

Sync agent thought to server for dashboard display. Parameters:
  • type: Thought type (situation, evaluation, thinking, decision)
  • content: Thought content (markdown supported)
Example:

syncThoughtsToServer(thinking: string): void

Simplified wrapper for syncing LLM reasoning. Parameters:
  • thinking: LLM reasoning/thought process
Example:

Utility Methods

getLogs(): Array<{timestamp, type, data}>

Get recent game event logs. Returns: Array of log entries (newest first, max 100) Example:

Types

PlayerEntity

Entity

GameStateCache

QuestData

WorldMapData

See Also