Hopefully someone can help!
I’m trying to figure out why setting interface pointer in reference to a duplicated object does not work. During method execution the value is set but once the method is completed, the value is NULL.
Basic idea behind the function is to take a “storable item” interface reference (an generic representation of storable object) which has physical/mesh blueprint implementation, make a duplicate virtual instance (UObject), remove the original item and append the UObject to a property of one of the storage slots
.
I am able to make a virtual duplicate and destroy the original physical actor. Also i’m able to set the duplicated UObject as property VirtualStoredItemRefrence
in ItemContainer
while in the loop (see snippet 1) but once the method StoreItem
finishes, when iterating over all ItemContainers the VirtualStoredItemReference
is a null object.
Snipped 1:
// Create virtual replica of original object
auto Replica = DuplicateObject(Item.GetObject(),Item.GetObject()->GetOuter());
IStorableItem* ReplicaInterface = Cast<IStorableItem>(Replica);
ItemContainer->VirtualStoredItemReference = ReplicaInterface;
Code:
EStorageStatus AStorageArea::StoreItem(TScriptInterface<IStorableItem> Item)
{
auto SlotContainers = GetStorageSlotContainers();
if(SlotContainers.Num() == 0) return EStorageStatus::FAILED_NO_SLOT_CONTAINERS_AVAILABLE;
// Process all present containers (must be manually placed in blueprint)
for(AStorageSlotContainer* Container : SlotContainers)
{
// Process only available storage slots
auto StorageSlots = Container->GetAvailableStorageSlots();
for(AStorageSlot* Slot : StorageSlots)
{
// Item to be stored has "storage container" that "can be stored in slot"
if(Slot->SupportedStorageItemContainer == Item->GetStorageItemContainerClass())
{
// Spawn object container (BP implementation)
AStorageItemContainer* ItemContainer = GetWorld()->SpawnActor<AStorageItemContainer>(
Item->GetStorageItemContainerClass().Get(),
Slot->GetActorLocation(),
Slot->GetActorRotation()
);
// Spawn child actor component
UChildActorComponent* ChildActorComponent = NewObject<UChildActorComponent>(Slot);
ChildActorComponent->SetChildActorClass(AStorageItemContainer::StaticClass());
// Attach to component
FAttachmentTransformRules AttachRules(EAttachmentRule::KeepRelative, EAttachmentRule::KeepRelative, EAttachmentRule::KeepRelative, true);
ItemContainer->AttachToComponent(ChildActorComponent, AttachRules);
// Register component
ChildActorComponent->RegisterComponent();
// Create virtual replica of original object
auto Replica = DuplicateObject(Item.GetObject(),Item.GetObject()->GetOuter());
IStorableItem* ReplicaInterface = Cast<IStorableItem>(Replica);
ItemContainer->VirtualStoredItemReference = ReplicaInterface;
// Destroy original actor
GetWorld()->DestroyActor(Cast<AActor>(Item.GetInterface()));
return EStorageStatus::SUCCESS;
}
}
this->Debug_FindFiledVirtualStoredItemReferences();
}
return EStorageStatus::FAIELD_NO_SLOTS_AVAILABLE;
}