Skip to main content

Dubhe ECS (Entity Component System)

The Dubhe ECS library provides a powerful Entity Component System for building complex game logic and data management. It integrates seamlessly with the GraphQL client to provide real-time data synchronization and efficient querying capabilities.

Installation

Getting Started

Prerequisites

Before using the ECS library, ensure you have:
  1. A running GraphQL server with indexed data
  2. DubheMetadata configuration from your contract deployment
  3. A configured GraphQL client instance

Basic Setup

Core Concepts

Entities and Components

In the ECS pattern, entities are unique identifiers that have components attached to them. Components are data containers that define the properties of entities.

Component Discovery

The ECS system automatically discovers available components from your DubheMetadata:

Querying Entities

Basic Queries

Advanced Queries

Conditional Queries

Real-time Subscriptions

Component Change Subscriptions

Query Result Watching

Resource Management

Resources are global data that doesn’t belong to specific entities:

Performance Optimization

Batch Operations

Caching

Error Handling

Cleanup

Integration with Dubhe Client

The ECS system works seamlessly with the Dubhe client for contract interactions:

Best Practices

  1. Use subscriptions for real-time updates instead of polling
  2. Batch queries when possible to reduce network overhead
  3. Configure appropriate cache timeouts based on your data update frequency
  4. Handle subscription errors gracefully with retry logic
  5. Clean up subscriptions when components unmount
  6. Use specific field selections to minimize data transfer

Common Patterns

Game Loop Integration

API Reference

DubheECSWorld

The main class for interacting with the ECS system.

Constructor Options

Methods

  • hasEntity(entityId: string): Promise<boolean> - Check if entity exists
  • getEntity(entityId: string): Promise<any> - Get complete entity data
  • getComponent<T>(entityId: string, componentType: string): Promise<T> - Get specific component
  • queryWith<T>(componentType: string, options?: QueryOptions): Promise<PagedQueryResult<T>> - Query entities with component
  • queryWithAll(componentTypes: string[], options?: QueryOptions): Promise<string[]> - Query entities with all components
  • queryWithAny(componentTypes: string[], options?: QueryOptions): Promise<string[]> - Query entities with any components
  • onComponentAdded<T>(componentType: string, options?: SubscriptionOptions): Observable<ECSSubscriptionResult<T>> - Subscribe to component additions
  • onComponentChanged<T>(componentType: string, options?: SubscriptionOptions): Observable<ECSSubscriptionResult<T>> - Subscribe to component changes

Types