# GUnrealEd is NULL in StartupModule

**URL:** <https://forums.unrealengine.com/t/gunrealed-is-null-in-startupmodule/295355>\
**Category:** Plugins\
**Tags:** modules, unreal-engine, Plugins, question\
**Created:** [October 5, 2014, 6:32pm UTC](https://forums.unrealengine.com/t/gunrealed-is-null-in-startupmodule/295355 "2014-10-05T18:32:58Z")\
**Posts on this page:** 4\
**Page:** 1

<div class="post-metadata">

**Author:** ![MSTF](https://d3kjluh73b9h9o.cloudfront.net/original/4X/2/8/5/28502927f1d97ba1051b3b47ccfe17233bf41ef0.png) [@MSTF](https://forums.unrealengine.com/u/MSTF)\
**Post date:** [October 5, 2014, 6:32pm UTC](https://forums.unrealengine.com/t/gunrealed-is-null-in-startupmodule/295355/1 "2014-10-05T18:32:58Z")

</div>

I creating a plugin, but cannot use “RegisterComponentVisualizer” because of GUnrealEd is NULL.

Checked github and usage is correct but not working.  
[https://github.com/EpicGames/UnrealEngine/blob/4.4/Engine/Source/Editor/ComponentVisualizers/Private/ComponentVisualizers.cpp](https://github.com/EpicGames/UnrealEngine/blob/4.4/Engine/Source/Editor/ComponentVisualizers/Private/ComponentVisualizers.cpp)

```
IMPLEMENT_MODULE(FMyVisualizerEditorModule, MyVisualizerEditor);

void FMyVisualizerEditorModule::StartupModule()
{
	RegisterComponentVisualizer(USplineTestComponent::StaticClass()->GetFName(), MakeShareable(new FSplineTestComponentVisualizer));
}

void FMyVisualizerEditorModule::ShutdownModule()
{
	if (GUnrealEd != NULL)
	{
		// Iterate over all class names we registered for
		for (FName ClassName : RegisteredComponentClassNames)
		{
			GUnrealEd->UnregisterComponentVisualizer(ClassName);
		}
	}
}

void FMyVisualizerEditorModule::RegisterComponentVisualizer(FName ComponentClassName, TSharedPtr<FComponentVisualizer> Visualizer)
{
	if (GUnrealEd != NULL)
	{
		GUnrealEd->RegisterComponentVisualizer(ComponentClassName, Visualizer);
	}

	RegisteredComponentClassNames.Add(ComponentClassName);

	if (Visualizer.IsValid())
	{
		Visualizer->OnRegister();
	}
}
```

---

<div class="post-metadata">

**Author:** ![MSTF](https://d3kjluh73b9h9o.cloudfront.net/original/4X/2/8/5/28502927f1d97ba1051b3b47ccfe17233bf41ef0.png) [@MSTF](https://forums.unrealengine.com/u/MSTF)\
**Post date:** [October 5, 2014, 10:36pm UTC](https://forums.unrealengine.com/t/gunrealed-is-null-in-startupmodule/295355/2 "2014-10-05T22:36:33Z")

</div>

After twelve hours finally found why it is NULL. The module is started in EngineLoop::PreInit and GUnrealEd is created in EngineLoop::Init. Changed LoadingPhase to PostEngineInit and it is working now!

> **[ELoadingPhase::Type](https://docs.unrealengine.com/5.0/en-US/API/Runtime/Projects/ELoadingPhase__Type/)**
>
> Phase at which this module should be loaded during startup.

---

<div class="post-metadata">

**Author:** ![errata](https://d1ap1mz92jnks1.cloudfront.net/user_avatar/forums.unrealengine.com/errata/32/891737_2.png) [@errata](https://forums.unrealengine.com/u/errata)\
**Post date:** [August 10, 2023, 4:40pm UTC](https://forums.unrealengine.com/t/gunrealed-is-null-in-startupmodule/295355/3 "2023-08-10T16:40:35Z")

</div>

at least as of UE 5.2.1:

# You cannot rely on setting the plugin’s loading phase to `PostEngineInit` to _guarantee_ the existence of `GEditor` inside `StartupModule()`!

Plugins will load at the earliest loading phase in their chain of dependencies, meaning the actual loading phase used by a plugin can become _earlier_ than the loading phase set in the .uplugin settings!

i.e. `GEditor` may not exist (yet) when the plugin is initialized.

### Example

1. Suppose we have a `Plugin A` which loads in `Default`.
2. A new `Plugin B` is added that depends on `Plugin A`. `Plugin B` loads in `PreDefault`.
3. Now `Plugin A` will be loaded in `PreDefault`, along with `Plugin B`.
4. `Plugin A` may suddenly and mysteriously start breaking as a result.

This can be rather surprising and is hard to figure out what’s wrong if you weren’t thinking about `Plugin A` when changing `Plugin B`’s loading phase or dependencies.

## Workaround

A more reliable way to access `GEditor` on module startup is to listen on the `PostEngineInit` delegate explicitly using `FCoreDelegates::OnPostEngineInit`.

```c++
 FCoreDelegates::OnPostEngineInit.AddRaw(this, &FMyModule::OnPostEngineInit);

```

Do note that if the loading phase _is_ `PostEngineInit`, then the `OnPostEngineInit` delegate will have already fired! so your `OnPostEngineInit` callback will not be executed in that case.

If you’re not worried about this, you can simply use an earlier loading phase, e.g. `"Default"` and always use the delegate, as the loading phase cannot get later than what’s specified in `.uplugin`, only earlier, IIUC.

```c++
void FMyModule::OnStartup() {
  // wait for GEditor to be ready
  FCoreDelegates::OnPostEngineInit.AddRaw(this, &FMyModule::OnPostEngineInit);
}

```

However **if you want to ensure fewer surprises when changing loading phases in the future** , you can wait for `GEditor` if it’s missing, or run your init immediately if `GEditor` is already around:

```c++
void FMyModule::OnStartup() {
  if (GEditor) {
    OnPostEngineInit(); // GEditor ready, init immediately
  } else {
    // wait for GEditor to be ready
    FCoreDelegates::OnPostEngineInit.AddRaw(this, &FMyModule::OnPostEngineInit);
  }
}

```

With this you should be able to adjust dependencies and loading phases without creating as many load order issues in other plugins.

edit: Also see this good suggestion from @Engelard:

> [@GUnrealEd is NULL in StartupModule](https://forums.unrealengine.com/t/gunrealed-is-null-in-startupmodule/295355/4):
>
> @errata Thanks! Your post helped a lot in my situation. I would even go further and recommend FCoreDelegates::OnBeginFrame , it is the very last point in the entire Editor loading process, when even viewports loaded and Editor displayed to the user. P.S. Don’t forget to FCoreDelegates::OnBeginFrame.Remove(MyHandle); once it reached your target part of code.

---

<div class="post-metadata">

**Author:** ![Engelard](https://d1ap1mz92jnks1.cloudfront.net/user_avatar/forums.unrealengine.com/engelard/32/903560_2.png) [@Engelard](https://forums.unrealengine.com/u/Engelard)\
**Post date:** [April 8, 2024, 9:56pm UTC](https://forums.unrealengine.com/t/gunrealed-is-null-in-startupmodule/295355/4 "2024-04-08T21:56:10Z")

</div>

@errata Thanks! Your post helped a lot in my situation.

I would even go further and recommend `FCoreDelegates::OnBeginFrame` , it is the very last point in the entire Editor loading process, when even viewports loaded and Editor displayed to the user.

**P.S.** Don’t forget to `FCoreDelegates::OnBeginFrame.Remove(MyHandle);` once it reached your target part of code.
