MassEntity: Select random entity from FMassEntityQuerry

Hey there,

currently, I am experimenting with Unreal’s Mass Framework. Therefore, I create this little space game with Factories, Stores, and Ships. The Factories create wares, the Stores sell wares and the Ships should move the wares from the Factories to the Stores.

My current problem is with the Ships and their Processor that should choose the next MoveToTarget. I just don’t know how to do that. The Processor has one FMassEntityQuerry to select all ships. And I’ve set up a second FMassEntityQuerry to select all Factories and Stores. But I cannot find any function that would allow me to select a random entity from the Factories/Stores, within the forEachEntityChunk of the Ships.

Any advice on this?

My code for reference:

void UTransferProcessor::ConfigureQueries()
{
	// Get all Transporters
	EntityQuery.AddRequirement<FMovementTargetFragment>(EMassFragmentAccess::ReadWrite);
	EntityQuery.AddRequirement<FStatusFragment>(EMassFragmentAccess::ReadWrite);
	EntityQuery.AddTagRequirement<FShipTag>(EMassFragmentPresence::All);
	EntityQuery.RegisterWithProcessor(*this);

	// Get all Factories and Stores
	TargetQuery.AddRequirement<FStorageFragment>(EMassFragmentAccess::ReadOnly);
	TargetQuery.AddRequirement<FProcessFragment>(EMassFragmentAccess::ReadOnly);
	TargetQuery.RegisterWithProcessor(*this);
}

void UTransferProcessor::Execute(FMassEntityManager& EntityManager, FMassExecutionContext& Context)
{
	EntityQuery.ForEachEntityChunk(EntityManager, Context, ([this](FMassExecutionContext& Context)
		{
			const TArrayView<FMovementTargetFragment> TargetsList = Context.GetMutableFragmentView<FMovementTargetFragment>();
			const TArrayView<FStatusFragment> StatusList = Context.GetMutableFragmentView<FStatusFragment>();

			for (int32 EntityIndex = 0; EntityIndex < Context.GetNumEntities(); ++EntityIndex)
			{
				if (StatusList[EntityIndex].Status != ShipStatus::LookingForTarget) {
					continue;
				}
				
				// Assign a random target here?!   <--------------------------

				StatusList[EntityIndex].Status = ShipStatus::MOVING;
			}
		}));
}