To start off i have an actor with a mass agent component on it, and in that component i have an entity config which has a basic inventory trait that has exposed variables MaxWeight and MaxSlots. Here is my BuildTemplate override for the inventory trait:
UE_LOG(LogEntity, Log, TEXT("*** Building inventory template"));
FFPInventoryContainerFragment& container = BuildContext.AddFragment_GetRef<FFPInventoryContainerFragment>();
// Name is set in initializer
container.InventoryID = FGuid::NewGuid();
container.CurrentWeight = 0.0f;
container.MaxWeight = MaxWeight;
container.MaxSlots = MaxSlots;
container.InventoryItems.Reserve(MaxSlots);
BuildContext.AddFragment<FFPInventoryCommandQueueFragment>();
BuildContext.AddTag<FFPInventoryNeedsInitTag>();
UE_LOG(LogFringeEntity, Log, TEXT("\t>Inventory template built [%s[%s]] -- Max Weight: %f -- Max Slots: %d"), *container.InventoryName.ToString(), *container.InventoryID.ToString(), container.MaxWeight, container.MaxSlots);
This should set the maxweight and maxslots variables in my inventory container, which it does. Now, i have a player inventory and a small chest inventory both are built according to my console log:
Log LogEntity *** Building inventory template
Log LogEntity >Inventory template built [[8FC379DF4268AE6A6AF03D93D772F0B4]] -- Max Weight: 250.000000 -- Max Slots: 15
Log LogEntity >Adding Player Inventory Specific Fragments/Tags
Log LogEntity >Player Inventory Specific Fragments/Tags Added
Log LogEntity *** Building inventory template
Log LogEntity >Inventory template built [[5A5C416A4F7C7011EFA2B992701B967B]] -- Max Weight: 2000.000000 -- Max Slots: 35
but my initializer processor:
EntityQuery.ForEachEntityChunk(Context, [this](FMassExecutionContext& Context)
{
int32 numberOfEntities = Context.GetNumEntities();
UE_LOG(LogEntity, Log, TEXT("*** Inventory initializer observer executing, %d entities to process"), numberOfEntities);
TConstArrayView<FFPEntityInfoFragment> entityInfos = Context.GetFragmentView<FFPEntityInfoFragment>();
TArrayView<FFPInventoryContainerFragment> containers = Context.GetMutableFragmentView<FFPInventoryContainerFragment>();
TArrayView<FFPInventoryCommandQueueFragment> commandQueues = Context.GetMutableFragmentView<FFPInventoryCommandQueueFragment>();
for (int32 entityIndex = 0; entityIndex < numberOfEntities; ++entityIndex)
{
FFPInventoryContainerFragment& container = containers[entityIndex];
FFPInventoryCommandQueueFragment& commandQueue = commandQueues[entityIndex];
FMassEntityHandle entity = Context.GetEntity(entityIndex);
if (Context.DoesArchetypeHaveTag(*FFPPlayerInventoryTag::StaticStruct()))
{
container.InventoryName = FText::FromString(TEXT("My Inventory"));
Context.Defer().AddTag<FFPRequestSyncEntityToUITag>(entity); // For initial sync to ui
}
else
{
const FFPEntityInfoFragment& info = entityInfos[entityIndex];
if (!info.EntityData)
{
UE_LOG(LogEntity, Error, TEXT("\t>Tried to initialize inventory but the object data was null"));
continue;
}
container.InventoryName = info.EntityData->BasicData.Name;
}
// Make sure the queue is clear
commandQueue.PendingOperations.Empty();
Context.Defer().RemoveTag<FFPInventoryNeedsInitTag>(entity);
UE_LOG(LogEntity, Log, TEXT("\t>Initialized inventory [%s[%s]] -- Max Weight: %f -- Max Slots: %d"), *container.InventoryName.ToString(), *container.InventoryID.ToString(), container.MaxWeight, container.MaxSlots);
}
});`
Outputs:
Log LogEntity *** Inventory initializer observer executing, 1 entities to process
Log LogEntity >Initialized inventory [My Inventory[5A5C416A4F7C7011EFA2B992701B967B]] -- Max Weight: 2000.000000 -- Max Slots: 35
It seems that my processor is just using the data from the last template built?