Respawn an Actor?
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.
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:
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?
Could you make a temporary weapon variable? Like this:
ABaseItem* TemporaryItem; TemporaryItem = ItemToDrop; ItemToDrop->Destroy();
And then when you need to spawn it again, spawn the temporary item? You shouldn't get the pendingKill problem that way.
Do you have a particular root component selected? I know there can be issues if you don't. Try these specific settings (I'm assuming your root component is something called capsule)
These are literally the settings I have on weapon items that I drop in the world. They fall and tumble around on the ground. Sometimes they bounce and fall through the floor, so I have a special code to take care of it. Here's the header and the source file for a weapon called AProjectile, which spawns with AddForce and AddTorque so it flies and spins, and notice my timer triggered function that detects if it falls through the floor and stops it if it does so. You must select the mesh in the blueprint (I chose not to hardcode the mesh into the code).
I hope this helps.
answered Feb 07 '16 at 05:31 AM
Make a Spawner that will spawn the object once it picked up and place it insted of placing ASword it self. you can make it universal by making UClass varable which you can set in details tab once you place it on the level.
Or keep ASword instance as inventory item in character, just deactivate the visual components make it spawn it self in spot it been picked up (so you need to keep that information).
answered Feb 01 '16 at 06:42 AM
In a project I'm working on, I have my character pick up a sword and then pick up a second, but on a key press he can switch weapons from the currently equipped weapon to the secondary weapon. Since I want to bring the weapon back after I've switched it, I don't want to destroy it, just make it invisible. So I use SetActorHiddenInGame(true) from within the weapon class to hide it, and SetActorHiddenInGame(false) to bring it back. Both weapons must be stored as separate variables and must exist in the world in some class (the character class is perfectly fine) just have in your character's .h file something like AWeapon* weapon1; AWeapon* weapon2; etc. or make blueprints of them and store UClass pointers. You can call Destroy() and spawn new ones each time, but when you do this, you're not actually brining the weapon back, but creating a new one altogether. So if you had any info stored in that weapon it would spawn with default values for everything, u less you stored those values somewhere like in the character and set them in the new weapon class upon spawning. If you wanna spawn new actors rather than toggle their visibility, use SpawnActor.
answered Feb 02 '16 at 02:45 PM
Follow this question
Once you sign in you will be able to subscribe for any updates here