Search code examples
referencenullunreal-engine4unreal-blueprint

UE4: How can i detect if an actor refrence has been destroyed in blueprints?


In a level, I'm spwaning an actor which is designed to exist at some times and not at others. I'm spawning the actor fine, and plan to destroy it when i don't need it. But I want to make sure I'm not going to be in a state where I'm trying to act on the actor when it doesn't exist.

I'm thinking along the lines of a c++ object pointer with new, delete and nullptr. I initailise the variable to nullptr, assign the pointer when i use new, delete the pointer when i'm done, then re-assign the variable as nullptr. This is so i can check if the pointer is valid or not by checking that it's not equal to nullptr.

What (if anything) is the equivalent of this seemly basic concept in UE4's blueprints?


Solution

  • You can check if an object reference variable is non-null and not pending destruction using the Is Valid node:

    IsValid Node

    If you only need branching behavior, you can right click on the Get node for that object reference and select Convert to Validated Get:

    Convert to Validated Get example (credit to @Thunder_Owl on twitter for images)

    So, basically you can have a reference (maybe in your level or GameMode) to the most recent instance of that spawned actor, and then where you want to check if it's destroyed, you can check if that reference is currently valid.