How do I create a struct that can be used with blueprint?

Hi, i’m trying to figure out how you would go about in making a struct within a class with c++ that then can be used by a blueprint that inherit from that c++ class.

I have this struct in my class header, can i in some way tag this struct so that it will be visible in the blueprint?
Or if i make a instance of this struct and make that variable visible for the blueprint, will the blueprint be able to use the variables that exist in that struct?

struct ReflectionPoint
{
	FVector Position;
	FRotator RRotation;
};

You need
USTRUCT(BlueprintType)

before your struct declaration and

GENERATED_USTRUCT_BODY()

after the first {

I tried it out making my struct look like this, but i get some errors.

	USTRUCT()
	struct 
	{
		GENERATED_USTRUCT_BODY()
		
		FVector Position;
		
		FRotator Rotation;
	};

The errors i got are “‘’ : is not a class or namespace name” /
“‘’ : undeclared identifier” and some other ones.
Can you see what i’m doing wrong or do you have an example for me?

Did you add an include for the auto generated header .h at the top of your .h file ? I

The shootergame example has several structs - ShooterWeapon.h for example defines FWeaponData

1 Like

Ah, i missed that. Thanks!

You need to add the UPROPERTY annotations to your fields. Like below:

USTRUCT()
struct 
{
GENERATED_USTRUCT_BODY()

UPROPERTY(EditAnywhere, BlueprintReadWrite)
FVector Position;
    
UPROPERTY(EditAnywhere, BlueprintReadWrite)
FRotator Rotation;
};

One extra note, 4.2 will introduce blueprint made struct :slight_smile:

Sorry, but i got the same problem…
I made a custom struct in C++ creating a new .h file directly in VS: the file is MyStruct.h, and its content is

#pragma once

#include "MyStruct.generated.h"

USTRUCT(Blueprintable)
struct FMyStruct
{
	GENERATED_USTRUCT_BODY()
	FMyStruct(float newVar = 0.0f) : myValue(newVar) {}
	FMyStruct() : myValue(0) {}

	UPROPERTY(EditAnywhere, BlueprintReadWrite)
	float myValue;

};

But it still not visible from blueprints in the editor.

Am I missing something or did I do something wrong?

You need to add BlueprintType in order to struct to be used as a blueprint varable type,Blueprintable is actully not needed

Ok, i changed from Blueprintable to BlueprintType, but still nothing.

Just to be (very) clear, the test i’m doing is trying to add a MyStruct variable to an actor blueprint, but the MyStruct type doesn’t appear in the Variable Type drop-down menu in the Details panel of the actor blueprint.

EDIT:
I just realized that creating MyStruct.h directly from VS didn’t create the MyStruct.generated.h file,maybe this is the point?

To create a struct that can be visible as a type to a blueprint you need to add USTRUCT(BlueprintType) to it. Then in the class you need to add GENERATED_BODY() and for every variable that you want to be exposed (able to be changed) in blueprint/editor you need to add UPROPERTY() with blueprintReadWrite to it. To use your example:

USTRUCT(BlueprintType) 
struct ReflectionPoint
 {
GENERATED_BODY()
public:
     UPROPERTY(BlueprintReadWrite, Category="ReflectionPoint")
     FVector Position;
     UPROPERTY(BlueprintReadWrite, Category="ReflectionPoint")
     FRotator RRotation;
 };

You don’t need GENERATED_USTRUCT_BODY() anymore. Have a look at Unreal Engine UStruct Specifiers | Unreal Engine 5.2 Documentation
and Unreal Engine UProperties | Unreal Engine 5.2 Documentation for more info.

Yes :slight_smile: also you need to include this header file somewhere in some cpp file (even if you dont use it there, this can be even empty file) in order for UHT to process it, otherwise UHT won’t touch it at all and struct wont be included

Ok, so sorry for the new noob question, but at this point i think i need a step-by-step guide…

  • From the editor i can “Add New C++ Class”, so for adding custom C++ structs i need to go trough VS, right?

  • Now, where do I have to create the .h and .cpp files? Some specific folders?

  • How can i include the MyStruct.generated.h if it is not auto-created from VS?

  • Do i really need to create the .cpp file, even if my struct doesn’t nedd to implement any method (except constructors)?

  • In the .h file, do i need to include other .h files?

p.s.: sorry again, but I couldn’t find any of these infos in the article USTRUCT etc

Ok, but even if don’t need to create a new .h and .cpp file, i prefer to do so, to keep organized the project files.

You don’t need to add a new .h and .cpp file - you can just add your struct in an existing .h file and you don’t need to add anything in the .cpp if you don’t want to - it is primarily done to structure your code.

Why you not make a seprete quastion for all this? :stuck_out_tongue:

“From the editor i can “Add New C++ Class”, so for adding custom C++ structs i need to go trough VS, right?”

You just create text files, thats it, it does not matter how you do it. Just don’t use VS because it places files in intermediate directory and won’t be compiled

“Now, where do I have to create the .h and .cpp files? Some specific folders?”

In “Source” directory if you have Public Private there, place h files in Public and and cpp in Private, only if you have them

“How can i include the MyStruct.generated.h if it is not auto-created from VS?”

Just include it, like in other files. There is not magic here, generated.h file will be generated by UHT once it process your file

“Do i really need to create the .cpp file, even if my struct doesn’t nedd to implement any method (except constructors)?”

No, you just need to use one of existing cpp files to include this header file so UHT will process it

“In the .h file, do i need to include other .h files?”

No (except XXX.generated.h) and it not recommended to do so, use forward referencing, place “class” or “struct” before types that compiler tell you that are to define, include header file in header files if it’s really needed. Generated header file automaticlly includes core UObject and core engine stuff

Thanks a lot @anonymous_user_f5a50610, that IS an exhaustive answer, you’re great.

The only doubt remaining is: since you tell to not use VS to create files because they will not be compiled, and I can create only class files from the editor, do I have to create a “fake” class file and than delete all its content to write the struct-related code?

I would just like to add to ShadowRivers answer. Just go into your Source folder (from the root directory of your project) and add your .h and .cpp files just as you would create any other file in windows. Then make sure that you right click on your .uproject file (in the root directory of your project) and select “Generate Visual Studie Project Files” or else Visual Studio won’t recognize the new file anyway (If you have Visual Studio open while doing this, just click “Reload All”). This way you don’t need to create a “fake” class.