get reference to variable in blueprint funcion return?

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

////test


	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;
	}

	/////
	UFUNCTION(BlueprintCallable)
		void AddOne(UPARAM(ref)int32& f)//
	{
		f+=1;
	}

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

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?

If your answer to my question (above) is “Yes”, here’s the information you require:

Update:

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.

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:

1 Like

“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 ++:

//example
//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.

1 Like

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 c++ - Is it counter-productive to pass primitive types by reference? - Stack Overflow

and here Reddit - Dive into anything

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

And see here Types - Unreal Wiki

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?

1 Like

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.

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)

Not quite exactly. I met the same problem, basically Primative types and Composite types are passed by value by default in Blueprint, but if you provide custom C++ implemention it can still provide reference, see struct break node and struct set members node.

1 Like