# Filtering actors in plugin::StartupModule()

**URL:** <https://forums.unrealengine.com/t/filtering-actors-in-plugin-startupmodule/388688>\
**Category:** Plugins\
**Tags:** actor, unreal-engine, editor, Plugins, CPP, question\
**Created:** [March 22, 2017, 6:37pm UTC](https://forums.unrealengine.com/t/filtering-actors-in-plugin-startupmodule/388688 "2017-03-22T18:37:49Z")\
**Posts on this page:** 5\
**Page:** 1

<div class="post-metadata">

**Author:** ![anonymous\_user\_3d09e359](https://d3kjluh73b9h9o.cloudfront.net/original/4X/2/8/5/28502927f1d97ba1051b3b47ccfe17233bf41ef0.png) [@anonymous\_user\_3d09e359](https://forums.unrealengine.com/u/anonymous_user_3d09e359)\
**Post date:** [March 22, 2017, 6:37pm UTC](https://forums.unrealengine.com/t/filtering-actors-in-plugin-startupmodule/388688/1 "2017-03-22T18:37:49Z")

</div>

Hi folks

In my plugin I would like to iterate and filter actors in plugin::StartupModule(). Following 's example I’m doing this as a simplified test in a vanilla Toolbar Button plugin:

```
	auto World = GEditor->GetEditorWorldContext().World();
	for (TActorIterator<AStaticMeshActor> ActorItr(World); ActorItr; ++ActorItr)
	{
		AStaticMeshActor* Mesh = *ActorItr;
		UE_LOG(LogTemp, Warning, TEXT("%s"), *ActorItr->GetName())
	}

```

These lines do exactly what I need if I have them PluginButtonClicked() but for some reason there are no Actors logged if I place the same lines in StartupModule(). I suspect this relates to which context/worlds exists at the time of StartupModule(). My .uplugin has “LoadingPhase”: “PostEngineInit” which solved a previous nullptr issue.

I don’t want to do this in PluginButtonClicked() because I need a one-time expensive call to the Shotgun api which builds the data-model that drives my menu extenders. StartupModule() seems like the right place but perhaps there is a better option, perhaps something post-load but pre-click.

Any help much appreciated

---

<div class="post-metadata">

**Author:** ![ExtraLifeMatt](https://d1ap1mz92jnks1.cloudfront.net/user_avatar/forums.unrealengine.com/extralifematt/32/918695_2.png) [@ExtraLifeMatt](https://forums.unrealengine.com/u/ExtraLifeMatt)\
**Post date:** [March 22, 2017, 7:37pm UTC](https://forums.unrealengine.com/t/filtering-actors-in-plugin-startupmodule/388688/2 "2017-03-22T19:37:51Z")

</div>

PostEngineInit just means the Engine has been initialized, but not that a world has actually been created.

You can see a list of the various phases and their descriptions [here](https://docs.unrealengine.com/latest/INT/API/Runtime/Projects/ELoadingPhase__Type/index.html).

It’s difficult to give you direction as I don’t know the requirements for your call. If you just need a valid world, you could look into hooking into something like [OnPostWorldInitialization](https://docs.unrealengine.com/latest/INT/API/Runtime/Engine/Engine/FWorldDelegates/index.html). But if you need to make that call every time someone adds/removes a Static Actor, then maybe doing it in a background or finding some hook for when an object is added to the world (I’m sure there is one - I just can’t remember one off the top of my head) would be better.

---

<div class="post-metadata">

**Author:** ![Shadowriver](https://d3kjluh73b9h9o.cloudfront.net/original/4X/2/8/5/28502927f1d97ba1051b3b47ccfe17233bf41ef0.png) [@Shadowriver](https://forums.unrealengine.com/u/Shadowriver)\
**Post date:** [March 22, 2017, 7:53pm UTC](https://forums.unrealengine.com/t/filtering-actors-in-plugin-startupmodule/388688/3 "2017-03-22T19:53:40Z")

</div>

There no way to do so from module start up since all modules get loaded before world is initiated. You can detect when world is initiated by binding to this event:

> **[UEngine::OnWorldAdded](https://docs.unrealengine.com/5.1/en-US/API/Runtime/Engine/Engine/UEngine/OnWorldAdded/)**
>
> Return the world added event.

You can also detect world destructions:

> **[UEngine::OnWorldDestroyed](https://docs.unrealengine.com/5.1/en-US/API/Runtime/Engine/Engine/UEngine/OnWorldDestroyed/)**
>
> Return the world destroyed event.

Keep in mind that in editor it will be triggered multiple times and contain different irreverent worlds, as Editor and PIE world is seperete and all previews are also creating own new world to display things, so use IsGameWorld to detect game world.

> **[UWorld::IsGameWorld](https://docs.unrealengine.com/5.1/en-US/API/Runtime/Engine/Engine/UWorld/IsGameWorld/)**
>
> Returns true if this world is any kind of game world (including PIE worlds)

As i remember PIE world get destroyed once you turn off play mode and new one is created in another rime, this will also happen on level change even in game builds if im not mistaken so prepare your code for world swaps.

---

<div class="post-metadata">

**Author:** ![anonymous\_user\_3d09e359](https://d3kjluh73b9h9o.cloudfront.net/original/4X/2/8/5/28502927f1d97ba1051b3b47ccfe17233bf41ef0.png) [@anonymous\_user\_3d09e359](https://forums.unrealengine.com/u/anonymous_user_3d09e359)\
**Post date:** [March 22, 2017, 8:31pm UTC](https://forums.unrealengine.com/t/filtering-actors-in-plugin-startupmodule/388688/4 "2017-03-22T20:31:01Z")

</div>

Thanks Matt, that’s exactly what I needed

In StartupModule()

```
FWorldDelegates::FWorldInitializationEvent::FDelegate OnWorldCreatedDelegate;
OnWorldCreatedDelegate = FWorldDelegates::FWorldInitializationEvent::FDelegate::CreateRaw(this, &FMyPlug::OnWorldCreated);
FDelegateHandle OnWorldCreatedDelegateHandle = FWorldDelegates::OnPostWorldInitialization.Add(OnWorldCreatedDelegate);

```

With

```
void FMyPlug::OnWorldCreated(UWorld* World, const UWorld::InitializationValues IVS) {

	for (TActorIterator<AStaticMeshActor> ActorItr(World); ActorItr; ++ActorItr)
	{
		AStaticMeshActor* Mesh = *ActorItr;
		UE_LOG(LogTemp, Warning, TEXT("%s"), *ActorItr->GetName())
	}

}

```

And the delegate somehow gets and passes the UWorld reference itself so no need for

```
auto World = GEditor->GetEditorWorldContext().World();

```

anymore.

I found a nice usage example here:

> <https://github.com/knepleyp/UTEmotionDetector/blob/master/Source/Private/EmotionDetector.cpp>

Funny how once you know the name of the thing you’re looking for, it all falls into place. Thanks for your help!

---

<div class="post-metadata">

**Author:** ![anonymous\_user\_3d09e359](https://d3kjluh73b9h9o.cloudfront.net/original/4X/2/8/5/28502927f1d97ba1051b3b47ccfe17233bf41ef0.png) [@anonymous\_user\_3d09e359](https://forums.unrealengine.com/u/anonymous_user_3d09e359)\
**Post date:** [March 22, 2017, 8:38pm UTC](https://forums.unrealengine.com/t/filtering-actors-in-plugin-startupmodule/388688/5 "2017-03-22T20:38:56Z")

</div>

Awesome, thanks for the extra links, my solution is above and I’ll keep an eye out for world swaps
