OpenLevel from c++

Hi,

I try to use OpenLevl in c++ to load my next level but i have an error with the 1st argument a UWorld * on my current level. I dont understand what i need to pass in st argument to my function.

Here is my code :

UGameplayStatics::OpenLevel(thisWorldPtr, FName(*PathToThisProvider), true);

And here is my error :

Assertion failed: WorldContext.World()->GetOutermost()->HasAnyPackageFlags(PKG_PlayInEditor) [File:D:\Build\++UE4+Release-4.12+Compile\Sync\Engine\Source\Runtime\Engine\Private\UnrealEngine.cpp] [Line: 9688] 

Thanks,

As far as I understand the WorldContextObject required is anything on which GetWorld() can be called validly, which would mean any actor that actually exists in the world (mostly entities derived from Actor). E.g. if you’re calling this from an actor that exists in the world you could pass it as the WorldContextObject, e.g.

UGameplayStatics::OpenLevel(this, FName(*PathToThisProvider), true);
1 Like

Hi already try this and i had this error :

UE_LOG(LogLoad, Error, TEXT(“WorldContext requested with invalid context object.”) );

i call OpenLevel in a UButton class

maybe you could try to get the Pawn, that owns your widget and pass that in as the WorldContextObject

Yes, UButton is not a world object so it won’t work. AllJonasNeeds’ suggestion of using the Character should work.

Get the pawn work. Thanks !