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"

get reference to variable in blueprint funcion return?

How in c ++ make function for blueprints which returns a reference to a variable?


     UPROPERTY(EditAnywhere, BlueprintReadWrite)
         int32 MyVar = 0;

     //reference MyVar v1 - dont work
     UFUNCTION(BlueprintPure, BlueprintCallable)
     int32& GetMyVarRef()
         return MyVar;

     //reference MyVar v2 - dont work
     UFUNCTION(BlueprintPure, BlueprintCallable)
     UPARAM(ref)int32& GetMyVarUparam()
         return MyVar;

     //reference MyVar v3 - dont work
     UFUNCTION(BlueprintPure, BlueprintCallable)
         void GetMyVar(int32& RetMyVar)
         RetMyVar= MyVar;
         void AddOne(UPARAM(ref)int32& f)//

alt text alt text

this works for c ++ code but does not work in blueprints

Product Version: UE 4.18
more ▼

asked Jan 23 '18 at 07:49 AM in C++ Programming

avatar image

147 9 16 21

avatar image Jin_VE Jan 23 '18 at 04:24 PM

To be clear: you want to declare a function in C++ that can be implemented in Blueprints, called from C++, and provides a return value?

avatar image Sn_a_ke Jan 23 '18 at 06:23 PM

not just return a value, it must be a reference to a variable, and if you assign a new value to the reference, then the original variable also changes.

similarly as: alt text

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

1 answer: sort voted first

If your answer to my question (above) is "Yes", here's the information you require:



If you want C++ to return a value to blueprints then you just want to use the BlueprintCallable specifier.

Returning a reference is done by using standard C++ syntax (the '&' token). You can search the web for more info on C++ references.

Still shaky on what you want, but if all you want to do is implement a variable in BP and the C++ base class has access to it then you should just use UPROPERTY with the appropriate (i.e. BlueprintXXX) specifier(s). You declare it in C++ and assign it the reference to an object from within the BP.

more ▼

answered Jan 23 '18 at 04:53 PM

avatar image

4.4k 32 79 42

avatar image Sn_a_ke Jan 23 '18 at 07:32 PM

"Returning a reference is done by using standard C++ syntax (the '&' token). You can search the web for more info on C++ references"

Yes it works in c ++ if to call function from with ++:

 //return reference to SomeVariable
 int32& GetMyVariable() 
    return SomeVariable; 

so I'm interested in how I do this function in the Blueprints .. because the function above works as it should in C ++, but does not return the reference in the Blueprints, only the value.

just try the code from the question these functions in the Blueprints and in C ++ work very differently.

I just want to understand if there is such an opportunity. But how and where to apply it is an entirely separate issue.

avatar image Jin_VE Jan 23 '18 at 07:45 PM

Passing primitive types by reference is not best practice. I'm not sure it even works with UE4. You can pass objects by reference but it might not work for primitive types because there's really no compelling reason to do it like that.

See here https://stackoverflow.com/questions/14013139/is-it-counter-productive-to-pass-primitive-types-by-reference

and here https://www.reddit.com/r/unrealengine/comments/47x4ag/compare_references_of_primitive_types/

Also, you have the MyVar property right there to access in the blueprint, so why would you need to return a reference to it?

avatar image Sn_a_ke Jan 23 '18 at 07:59 PM

a lot of things may need to be returned

I just gave a simple example. I agree that use can be controversial. But at the moment I'm just interested in: Is it possible to do this in a simple way or not?

avatar image Jin_VE Jan 23 '18 at 08:20 PM

You can return references to actors and other UE4 objects. But it certainly looks as if you cannot get a reference to a primitive type. That's the best I can do beyond the tutorials, FAQs, and examples that show how to implement UFUNCTIONs.

avatar image Sn_a_ke Jan 24 '18 at 04:48 AM

Thanks for your reply.   If for some time there are no other answers, I'll mark the issue as resolved:

Blueprints do not allow referencing a variable's memory location if the variable is not UCLass USruct or UEnum (I hope I correctly formulated)

(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