FMassActorFragment with Mass Framework

Published on

What's purpose of FMassActorFragment and how to use that in Mass Framework? Mass Framework is an ECS for managing crowds and traffic, available since Unreal Engine 5.0. The code below uses the Unreal Engine 5.8 API.

Platforma

FMassActorFragment MassGameplay built-in resource

FMassActorFragment allows to access Mass controlled Actors from the Mass system. It may be used to trigger any function, push any values and so on, see the example below.

Adding FMassActorFragment to MassConfiguration

FMassActorFragment can be placed into the process either manually through the Assorted Fragments section of Mass Entity Config Asset, or with the help of code BuildContext.AddFragment<FMassActorFragment>(); placed in a Trait, see both methods at the Mass page.

Once FMassActorFragment is placed in the system, you can use it to access Mass Actors as follow:

USampleProcessor::USampleProcessor()
	: EntityQuery(*this) // registers the query with the processor
{
	bRequiresGameThreadExecution = true; // we touch actors - stay on the game thread
}

void USampleProcessor::ConfigureQueries(const TSharedRef<FMassEntityManager>& EntityManager)
{
	EntityQuery.AddRequirement<FMassActorFragment>(EMassFragmentAccess::ReadWrite);
}

void USampleProcessor::Execute(FMassEntityManager& EntityManager, FMassExecutionContext& Context)
{
	EntityQuery.ForEachEntityChunk(Context, [](FMassExecutionContext& Context)
	{
		const TArrayView<FMassActorFragment> ActorFragments = Context.GetMutableFragmentView<FMassActorFragment>();
		const int32 NumEntities = Context.GetNumEntities();
		for (int32 EntityIdx = 0; EntityIdx < NumEntities; EntityIdx++)
		{
			FMassActorFragment& ActorFragment = ActorFragments[EntityIdx];
			if (ASampleCharacter* Character = Cast<ASampleCharacter>(ActorFragment.GetOwnedByMassMutable()))
			{
				Character->DoAnything();
			}
		}
	});
}

Note: There are many other ways of using FMassActorFragment to do the same.

Vrealmatic consulting

Anything unclear?

Let us know!

Contact Us