Lets say I pick up a sword on the ground called ASword. To remove it from the world I could call Destroy on ASword but that would prevent me from being able to re-spawn it into the world. I tried to call RemoveActor from UWorld but that did nothing.
What is the proper way to “Re-Spawn” an actor?
Below is my current implementation:
This is how I add an item to my inventory, It gets called when NotifyActorBeginOverlap() is called by the item (a sword in this case). I first check to make sure it was successfully added to the inventory before destroying/removing the item.
bool SEtherItemGrid::AddItem(ABaseItem* itemToAdd)
{
UWorld* theWorld = itemToAdd->GetWorld();
bool itemWasAdded = false;
int32 firstEmptySlotIndex = -1;
if (itemToAdd != NULL)
{
//Get the index of the first available slot.
firstEmptySlotIndex = this->FindFirstAvailableSlot();
if (firstEmptySlotIndex >= 0 && firstEmptySlotIndex < this->GetNumSlots())
{
//Add the item to the empty slot we just found.
itemWasAdded = this->AddItem(firstEmptySlotIndex, itemToAdd);
//Check to see if the item was added successfully.
if (itemWasAdded == true)
{
//Remove item from the world since it was successfully picked up.
itemToAdd->Destroy();
}
}
}
return itemWasAdded;
}
In my inventory, I have the option to right click an item and drop it. In other words this will remove the item from the inventory and then attempt to re-spawn it into the world. Here is what the code looks like for that:
void AStandardCharacter::DropItemFromInventory(ABaseItem* item)
{
UWorld* theWorld = this->GetWorld();
ABaseItem* removedItem = NULL;
FRotator spawnRotation;
FVector spawnLocation;
//Remove the item from the inventory.
if (item != NULL && myInventory.IsValid() == true)
{
removedItem = myInventory->RemoveItem(item);
//Take the removed item and re-spawn it into the world.
if (removedItem != NULL)
{
if (theWorld != NULL && itemToDrop != NULL)
{
spawnRotation = this->GetActorRotation();
spawnLocation = this->GetActorLocation();
//Spawn the item at the spawnLocation.
theWorld->SpawnActor<ABaseItem>(itemToDrop->GetClass(),
spawnLocation,
spawnRotation);
}
}
}
}
When Attempting to drop the item, nothing happens and I get a warning saying “SpawnActor failed because the spawned actor IsPendingKill”.
So what Is the proper way to do this?