How to create a Structure in a separate file

Hi, I generally make USTRUCT above the class to which they generally related to. But my current structure contains a a lot of smaller structures and I want to place the structure in a separate file. How to I create a separate header file containing a structure?

#Static UObject Library

I use a Static UObject Library to store all my USTRUCTS so they can be used in any class in my entire code base

#My Wiki On Static Function Libraries

#For You

Just put all your USTRUCTS above your class definition and below the #include :slight_smile:

Then include your static library in all your game classes .h files :slight_smile:

#Static USTRUCT Library

Section of tutorial most related to your question

Rama

That does not answer question, it is workaround. I would like to know answer for this as well as Iā€™m facing same situation and I have lot of various data defined through structs and want to move it to separate file as it makes monstrous size fileā€¦ Can you elaborate on original question how to extract them to separate file so it still works? I was not able to make it work without compile errors. it might be good idea in future for your wiki articles to include FULL file along with it supposed name. For newcomers it is hard to figure out what is going on without that. Both linked examples does not answer question. It shows only definition of stuct not how to separate it or move it to separate file so it compilesā€¦ Thank you.

OK I figured it out, exactly what you need (me as well):

  1. Close Unreal Editor if you have it open.
  2. In VS add new file to project, select C++ category and pick Header file.
  3. Move code of your struct to new file.
  4. Add include statement for UHT (Unreal Header Tool, thatā€™s for parsing and generating reflected code) so something like this #include "NameOfYourStruct.generated.h" will work when you save that (.h) file as NameOfYourStruct.h, in header file your struct should start with capital F if you plan to use it with UE code or blueprints as thatā€™s naming convention UE requires. You save it without that capital ā€˜Fā€™.
  5. Save this file into folder of your liking in Source folder of your project and compile. If you got naming convention right UE will recognize it and UHT will generate itā€™s own header as well.
  6. [Optional] In your project folder right click MyProjectName.uproject (Unreal Engine Project File, currently it have blue icon. v4.12.5) and select ā€œGenerate Visual Studio project filesā€ option from that content menu.
  7. [Optional] Open up Visual Studio and now your project should compile without errors as UHT auto-generated all required code ( that NameOfYourStruct.generated.h which you canā€™t obtain any other way) that is normally triggered when you add new C++ class from Unreal Editor.

Hope it helps :slight_smile:

PS: @Rama no offence I like your efforts with WIKI and all as I just migrated 2 weeks ago from Unity knowing nothing about C++ at all :slight_smile: Very appreciated!

2 Likes

About your fourth point:

Do you mean that I should use a ā€œinclude MyFile.generated.hā€ or that I should find the file for the header tool (I still have no actual idea what that is and how to access it and thereā€™s no explanations anywhere)?

Because the former doesnā€™t work. When I tell VS to generate the project files, it just flat-out deletes the .h file that includes my structs.

Edit:

Now it didnā€™t delete my file. But it still claims that the generated.h does not exist.

For some reason Intellitext also does not display the proper behavior in my struct file. It gives out errors for the USTRUCT() macro, for example.

*.generated.h is file that is automatically generated by Unreal Header Tool (UHT) either during manual request to regenerate visual studio files or during compilation. You canā€™t access it normal way and you should not either way. That file contains some extra stuff that enables unreal reflection system to work. Regarding USTRUCT macro check other answers and up to date documentation. Some things changed a bit not long time ago. Look into the wiki. I do not have VS with project at hand so I canā€™t double check. That *.generated.h will be produced by UHT only when you are using macros in your data structures/classes, you can still use plain C++ classes, structs and other datatypes in your program but without adding benefit of unreal reflection system.