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:- A running GraphQL server with indexed data
- DubheMetadata configuration from your contract deployment
- 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
- Use subscriptions for real-time updates instead of polling
- Batch queries when possible to reduce network overhead
- Configure appropriate cache timeouts based on your data update frequency
- Handle subscription errors gracefully with retry logic
- Clean up subscriptions when components unmount
- 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 existsgetEntity(entityId: string): Promise<any>- Get complete entity datagetComponent<T>(entityId: string, componentType: string): Promise<T>- Get specific componentqueryWith<T>(componentType: string, options?: QueryOptions): Promise<PagedQueryResult<T>>- Query entities with componentqueryWithAll(componentTypes: string[], options?: QueryOptions): Promise<string[]>- Query entities with all componentsqueryWithAny(componentTypes: string[], options?: QueryOptions): Promise<string[]>- Query entities with any componentsonComponentAdded<T>(componentType: string, options?: SubscriptionOptions): Observable<ECSSubscriptionResult<T>>- Subscribe to component additionsonComponentChanged<T>(componentType: string, options?: SubscriptionOptions): Observable<ECSSubscriptionResult<T>>- Subscribe to component changes