Pass blueprint UStruct by reference

Hey guys,

I’m a little stumped here …

My goal is pretty simple: I want certain objects to be ‘grabbable’ by the player. In order to do this, I have created a ‘grabbable’ interface, which implements various methods that tell the player how it should interact with the particular object. All of that works fine, I make use of BlueprintNative events so that I am able to have blueprint and native implementations for each grabbable object. cool.

Now here’s the problem …

To determine where an object can be grabbed, I defined a ‘grab point’ UStruct (FGrabPoint). I would like designers to be able to create arrays of these structs within the blueprints that implement the grabbable interface, and have the interface pass a REFERENCE to these structs back to C++, where I can apply some logic to them (perform complex attachments, or other things best left out of blueprint).

To this end I created a BlueprintNativeEvent in my interface that looks like this:

UFUNCTION(BlueprintNativeEvent,Category=Grabbable)
void GetGrabPoints(TArray<FGrabPoint> &AvailableGrabPoints);

I was able to implement the event in my blueprint class and link the ‘AvailableGrabPoints’ reference parameter up to a grab point member in the blueprint. Everything appeared to be working, until I realized that ‘AvailableGrabPoints’ was not being passed by reference, but by value, with the result that when I got the array into C++, I was working with a copy of the structure data. Not very useful when you want to make changes to a grab point’s properties !

I believe what I need in this situation is actually a TArray of FGrabPoint pointers, but a glance around google has informed me that it is impossible to declare a pointer to a struct if it uses the UPROPERTY specifier. Not very useful for me, since I need my grab points to be modifiable in the editor …

Am I barking up the wrong tree here? It doesn’t seem like this should be as difficult as its proving to be.

I suppose I could use a UObject for my GrabPoints, but this would add additional complexity that I’m not keen on tackling. Are UStructs really just that limited in UE4?

Any help is welcomed! Thanks!

structs are value types. if you want to reference them, you have to reference the object that owns it instead.

why not use an array of Actor pointers? then you can use interface functions to read or write their Grab Point data.

You could make your GrabPoint Array a UPROPERTY and fill this property from blueprint.

Thanks guys, I ended up just kinda working around it ( had to make a copy of the struct and manually copy the changes values across :frowning: )

Oh well!

You can make BlueprintCallable function take a struct by reference by prepending UPARAM(ref) to the type of the parameter declaration, for example:

UFUNCTION(BlueprintCallable)
void ScaleInPlace(UPARAM(ref) FVector& Vector, float Scale);
4 Likes