GEngine - GetWorld() return NULL?

How to get Real World pointer ?
This return NULL >>

UWorld* wp = GEngine->GetWorld();

Every class derived from AActor can use GetWorld() to access the World it is currently spawned in:

including APlayerController, AHUD, AGameMode, AGameState, and so on.

You can call GetWorld() in UGameViewport to get the World it’s related to at the moment, you can call GetWorld() in UGameInstance.

I think it’s enough to not worry about getting it in whatever place you need. I can’t check now, but as I remember GEngine is a static, to also get a GetWorld(), so you are doing it correctly.

The only thing that comes to my mind when considering your problem, is the place you invoke your GetWorld() in. Isn’t it a constructor of some class? Try to do it in BeginPlay() overridden method.

// Fill out your copyright notice in the Description page of Project Settings.

#pragma once

#include "Kismet/BlueprintFunctionLibrary.h"
#include "my_Center_Mouse.generated.h"

class MYPROJECT_API Umy_Center_Mouse : public UBlueprintFunctionLibrary

	UFUNCTION(BlueprintCallable, Category = "111")
	static void my_Center_Mouse();

// Fill out your copyright notice in the Description page of Project Settings.

#include "MyProject.h"
#include "my_Center_Mouse.h"

void Umy_Center_Mouse::my_Center_Mouse()
	UWorld* wp = GEngine->GetWorld();

	if( !wp )
		GEngine->AddOnScreenDebugMessage(-1, 15.0f, FColor::Yellow, TEXT("GEngine->GetWorld() return NULL"));
	APlayerController* pc = GEngine->GetWorld()->GetFirstPlayerController();
	FViewport* vp = pc->GetLocalPlayer()->ViewportClient->Viewport;

	FIntPoint scrRes = GEngine->GameUserSettings->GetScreenResolution();
	vp->SetMouse(scrRes.X / 2, scrRes.Y / 2);
1 Like

Thank You for reply - but how me get this Real world PTR during extending UBlueprintFunctionLibrary

Every UObject has the GetWorld() function, however it is not implemented for all of them since for most it doesn’t make sense. GEngine is a UEngine object, which is a static object that persists from game start to game end. During it’s lifetime multiple different worlds can be loaded, so it can’t return specific world to you. Since you have a blueprint function library you want to use a world context object to get the current world, which can look like this:

UFUNCTION(BlueprintCallable, Category = Misc, meta = (WorldContext = WorldContextObject))
		static bool RandomFunc(UObject * WorldContextObject);


UWorld * World = GEngine->GetWorldFromContextObject(WorldContextObject);

See Fluppi393’s answer.

Well explained, sir.

Okay - then be better use in bluePrints - Get Player Controller( 0 )
which return APlayerController*… from them can execute target code…

I thinked already has Static Function in C++ to do this without BP…
Someone CALLBACK function or something else…

Some information understand - need better investigate UE4…

UGameplayStatics::GetPlayerController also needs a world context object :wink:
If you declare the function like I wrote before, the WorldContextObject will automatically be assigned in blueprints and you don’t see it as a node.

If you want GetWorld() at UObject, Use this code

void UserWidget::TempFunc()
UObject* NewObj = NewObject(this); // use outer as a userwidget