Search in
Sort by:

Question Status:

Search help

  • Simple searches use one or more words. Separate the words with spaces (cat dog) to search cat,dog or both. Separate the words with plus signs (cat +dog) to search for items that may contain cat but must contain dog.
  • You can further refine your search on the search results page, where you can search by keywords, author, topic. These can be combined with each other. Examples
    • cat dog --matches anything with cat,dog or both
    • cat +dog --searches for cat +dog where dog is a mandatory term
    • cat -dog -- searches for cat excluding any result containing dog
    • [cats] —will restrict your search to results with topic named "cats"
    • [cats] [dogs] —will restrict your search to results with both topics, "cats", and "dogs"

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()?

Product Version: UE 4.12
more ▼

asked Jul 04 '16 at 05:45 PM in C++ Programming

avatar image

13 2 6 5

(comments are locked)
10|2000 characters needed characters left
Viewable by all users

2 answers: sort voted first

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.

more ▼

answered Jul 05 '16 at 08:33 AM

avatar image

541 15 5 19

avatar image pwalker45 Jul 05 '16 at 09:54 PM

Thanks! That did it!

(comments are locked)
10|2000 characters needed characters left
Viewable by all users

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.

more ▼

answered Jul 04 '16 at 06:24 PM

avatar image

27 8 8 12

(comments are locked)
10|2000 characters needed characters left
Viewable by all users
Your answer
toggle preview:

Up to 5 attachments (including images) can be used with a maximum of 5.2 MB each and 5.2 MB total.

Follow this question

Once you sign in you will be able to subscribe for any updates here

Answers to this question