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
Just put all your USTRUCTS above your class definition and below the #include
Then include your static library in all your game classes .h files
#Static USTRUCT Library
Section of tutorial most related to your question
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):
- Close Unreal Editor if you have it open.
- In VS add new file to project, select C++ category and pick Header file.
- Move code of your struct to new file.
- 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’.
- 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.
- [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.
- [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
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 Very appreciated!
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.
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.