Responding to Unreal Engine Editor Selection (C++)

Tested in Unreal Engine version 4.27

This actor responds to being selected and deselected (and moved - see bonus section!) in the Editor. Written in C++.

Want to respond to your Actor being selected or deselected in the Unreal Engine Editor with C++? Read on!

Problem

I ran across this problem when implementing a volume that I wanted to randomly spawn actors inside of. I wanted to be able to see a sample of possible spawn locations directly in the editor before play began. I came across this forum postarrow-up-right explaining how to respond to editor selection, but unfortunately the solution proposed in that post only works when actors are selected in the viewport, and NOT if that actor is selected in the World Outliner.

Solution

This is an updated answer to the Unreal Forum post mentioned above that works when the actor gets selected/deselected in the viewport OR the world outliner.

In the Actor's constructor, bind the selection events like this:

Then declare and implement the OnSelectionChanged function:

In ExampleActor.h

In ExampleActor.cpp

And that's it! You can now write whatever code you want to run whenever your actor is selected or deselected in the editor.

Bonus: Responding to Actor Moved in Editor

This one is much simpler! All AActors have a native PostEditMove()function that gets called whenever the actor is moved in the editor.

In ExampleActor.h

In ExampleActor.cpp

Conclusion

With the above code, you should now be able to add functionality to any of your C++ actors in the editor when they are selected, deselected, or moved. You can use this to display useful information right in the editor, before play begins. Cheers!

Last updated