UFUNCTION in USTRUCT?

Hello there. I was learning about USTRUCTS() with this tutorlial. Apparently, functions can be placed inside an USTRUCT(); however, if I want to add them an UFUNCTION(), the compiller tells me that it can’t be done.

How can I implement a function inside an USTRUCT so it can be called by blueprints?

Thanks in advance.

You can call a function inside a struct by first accessing that struct and then calling it. Something like this you can do:

// header file
USTRUCT()
struct FMyStruct
{
	GENERATED_USTRUCT_BODY()
 
	UPROPERTY()
	int32 SampleInt32;
    
    void SetInt(const int32 NewValue)
	{
		SampleInt32 = NewValue;
	} 
    
    int32 GetInt()
	{
		return SampleInt32;
	}
 
	//Constructor
	FMyStruct()
	{
		SampleInt32 	= 5;
	}
};

FMyStruct MyStruct;

UFUNCTION(BlueprintCallable, Category = "My Category")
void SetInteger(const int32 NewInteger);

UFUNCTION(BlueprintPure, Category = "My Category")
int32 GetInteger() const;

//source file
void MyClass::SetInteger(const int32 NewInteger)
{
    MyStruct.SetInt(NewInteger);
} 

int32 MyClass::GetInteger() const
{
    return MyStruct.GetInt();
}

That works, but I was wondering if there was a ‘direct’ way in which I don’t need to write all functions name twice.

Reflection system does not support functions in struct, Even thru in C++ class and structs are practically have same capabilities as class, UE4 conventions limits structs to only contain data structures… actually giving structs more sense of existence. Ofcorse non-reflected function in structs will normally work in C++, in fact you can find examples of that in UE4, most notably FVector and FRotator

Common practice in UE4 in case of blueprint node can’t be contained in type that it is operating (exacly like FVector) is to create static functions is some class. You can use const FSomeStruct& argument type to make input pin that modifies inputed structure directly

2 Likes

NOTE:

in 4.11+, GENERATED_BODY() should be used instead of GENERATED_USTRUCT_BODY()
1 Like

Even thru[SIC] in C++ class and structs are practically have same capabilities

This is a bit misleading. Structs and classes (not to be confused with UStructs and UClasses) have EXACTLY the same capabilities in C++. The ONLY functional difference is that structs default to “public” and classes default to “private” for members. Both support all the same functionality as the other, just you may have to explicitly use a public or private specifier first, depending on what you want to do.

wrong. because USTRUCT != struct.
u may or not may be able to add C++ functions to a USTRUCT, but definitely not UFUNCTIONs.
as dr. has pointed out, USTRUCT is exclusively a data structure when it comes to the reflection system, and resembles more of a C struct rather than C++ struct on UE4 level.

Then i might misslead you because i was refering to C++ classes exacly, i fixed original anwser

It’s actually not wrong at all. You obviously misunderstood what I was referring to. I’ve edited my original comment to make this more clear.

This quote:

Even thru[SIC] in C++ class and structs are practically have same capabilities

They aren’t “practically” the same in capabilities. They are EXACTLY the same in what they are capable of.

Everything else is correct about this answer except that.

Unless of course he’s talking about UStructs and UClasses, which I believe is how you interpreted it, in which case the answer should probably be edited to clarify that.

Based on how he followed that statement, I’m just about 100% confident he was referring to standard C++ structs and classes, though, since he goes on to talk about how UE4 changes their functionality to be more limiting, and giving an actually functional difference between structs and classes.

1 Like