I’m creating an editor plugin, and depending on whether the game is running or not I need to get either pass either an object from a level (if the game is running), or use the editor world itself.
For that reason, I need the ability to know whether we are running the game or are still in the editor.
Preprocessor directives “#if WITH_EDITOR” and the likes do not work because they tell about the build configuration, not whether we are actually in “edit-mode” or playing the game. Any advice?
Sadly I can not do that, because when in editor, trying to get the world of an object returns null.
When the game is running, I want to access the object’s world.
When it’s not running, I want to access the Editor World. I guess I could try and get the object’s world and if it returns nullptr I can just use the Editor World instead…
Hey, you can access GEngine->GetWorldContexts, iterate over all the contexts and if at least one world is PIE you know at least one world is playing right now. That’s how I found it inside engine code a couple times.
I know this thread is very old but the solution I found gives very precise results through the enumerator “EWorldType::Type” present in “Engine/EngineTypes.h” and you can easily extend it for PIE, Game, GamePreview …
Additionally, you can ask every UObject where it is currently running.
// Utils.h
#pragma once
#include "CoreMinimal.h"
#include "Kismet/BlueprintFunctionLibrary.h"
#include "Utils.generated.h"
UCLASS()
class TTCOMMON_API UUtils : public UBlueprintFunctionLibrary
{
GENERATED_BODY()
public:
// returns true if the given 'Object' is instantiated and running in Editor Preview (e.g. Persona, AnimBP...)
UFUNCTION(BlueprintCallable, Category = "Utils", meta = (Keywords = "editor tools"))
static bool IsRunningInEditorPreview(UObject* Object);
};
// Utils.cpp
#include "UUtils.h"
#include "Engine/EngineTypes.h"
bool UUtils::IsRunningInEditorPreview(UObject* Object)
{
if (!Object)
{
return false;
}
if (auto world = Object->GetWorld())
{
return world->WorldType == EWorldType::EditorPreview;
}
return false;
}
For those arriving here looking for a solution, I have one that works well for me. For context, I needed to keep a quit confirmation canvas panel off to the side during development, but got tired of having to move it. I wrote some code to automate that, but since UI/HUD elements are set up in the PreConstruct Event, this was running my code both during gameplay and while editing. To prevent that, I simply added a check to see if the Game Instance was valid and if not, don’t move the canvas into place. Works like a charm! Now the canvas only moves home to 0,0 when the game is actually being played. You could encapsulate the logic (Get Game Instance, CastTo GameInstanceName, IsValid) into a func that would allow this to be used as a single node, but it’s already small enough to use as is I think. Here’s what it looks like. Happy coding!!!
In my case all I needed to do was prevent execution from a Call In Editor event if the game isn’t running, so that my actor isn’t put into a bad state. A simple gate works perfectly, so I wanted to post this in case it’s useful to someone.
This is probably the intended way to go. I needed to check if something was in Editor, in Pie, or in Standlone.
So I made these 2 functions:
.h
// Returns true if this world is any kind of editor world (including editor preview worlds)
UFUNCTION(BlueprintPure, meta = (WorldContext = "WorldContextObject"))
static bool CheckInEditor(UObject* WorldContextObject);
// Returns true if this world is any kind of game world (including PIE worlds)
UFUNCTION(BlueprintPure, meta = (WorldContext = "WorldContextObject"))
static bool CheckInGame(UObject* WorldContextObject);
CheckInGame returns true if at Runtime, either in PIE or Standalone.
CheckInEditor returns true if in the Editor or in PIE.
With these i can check the exact state of my game
For example if I want to know if we’re in the Editor but not in PIE, CheckInGame must be false and CheckInEditor must be true.