Introduction

What is Mass?

Mass is a gameplay-focused framework for data-oriented calculations. Is an archetype-based Entity Component System (ECS). It’s designed for managing large amount of entities efficiently. It enables high-performance simulations of large crowd and complex interactions.

Data oriented design

Data-oriented design is a programming optimization strategy focused on making efficient use of the CPU cache. It emphasizes organizing and transforming data based on when and how it will be used, prioritizing data layout over object hierarchies.

OOP

Traditional object-oriented programming (OOP) tends to result in poor data locality. OOP organizes code around data types and their relationships rather than grouping related fields and arrays in memory for efficient access by specific functions.

ECS

The Entity Component System (ECS) is a software architectural pattern used to represent objects in a game world. Objects are modeled as entities composed of components (data), which are then manipulated by systems.

Mass

Is an archetype-based Entity Component System (ECS).

ECSMass
EntityEntity
ComponentFragment
SystemProcessor

An entity is a composition of fragments. These fragments get manipulated by processors.

An entity itself is just a unique identifier pointing to fragments. A processor defines a query that filters entities containing specific fragments. For example, a simple movement processor might query entities that have both a transform and velocity fragment, then update their positions by adding velocity to the transform.

Entities

A small unique identifier that references a combination of fragments and tags in memory.

Fragments

Data-only structs that entities can own and processors can query.

Shared fragments

Fragments that multiple entities can reference. These are often used for configuration.

Always use the UPROPERTY macro

Add the UPROPERTY macro in all properties of a shared fragment

Tags

Empty structs used by processors to filter entities based on whether the tag is present.

Subsystems

Mass supports UWorldSubsystems in processors, allowing you to encapsulate functionality to operate on entities.

To use this subsystem, you need to define its traits so Mass knows how to access it.

Archetypes

An archetype is a unique combination of fragments and tags.

This is a caption!

Each archetype holds a bitset containing tag presence information. Each bit represents whether a tag exists in the archetype.

Chunks

Each archetype contains an array of chunks with fragment data. A chunk stores a subset of entities in a struct-of-arrays-like format This maximizes CPU cache efficiency and allows for a great number of whole-entities to fit in the CPU cache.

Chunk sizes are tuned for next-generation cache sizes.

In the image below we can see the difference for Archetype 0 using a chunk versus storing them in a linear way.

The chunked Archetype gets whole-entities in cache, while the Linear Archetype gets all the A Fragments in cache, but cannot fit each fragment of an entity.

Having this chunks we avoid cache misses as we can fit the whole entity in the CPU cache.

Processors

Processors combine user-defined queries with functions that operate on entities.

Queries

Queries filter and iterate entities given a series of rules based on Fragment and Tag presence.

When adding a requirement, you must specify access permissions:

  • None
  • ReadOnly
  • ReadWrite

Processors execute queries in their Execute function. The query receives a lambda where fragments are processed.

Mutating entities

Use the Defer function from FMassExecutionContext to modify entities safely.

For more advanced mutations, use PushCommand.

Observers

Observers are specialized processors that react when a fragment or tag is added or removed.

Traits

Traits are C++ classes that declare a set of fragments and tags, used to create new entities.
To assign traits to an entity, create a DataAsset inheriting from UMassEntityConfigAsset.

Creating a trait

Create a class inheriting from UMassEntityTraitBase and override BuildTemplate.

Sources

Continue reading

If you want to continue learning about mass I wrote some blogs about replication.