Passing 'self' or 'this' from blueprint to C++ function

I need to use the GetWorld() function into my blueprint-called C++ function. Since I chose to use this as a blueprint library (i.e. static functions only), I need to pass an instance of a world actor or object in order to be able to call GetWorld(). I tried something like the following:

UFUNCTION(BlueprintCallable, Category="Something")
static void myCustomFunction(AActor *self);

but I can’t link my self node in the blueprint to the self input of the C++ function.

How can I pass my self instance to the C++ function in order to call GetWorld()?

1 Like

AFAIK, Blueprints cannot be used directly in C++ as types unless they are already derived from a C++ class. I’m fairly certain this includes passing a reference as a C++ parameter.

EDIT: If the blueprint in question is a UMG widget, this is not the case. You need to add some references to your Build.cs, and you will have access to Slate and UMG. If it’s not, then the above should hold true.

If you look at the many blueprint functions in GameplayStatics.h most of the functions need a valid world to do their things, these functions have one thing in common, they have WorldContextObject as its first function parameter and an additional meta: WorldContext=“WorldContextObject”, such as this one:

	UFUNCTION(BlueprintPure, Category="Game", meta=(WorldContext="WorldContextObject", UnsafeDuringActorConstruction="true"))
	static class APlayerController* GetPlayerController(UObject* WorldContextObject, int32 PlayerIndex);

or this:

	UFUNCTION(BlueprintCallable, meta=(WorldContext="WorldContextObject"), Category = "Game")
	static void FlushLevelStreaming(UObject* WorldContextObject);

If you have ever used GetPlayerController in blueprint (you must certainly have) you should have noticed that the WorldContextObject parameter is not there in the input pin of the blueprint node, AND, you should also noticed that GetPlayerController can not be called on Object that does not override GetWorld(). This is how static blueprint functions get their world, and it’s very convenient because Unreal does all the things needed to make sure blueprint user call the functions inside an object that has a valid world.

So, the solution to your problem would be to just change your function declaration to something like this:

 UFUNCTION(BlueprintCallable, Category="Something", meta=(WorldContext="WorldContextObject"))
 static void myCustomFunction(UObject* WorldContextObject);

and you can get the world in your function definition by using GEngine->GetWorldFromContextObject, such as this:

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

Also note that despite all the efforts from UnrealEd module to prevent blueprint user from calling the function from a non-valid non-world-context object, there is a possibility that the function is called from c++ which can be called from anywhere in the code, which means the WorldContextObject is not guaranteed to be valid nor related to a world. So you would want to check the validity of the World that you get from GetWorldFromContextObject function.

If you are making a single player game and you don’t really care about the incoming multi world feature of Unreal Engine, you can get the current world instance using the global variable GWorld, although I really donot recommend this at all.

Thanks! That did it!