How to check if an actorcomponent still exists?

I use physics constraints for some kind of ‘sticky’ thing that sticks to the first thing it touches. This works great, but sometimes the thing it has been stuck to gets destroyed. Is there a way to be notified of the destruction of an actorcomponent that is part of a physics constraint?

What I was thinking about… obviously you have the pointer to the actorcomponent from the physics constraint, can you check if an actor component is still not destroyed using it’s pointer? I assume after you destroyed an actor component, the pointer becomes invalid, so I can’t really call functions on it or anything. Is there a safe way to check if a pointer to an actor component is still valid and alive?

Thanks!

My Assumption is that “Thing that it’s stuck to” is an actor in the world that your sticky actor sticks to.

Short Answer… Actor.IsValid() may do the trick… but it may not, and if it does not…

Handling the destruction of an Actor with your ActorComponent:
There are two functions BeginPlay and EndPlay. BeginPlay is called when the level loads or when an Actor is spawned. EndPlay is called when the game quits and/or when the Actor is Destroyed. So put your code to handle your sticky situation in the EndPlay function.

My alternative Assumption that “Thing that it’s stuck to” is an actor and your stick actor uses the physics constraint instead of the actor it’s stuck to:

My suggestion is that anything that is “destructible” have a base class or interface that it implements e.g. DestructableActor or IDestructableActor. Basically Any Actor that would be destructable and you want your sticky thing to stick to would inherit from your DestructableActor that implements registering your stick object to it and handles clearing it in EndPlay. If you have multiple sticky objects that could be registered use an Array.

yeah the stick actors initiates and contains the physics constraint, because it can stick to many different actors. I guess adding code to a base class that all my actors derive from (which doesn’t exist yet) could work. It should then contain a list of all the sticky actors that are attached to it (could be multiple), and notify all stricky actors of it’s destruction on EndPlay. I was hoping for something more simple but I guess it would work.
Anyway the physics part of the engine apparently knows that one of the actors of the constraint is destroyed, because the engine does not crash when you destroy when you destroy one of the actors. Too bad there isn’t a notification about this that developers can hook into…

If you are doing this in blueprint you can also use the “On Tick” event to check if the Actor being pointed to is not equal to “none”… that may do the trick as well… As far as I know as long as are are dealing with a UProperty, UE4 ~should~ set it to null/empty if you destroy the actor…

Also could have 500+ sticky objects constantly checking if they are sticking to things every single tick… may not be a good thing.

that could work too in c++ I suppose. I think there is a flag that marks actors which are to be destroyed, because afaik they don’t really get destroyed until the very end of the tick. But that would require to check it every tick for every sticky actor…

yes, that like I said that may not be a good thing. It would be better to have an event that signals all the sticky objects that are attached to the actor being destroyed, that way your cycles are not being eaten by 1000+ sticky objects