Custom USTRUCTS in Blueprints, How Access member functions in BP?

Dear Friends at Epic,

I’ve created a USTRUCT that I can successfully create in a Blueprint!

USTRUCT(BlueprintType)
struct FJoyInventoryItem
{
GENERATED_USTRUCT_BODY()

UPROPERTY()
int32 Type;

UPROPERTY()
int32 NumUses;

UPROPERTY()
int32 SpecialCharacteristics;

void SetType(const int32& NewType)
{
	Type = NewType;
}

FJoyInventoryItem()
{
	Type = 0;
	SpecialCharacteristics = 0;
	NumUses = 1;
}

};

How do I call its member functions from a blueprint graph?

Adding UFUNCTION(BlueprintCallable) threw a compile error :slight_smile:

#Last Resort

Currently I must create a series of static accessors using a Blueprint Function Library

Any chance of support for accessing a custom BlueprintType USTRUCT’s members more easily?

Rama

Hey Rama,

As far as I am aware, and as you concluded, there is no direct way to place a UFUNCTION on a USTRUCT.

I believe they made this decision because they treat structs inside of a blueprint as simple data containers (kind of like POD, but a bit different). They are meant to be passed and stored purely by value (for example, you cannot create a USTRUCT* UPROPERTY). Because of this, they are not garbage collected.

This allows USTRUCT’s to be very efficient and cheap to work with, as they are not forced to inherit UObject like a UCLASS is.

Unfortunately, it also means they can’t contain UFUNCTION’s, because they are not compatible with delegates.

Think of it this way. Any UFUNCTION can be stored within a delegate. The delegate requires a pointer to both the function definition and the target object in order to work. The function definition does not change location in memory, and can therefore be statically referenced (meaning it does not need any garbage collection or ‘safe pointer’ support). The target object, however, is much less reliable. It could be deleted at any time, leading to memory corruption if the delegate was executed and not properly checked. Being a USTRUCT, the target object would not work with ‘safe pointer’ classes, and could not be garbage collected.

To bring this back to your specific question, K2Nodes (such as K2_CallFunction) use a system similar to delegates (thought to be fair, a bit different). They rely on UObjects, both to store ‘target objects’ and to trawl their UClass for the function definition. This will not work with USTRUCT’s.

Now I could be wrong. They could potentially do some magic behind the scenes that could make it work (such as bypassing the need for ‘delegates’ when linking up a blueprint graph). They could hard-link the methods in their pre-processing build tool. I don’t have access to their engine code after all, so I am just making an educated guess. I would very much doubt it, however, and their exposed classes seem to support my deduction. The additional complexity and loss of flexibility in other areas would do more harm than good. This may well change in the future, but for now I think we have to rely on blueprint function libraries.

1 Like

Thanks for the extensive and insightful comment Andrew!

Epic seems to have custom structs on the way in a future update, but I was hoping there was a work around for the present :slight_smile:

But thanks again for the info!

Any commentary Epic?

They are? Have you got a link to that? It would be an interesting read.

I would assume if they’re putting in custom struct support, it would mean creating and accessing structs from a blueprint (pretty much the same as writing one in code). I doubt it would provide method authoring or access.

You could use a function in the class scope declared UFunc to access the struct member, you need it to have the correct param types but it’ll let you access a struct member the round abouts way.

Yeah, that’s about the only way to do it at the moment. A helper UFUNCTION inside a UCLASS to access the USTRUCT for you.

Yes, that is what I’m doing right now. Though I rarely give function direct access to simply pull data out of struct. Everything in struct is public anyway, and can be simply access trough Struct.Property.

In class struct property is usually defined as UPROPERTY as well.

In blueprint you can always use Break Struct or Construct Struct (or something like that ;p).

Prolly exposing most of it as public is not that good idea, but it is convenient to work with blueprints.

Hey guys!

Custom structs are indeed coming. We were hoping to make the next release, but we had to punt last minute because of one last bug that we couldn’t get resolved in time for the build.

However, when they are in, you will be able to define your own structs in the blueprint editor itself, or as their own assets, just like you can with enums. They’ll work the same way, and be legit blueprint types, so you can use them across multiple blueprints, as function parameters, and all that fun stuff.

With the next change though, you’ll also get generic functions for making and breaking a struct to access its members. When you drag off the struct, you’ll be given an option to lay down a pure “break” node, which will let you access the members, which you can recombine as a “make” struct node. There’s also plans for making a node to modify just specific members of a struct without having to break and make.

As for UFunctions, as was stated above, we aren’t planning on supporting UFunctions on the struct calling scope. UFunctions themselves will still have to be class members.

Thank for the detailed update Nick!

" in time for the build."

ooooh!

Yaaay!

Next build is coming!!!

Rama