How to get notification when Actor is selected in Editor?

Hi,
I am trying to perform some tasks in the Editor when an actor I created get selected.
Basically I would like the Actor to be notified when it is clicked in Editor mode.
However i couldn’t find any way of doing this, because OnClicked() events are for play mode (I think).
Do someone know how to do this?

I am trying to do the same. Did you get any answer ? did you advance on it ?

There is a way to check if an actor is selected in editor by checking with AActor::IsSelected(). You can also use a TActorIterator to go through every actor of your scene and return true when on is selected.

for(TActorIterator<AActor> ActorIterator = ItBegin; ActorIterator; ++ActorIterator)
	{
		if(ActorIterator->IsSelected()) //do something	
	}

But there is one problem I haven’t been able to figure out yet : when to call this method. I haven’t managed to use Tick in the editor when the game isn’t running… so yeah…

You can create a derive from UUnrealEdEngine and override NoteSelectionChange(). Then you can call GetSelectedActors() in that function. GetSelectedActors() returns a USelection. You can loop on this data like this:

	for (uint16 i = 0; i < Selection->Num(); i++)
	{
		AActor* Current = Cast<AActor>(Selection->GetSelectedObject(i));
	}

You can derive from UUnrealEdEngine and override NoteSelectionChange(). Then you can call GetSelectedActors() in that function. GetSelectedActors() returns a USelection. You can loop on this data like this:

	for (uint16 i = 0; i < Selection->Num(); i++)
	{
		AActor* Current = Cast<AActor>(Selection->GetSelectedObject(i));
	}

This does not seem to be getting called when selecting actors in the editor? Do i have to enable anything?

Haven’t found a way to get an event for single actor/object, buy you can bind an event to get notified by any selection, then check if its this actor/object.

AMyActor::AMyActor()
{
#if WITH_EDITORONLY_DATA
	USelection::SelectObjectEvent.AddUObject(this, &AMyActor::OnObjectSelected);
#endif
}

#if WITH_EDITORONLY_DATA
void AMyActor::OnObjectSelected(UObject* Object)
{
	if (Object == this)
	{
		// DO STUFF
	}
	else if (!IsSelected())
	{
		// UNDO STUFF
	}
}
#endif
1 Like

For some reason this code seems to run twice. Any idea why this can happen?
I’m using is in a UObject inside an editor plugin.

I found it. I create a new object everytime the edit mode is enabled!

The active editor mode has a tick function. It is run every time when you are on the editor using that mode. Maybe that helps