How to make custom Struct?

Hi, i’m quite new to unreal and having trouble with make my custom Struct with C++.

i knew how to create struct in default C++ programming ( struct name {} thing). but can’t figure out how to create struct on with unreal engine.

i search the doc and it says just there is USTRUCT() thing for setting properties and you can create by following format struct name { GENERATED_STRUCT_BODY() struct variables }.

should i create header file on visual studio and include “CoreMinimal.h” and “objectMacros.h” my self to use USTRUCT to create struct?

some people says you need to create new AActor class on editor, and use that class file to create your struct, but i think that is not the way unreal intended.

i read the wiki too and wiki does not tell how to create files. just show the finished struct code. should i type all the base macros(GENERATED_BODY() thing)?

i really want use C++ as i can possible to create my first game with unreal engine. but unreal just keep make docs about blueprints and it is very hard to learn C++ code way by my self. :confused:

I’ve only found one solution to this, and it’s quite tedious to this every time, but at least it works for me.

Go into the file explorer and navigate to your game directory. Go into the “Source” folder and create a new header file from there. You do not need to create the actual struct right at this point, as long as you just name the header file with the name of your struct.

Go back to the root directory, right click the .uproject, and click on “Generate Visual Studio files”. This will add the file to your visual studio solution. Now you can create your struct and it’ll be included in the editor.

i really appreciate your help :smiley:
but still even after generating work, nothing changed on file and there’s no Generated.h for my struct.h and can’t use USTRUCT with out including headers manually.

is it normal?

What do you mean “there is not generated.h”. That file is automatically generated when you build the project, so you shouldn’t see the actual file anywhere. It’s all handled by the engine.

If you mean that it isn’t in the top of the header file, then yes, that is normal when you do it like this. Just manually add the include for the generated file.

You say that you can’t use the USTRUCT without including headers manually? What headers do you need to include? When I use this method, I only include the .generated.h header, but I can use USTRUCT just fine. :slight_smile:

Just make sure you understand, I’ do not know if this is a “correct” way of creating structs, but it’s the only efficient way I can find, and I’ve been doing it like this always. It works fine enough for me :slight_smile:

sorry i think my reply was not detailed enough

i create new header file named MyStruct.h on visual studio then save and exit and clicked “generate visual studio files”

but after that when i open visual studio, i couldn’t find MyStruct.h file. so i go to the MyProject/Intermediate/ProjectFiles folder to find MyStruct.h and added on visual studio manually.
and since that i thought maybe my work setup is weird so that file was deleted on visual studio.

and couldn’t find MyStruct.generated.h. that’s why i asked should i include needed headers(coreminimal.h and objectMacros.h) manually.

I did also say that you should go into the file explorer :slight_smile: I cannot find a solution that works when adding the files in visual studio, since those files are only “intermediate” files. Open the directory where your game exists, and follow the guidelines I explained in my answers to get the header file into visual studio.

Ahhhhh that’s you’re told me about. i thought the ‘Source’ on Solution explorer XD. i’m really sorry i think i’m too new to unreal and programming

i re created header file on Source folder in project directory (by create new text file named MyStruct.h) than as you told me, generate visual studio project files but still can’t include MyStruct.generated.h file.

so i just create test struct to figure out it is working or not. but it even can’t compile.
here is the code in MyStruct.h
#pragma once

#include "CoreMinimal.h"
#include "ObjectMacros.h"

USTRUCT()
struct FMy_Struct
{
	GENERATED_USTRUCT_BODY()

	UPROPERTY()
		int32 test;

	UPROPERTY(BlueprintReadOnly)
		int32	Test2;
};

Edit : wait i created generated file i think i can figure out the problem

Nono, you don’t need to create the .generated.h. The engine does that automatically but you cannot see it. It just need to be there, but ignore the fact that you cannot see it.

Your struct should look like this:

#pragma once

 #include "MyStruct.generated.h"
 
 USTRUCT()
 struct FMy_Struct
 {
     GENERATED_USTRUCT_BODY()
 
     UPROPERTY()
         int32 test;
 
     UPROPERTY(BlueprintReadOnly)
         int32    Test2;
 };

It shouldn’t be necessary to include CoreMinimal nor ObjectMacros. Try my code and check if it compiles.

no i mean the engine created the generated.h file LOL XD. i’m so excited i find the way to do it.

i tried your code but it didn’t work. because engine doesn’t create generated.h file when i click “generate visual studio project files”. i think there’s some problem to create generated.h file.

but i find a way to make engine to create generated.h file. i’ll post the way to answer my question.
and i really appreciate your help. it helps me a lot.

hope you have great day :smiley:

So with Detilium’s help, i find the way to create struct. i really think it is not the way that unreal intended but, it works for me. and i leave the answer for who need this.

this is the way i did. i didn’t tested if it work without adding on solution explorer or click ‘generate visual studio project files’. so please tell me if that step doesn’t need to it be work.

  1. Go to your project’s directory.
  2. Open Source folder and create header file that you want to (i will assume as Struct.h for this post). than add it on visual studio’s solution explorer so that you can edit on it.
  3. Save and exit (or just save) Visual Studio and right click .uproject file than click the “Generate Visual Studio Project Files”.
  4. Open the Struct.h file you created. than include “CoreMinimal.h” and “ObjectMacros.h”. if you can’t include CoreMinimal, go to other header file that unreal created and find the core header files name :stuck_out_tongue:
  5. Create basic Struct form. ( USTRUCT()struct FStruct{ GENERATED_USTRUCT_BODY() } ) and after that your header file may looks like Code A ( sorry i wanted to put code below fifth step but due to numbering command, i can’t)
  6. compile it than visual studio will pop bunch of errors related Struct.gen.cpp file (which unrea engine generated by self). and it may tells like " Code C2039 / ‘StaticStruct’: not the member of ‘fTest_Struct’ "(it may has some difference since my studio setup isn’t ENG).
  7. Than you can find the generated.h file on “ProjectDirectory/Intermediate/Build/Win64/UE4Editor/Inc/GridProject”. delete the line that you including “ObjectMacros.h” on Struct.h. and replace it as new generated header “Struct.generated.h”.
  8. Now you can compile and use your struct. if you want to see your struct on Unreal Editor, replace USTRUCT() as USTRUCT(BlueprintType).

-------- Code A --------

#pragma once

#include "CoreMinimal.h"
#include "ObjectMacros.h"

USTRUCT()
struct FMy_Struct
{
	GENERATED_USTRUCT_BODY()
};

-------- Finished Code --------

#pragma once

#include "CoreMinimal.h"
#include "Struct.generated.h"

USTRUCT(BlueprintType)
struct FMy_Struct
{
	GENERATED_USTRUCT_BODY()
};

Well, I hope I helped you out :slight_smile:

yeah your answer really helped me :smiley:
due to some weird reason, my way is a bit wasting time but still, i really like this way.
cause when i searched about this problem, people said ‘create new actor, remove all the code than write your struct’ or ‘create struct on project.h than engine create when project created’ as answer.

but i think that way may has some issue due to i guess that header file did not generated for create struct. so i really like this way cause this file is generated for struct.

again, i really appreciate your help and thanks for not giving up for helping me. i think that was a bit annoying.

i read the post on Stack Overflow that people doesn’t like thanks reply. so i really try to avoid to say people to thanks even i really want. but i think i have to say thanks you :).

i up voted your answer but since the way you told was not worked for me and i found the way for me (even your answer really helped me out). i will click V mark on my answer. so i hope that would not bother you.