Correct way to create mass entities

MassSpawner uses MassSpawnerSubsystem, which has two SpawnEntities methods.

I call this method, from an actors beginPlay, subsystems OnWorldBeginPlay and from blueprints. Sometimes I get the following error:

Synchronous API function FMassEntityManager::BatchCreateEntities called during mass processing. Use asynchronous API instead.

However, I didn’t find any asynchronous entity spawning methods anywhere.

MassSpawnerSubsystem doesn’t have any asynchronou API. Is there a safe way to use this subsystem then?

MassEntityManager doesn’t have any asychronous entity spawn, it has generic defer(), so I am supposed to write my own command?

Is there some already existing method for spawning entities that works out of the box?

[Attachment Removed]

Hi Martin,

The error you’re seeing is expected Mass behavior, but it can be misleading if interpreted as a “thread async” issue. What it actually indicates is that you’re attempting to mutate the entity archetype storage while Mass is executing processors, which is explicitly forbidden. This can be triggered by calling BuildEntity/DestroyEntity directly while IsProcessing() is true.

The error message is telling you to use deferred commands instead. MassSpawnerSubsystem is not safe to call during Mass processing, and all runtime structural changes must be deferred.

For example:

if (EntityManager->IsProcessing())
{
    // Request via command buffer since we can't move entities while processing is happening
    EntityManager->Defer().DestroyEntity(Entity);
}
else if (ensure(EntityTemplate))
{
    SpawnerSystem->DestroyEntities(TArrayView<FMassEntityHandle>(&Entity, 1));
}

Hope this clarifies the issue. Let me know if you have any further questions.

Best regards,

Henry Liiu

[Attachment Removed]

Hi Martin,

Here’s an example of using the Defer command:

EntityManager.Defer().PushCommand<FMassDeferredCreateCommand>(
    [this](FMassEntityManager& Manager)
    {
        // You may have your own implementation for spawning entities.
        SampleSpawnEntityExamples();
    });

Depending on the data and feature requirements for your use case, you may have your own CreateEntities functions and build a custom subsystem. You’re also correct that FMassDeferredCommand is primarily intended for internal engine use. Gameplay code generally shouldn’t rely on it directly. If you’re looking for a more convenient and supported option, UMassAgentComponent is likely the solution.

It’s also important to know that Mass relies on an execution context. Defer should not be interpreted as “thread async.” There are no lock, mutex, or atomic based “safe spawn” APIs. Gameplay thread code runs outside of Mass execution windows, while processors run inside controlled phases. If your code may be executed during processing, deferral is the only safe option.

Hope this helps clarify things. Let me know if you need any assistance with this.

Best regards,

Henry Liu

[Attachment Removed]

Well I know there is deferred destruction of entities, however I was more interested in deferred creation of entities, for which I couldn’t find any pre-made available functions. SpawnerSubsystem does several convenient things packed into one function (archetype lookup, creation, calling initialization processor), I certainly don’t want to reimplement all this functionality just to make it deferred.

Also is there some mutex to complement EntityManager->IsProcessing() ? There is no guarantee that processing won’t start between the if and the call itself unless there is some actual synchronization involved.

[Attachment Removed]