Mass AI Example
repository·main·Indexed 18 days ago
https://github.com/ji-rath/massaiexampleAn experimental project for Unreal Engine's Mass ECS framework providing implementations for high-performance AI, spatial partitioning, and large-scale entity visualization. It covers core Mass ECS concepts (Fragments, Tags, Traits, Processors), State Tree integration for AI logic, Smart Object interaction, Niagara-based visualization with LOD strategies, Spatial Hash Grid queries, and Mass entity persistence.
What's inside massaiexample
- Mass AI Example is a project designed to experiment with Mass, an Entity Component System (ECS) framework in Unreal Engine. It provides various implementation examples for high-performance entity simulation, including AI logic, visualization, and spatial queries.
How State Trees work with Mass
mainState Trees are used for high-level AI logic management, acting similarly to Behavior Trees (BT) where Fragments act as the Blackboard.
Key components include:
- Evaluators: Gather data to be used within the State Tree.
- Enter Conditions: Used on leaf nodes to determine if a leaf should be executed.
- Tasks: Execute the actual logic (similar to BT tasks).
- Transitions: Allow the State Tree to move between different branches based on conditions.
Important Implementation Notes:
- Data Mapping: Use the
UPROPERTYcategoryInstanceDatato defineInput,Output, orParametervalues. - Debugging: If a task fails due to missing fragments, check the task's header file for
TStateTreeExternalDataHandleto identify required dependencies. - Execution: In
UMassStateTreeProcessor, Tick on StateTree Tasks is only run once and utilizes subscribed signals.
Core Mass ECS Concepts
mainThe project follows the standard Mass ECS architecture. Understanding these primitives is essential for building with the framework:
- Fragments (
FMassFragment): Hold the raw data for entities. - Tags (
FMassTag): Used to filter entities based on specific properties or states. - Traits (
UMassEntityTraitBase): Used during entity configuration to define which fragments and tags an entity possesses. - Processors (
UMassProcessor): Perform logic by operating on groups of entities that share specific fragments or tags. - Subsystems: Used to bridge logic between the Mass framework and the standard Unreal game world.
- ObserverProcessors: Can observe multiple fragments or tags by overriding the
Register()function.
- Fragments (
Mass Visualization and LODs
mainEfficiently visualizing large numbers of entities (e.g., ~50k) requires specialized techniques:
- Niagara Visualization: Use Niagara systems to render entities efficiently.
- LOD Strategy: Implement multiple Levels of Detail (LODs) using bone vertex animation and billboards.
- Key Components:
Visualization TraitMass Viewer Info FragmentMass Actor Fragment(for actor-based visualization)Mass Visualization LOD Processor(Index 41)Mass LOD Collector TraitandMass LOD Collector Processor(Index 17). Note:LODCollectorProcessormay need to be enabled for high entity counts.
- Instanced Static Meshes (ISM): To apply unique textures/animations to ISMs, use an atlas and instance custom data. Use
FMassRepresentationFragment,FMassRepresentationLODFragment, andRepresentationSubsystemto manage this.
Mass Persistence (Save/Load)
mainThe project demonstrates a save/load approach for Mass entities using:
- A Signal Processor to write entity data.
- A Subsystem for centralized data processing.
- Integration with the standard Unreal Save System.
Spatial Hash Grid for Fast Queries
mainThe project utilizes a Spatial Hash Grid to perform fast spatial queries (e.g., finding entities within a specific range). This is the same underlying system used for Mass avoidance.
Implementation Details:
- Use a custom processor to update entity locations within the hash grid.
- Performance Note:
THierarchicalHashGrid2Dis generally more efficient for Mass avoidance thanTPointHashGrid3.
Mass Smart Object Integration
mainSmart Objects allow Mass entities to interact with specific objects in the world.
Key Requirements and Patterns:
- Definition: A
SmartObjectDefinitionrequires aUSmartObjectMassBehaviorDefinitionand all default tag filters to appear on theMass SmartObject Evalevaluator. - Execution: The
UseSmartObjectTaskexecutes the C++ logic defined inUSmartObjectMassBehaviorDefinition. - Accessing Objects: Use
FMassSmartObjectHandlerto interact with smart objects within Mass rather than accessing the smart object subsystem directly. - Safe Destruction: When destroying a smart object within
USmartObjectMassBehaviorDefinition, usePushCommand()to allow theSmartObjectUseTaskto release the object safely before destruction.
- Definition: A