Hi Raxis,
Okay, I did some digging about this and found a few threads which are relevent there.
The first one: How do I expose a USTRUCT by reference/pointer? - Editor Scripting - Epic Developer Community Forums
Indicates that you can’t really store exposed USTRUCT references in your BP.
UE4 treats struct references as copies basically, and even if you have a C++ function which returns a struct my reference, I believe UE4 will still create a copy before giving it to you in your BP. The thread relevant to that issue is: Pass blueprint UStruct by reference - Programming & Scripting - Epic Developer Community Forums
I did some experimenting and found a workaround to this issue. This solution isn’t as elegant as just storing a reference to a struct and modifying the values, which apparently UE4 doesn’t like, but it gets the job done without requiring you to create dummy structs and manually copy each element over…
Workaround:
Basically, in the C++ class that has the struct pointer, you can define a getter and a setter method to obtain the struct for your blueprints. Something like
In my class header file, my class has a reference to a USTRUCT of type “Frar” which I call “myRar”
... class definition in header ...
{
GENERATED_BODY()
public:
// Note i don't use UPROPERTY here! It will fail to compile if I do.
// Just assumy "myRar" is set up somewhere, using a USTRUCT and the members have
// UPROPERTY defined on them appropriately...
Frar* myRar;
... rest of class member definitions ...
//
// The following are my getters and setters (by value! to access the myRar
// struct I'm holding
//
// Initializes "myRar" with a new operator
UFUNCTION(BlueprintCallable)
void CreateMyRar();
// Gets "myRar" value. Note that even though this returns by reference,
// UE4 seems to treat it as return by value
UFUNCTION(BlueprintCallable)
void GetMyRar(Frar & getVal);
// Sets the "myRar" value by copying over everything from the input "setVal" struct
UFUNCTION(BlueprintCallable)
void SetMyRar(const Frar & setVal);
In the class CPP file, implement the getters and setters (which just copy values and return copy of struct, even though inputs/outputs are references):
void URayBox::CreateMyRar()
{
myRar = new Frar;
}
void URayBox::GetMyRar(Frar & getVal)
{
getVal = *myRar;
}
void URayBox::SetMyRar(const Frar & setVal)
{
*myRar = setVal;
}
With these getters and setters set up, you can access the “myRar” reference from the blueprint.
Basically what the blueprint is doing is this:
BeginPlay: Initialize my “myRar” reference (note you can initialize this however you want…I just chose to make a custom function for this for demonstration). Next, it’s calling “GetMyRar” which returns myRar by value . Next I’m calling “Set members in …” function to set the members in the struct by value. When I did this, after adding the “Set members in” node, I had to right click on it and click “Restore all structure pins” for the struct’s members to show up. Next, I’m calling “Set My Rar” which will copy the updated rar struct to my “myRar” reference in the class.
In the “PlayerCollision” event: I’m calling “Get My Rar” function, then calling "Break " to access the individual members of the copy returned by “Get My Rar”. Then I add the two members together (arbitrary operation), and call “Set members in …” again. Note this time, since I didn’t want all of the member pins exposed (since I don’t want to set the value of “dummyVal”) I had to right click on each pin I didn’t want, and clicked “Remove this struct variable pin”. that way the “dummyVal” isn’t updated and reatins whatever value it has. Then I call “Set My Rar” again to copy to my classes “myRar” reference.