Why does destroying an object in the world before I get the Variable for it and cast to it work?

Hoping you can help me solve a blueprint mystery. Here’s the situation:

  1. I’ve got an actor BP in the world, and in my player character I check for overlap and cast to that item.
  2. If the cast checks out, I grab the state of a variable in that item (of class:actor) spawn it, add it to an array and then remove the spawned item from the world. This all works great.

Here’s the part I don’t understand: after I’ve done all that, I want to remove the initial object that I collided with, the one that was in the world to begin with. I use a destroy actor node, with the proper target set at the end of the sequence but it doesn’t work.

Oddly, if I destroy the object that’s in the world BEFORE I get the variable from it and cast to it, it works? There must be something going on with the way blueprints work that I do not fully understand, because to my mind, you shouldn’t be able to get a variable from an object you just destroyed?

I’ve tried this with and without adding the item to the array, thinking that maybe once you add an item to an array you can’t delete it out of the world possibly, but it still has the same behavior. I’ve also tried triggering destroying the actor with an event, and doing it before I destroy the other actor I’m spawning. The only configuration that works is destroying it BEFORE I spawn the actor from the Class:Actor variable in it. Any ideas? This one’s got my stumped!

SpawnActor node does not require a target Actor.Thats why it is working even after the Actor is destroyed. But Destroy node needs a target and you are passing the Actor whch just got destroyed, which will not work.

In simpler terms, this is what is happening in each case

Case1:

YourActor->destroy()

SpawnActor(class of Youractor, …, …,) → notice that YourActor instace is not used here. You only use the classname

Case2:

YourActor->destroy()

YourActor->destroy() → YourActor is already destroyed so this wont work

However I do agree with you that this is very confusing. In normal code (like C++), you will not get to access any of the member varibales after the object is destoyed. Perhaps the BP compiler works in a different way and calculates values for all output pins for a node before the output execution pin is fired?

This is tangential, but I’m seeing something problematic in both your examples. You’re adding an actor reference to an array, and then destroying that actor - that means your array entry will return None, instead of that actor. Think of object’Actor’ arrays as a list of names/addresses of actors that exist in the world - if you destroy one of them, and try to access the address, it will error out with an Accessed None error.

If you want to add it to the inventory and hide it, you’re better off doing it by hiding that actor’s visible components, or (if your inventory items don’t carry persistent properties) simply storing a reference to its class, so you can spawn and destroy it at will.

That’s a very good point as well! I suspected it should be working the way you describe, oddly enough though, it does keep the Actor in the array even after I’ve destroyed it from the world. Maybe it’s not garbage collected right away or something?