Download

How do I get a C++ reference to the world while inside the editor?

Edit: Well, I got it to work. This is embarrassing. It seems all I needed to do was to reference GWorld. I swear, that was the first thing I did. Maybe one of the intermediate steps got GWorld to work, but I kept trying to use GEditor->GetWorld() and the other commands. I’m not sure which step it was, but apparently you can just use GWorld in the editor.

I’ve been working on this all day. It seems like it should be a pretty simple task. I have an actor that has this function set up in the header file:



UFUNCTION(BlueprintCallable, CallInEditor, Category = "WorldCreation") virtual void SpawnNodes();


And I drop one of these in the level, and I can see that it has a button that I can click. And I click the button and so far I can get it to write to the log. So far so good.

But I’m having trouble making this function spawn other actors in the editor. I’ve narrowed the problem down to where I am trying to get a reference to the world.



// UEditorEngine* EditorEngine = GEditor; // So far so good.
// UWorld* World = GEditor->GetWorld(); // Doesn't build.  error LNK2019: unresolved external symbol
// UWorld* World = GEditor->GetEditorWorldContext().World(); // Doesn't build.  error LNK2019: unresolved external symbol
// UWorld* World = GEditor->LevelViewportClients[0]->GetWorld(); // Doesn't work.  LevelViewportClients not accessible.
// FLevelEditorViewportClient* EditorViewportClient = GEditor->GetLevelViewportClients()[0]; // Doesn't work, but is that because this isn't allowed in C++?
// UWorld* World = GEditor->EditorWorld; // Compiles, but crashes the editor
// UWorld* World = GEditor->EditorWorld->GetWorld(); // Doesn't work.


I did some searching and it looks like I needed to add the right header references, so I added these to the top of my cpp file.



#include "UnrealEd.h" // Added this later, but still doesn't work
#include "LevelEditor.h"
#include "Editor.h"


But that didn’t work, so I did some more searching and it looks like if I want to do this in the editor, I have to open up my game’s Build.cs file and add



PublicDependencyModuleNames.Add("UnrealEd");


But that didn’t work, so I did some more searching and it looks like I have to create a separate editor-only module and make that module depend on UnrealEd. So I downloaded this module tool, enabled it in the plugins menu, restarted the editor, and then I made a new module called HXGameEditor. I’m not sure if I set it all up right. But I added

HXGame.Target.cs



public class HXGameTarget : TargetRules
{
    public HXGameTarget( TargetInfo Target) : base(Target)
   {
      Type = TargetType.Game;
      DefaultBuildSettings = BuildSettingsVersion.V2;
      ExtraModuleNames.AddRange( new string] { "HXGame" } );
      ExtraModuleNames.Add("HXGameEditor");
   }
}

HXGame.uproject



"Modules": 
...
{
   "Name": "HXGameEditor",
   "Type": "Editor",
   "LoadingPhase": "Default",
   "AdditionalDependencies": 
         "Engine",
         "UnrealEd"
      ]
   }
]


HXGameEditor.Build.cs


public class HXGameEditor : ModuleRules
{
   public HXGameEditor(ReadOnlyTargetRules Target) : base(Target)
{
...
PublicDependencyModuleNames.AddRange(
   new string]
   {
      "Core",
      "UnrealEd"
   }
);

And I put the includes inside #if WITH_EDITOR, and same with the attempts to get a reference to World. Nothing is working. Can any of you see why I’m not able to access World from a function that runs in the editor?

This will get the world.


GetWorld();