How to remove item from game after looting, but keep a copy of it in your inventory array? C++

I’m trying to loot an Item from the world, add it to the inventory array of item pointers… All is good it replicates I can spawn them and loot them and they keep replicating.

If I however call LootedItem->Destroy(); everything goes to ■■■■… Obviously my pointer goes to null, so bye bye item in the inventory…

I try and fix this by creating a copy if the Item before calling destroy and adding that to my inventory…

Looting and adding item to inventory:

Item* ItemCopy = DuplicateObject(LootedItem, ItemOwner);

PlayersInventory[FirstFreeSlot] = ItemCopy;


all is well again, it replicates from the servers copy of inventory to the owning clients inventory. I spawn it again, everything fine both server and client have no Item in inventory and I can see the item in the world on every client.

However when I loot the spawned item again, It only shows up on the servers inventory, the client has nothing in it’s inventory.

Spawning the item in the world

Item* SpawnedItem = GetWorld()->SpawnActor<ALootable>(ItemToSpawn->GetClass(), SpawnLocation, SpawnRotation);

Does anyone have an idea what is going wrong? or some way to get this replication to work?

I advice not to destroy it at all. Instead just disable the actor so that it no longer interacts with the world:

// LootedItem->SetActorTickEnabled(false); // optional

At least that is what I do for SP games. I assume it would also work for MP games since the actor is (hopefully) already replicated. And this also makes it easier for dropping the item back into the world (if needed).

EDIT: Of course this only works if you don’t need the 3D model for the inventory (I assume you use some 2D Texture there) because otherwise that too would be invisible.