How to check if game is running

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?

Hey,

Try to get current world type from UWorld if possible.

https://api.unrealengine.com/INT/API/Runtime/Engine/Engine/UWorld/IsEditorWorld/index.html
https://api.unrealengine.com/INT/API/Runtime/Engine/Engine/UWorld/IsGameWorld/index.html

Lot of other getter is there (like isPlayInEditor), check the doc

Cheers

GEditor->GetEditorWorldContext().World()
should return editorworld i think

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…

Just make your own node.

270112-4346778b2f839f628bc8919291c4525f.png

What does that node do?

Just returns if the game is running in the editor it not.

no answer is right!

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.

One way would be to check if the object returned from GetGameInstance is valid.
It is created once the game starts, one way or another.

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. :wink:

// 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;
}
7 Likes

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!!!

6 Likes

Thanks a lot ^^

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.
Gate using BeginPlay

1 Like

Good tip; thanks for sharing.