How and where do I create Structures?

Hi,

I was reading about Structures and USTRUCT() and all makes sense but I just couldn’t find it anywhere on where and how to declare them.

  1. Is the structure created in a separate dedicated .h file or can I just create it in any of my .h files?
  2. Can the structure be created inside my class definition (i.e. inside the class itself) or does it have to go on top of it?
  3. In some examples I saw the structure example contain the GENERATED_USTRUCT_BODY() macro, I think this means it was created using the ‘add code to project’ from inside UE4 and then choosing a USTRUCT type but I can’t figure out how to add it from the ‘add code to project’. In other examples the structure example didn’t contain the GENERATED_USTRUCT_BODY() and was just part of an existing class .h file. Is the GENERATED_USTRUCT_BODY() required or not?

So how do I go with creating a simple structure from scratch?

Thanks in advance

Wiki is full of great tutorials. =) A new, community-hosted Unreal Engine Wiki - Announcements - Unreal Engine Forums

You should also have a look at how to serialize your structs into an Archive, an archive is just a file reader or writer steam. More info: A new, community-hosted Unreal Engine Wiki - Announcements - Unreal Engine Forums,Read%26_Write_Any_Data_to_Compressed_Binary_Files#SaveLoadData:_Two-Way_Save_System_Function

Hi TimGS, thanks, yea I’ve seen that wiki link before, it’s great and full of useful info. although I don’t think it answers my questions above.

Moss, thanks for pointing that out!

Ok, I’ll answer them then.

  1. Of course you can put it in separate header file or put it in your class header file. It’s not something UE-specific. It’s C++ basics.
  2. You can use simple c++ structs in your classes like you always do. But UStruct is not like that, its not a struct. UStruct is a basis of UClass, not visa versa. So correct way to use it is to create a UStruct definition in header file (same as UClass or separate) and use it as a member of this type in your UClass.
  3. You’ve got a wrong idea about GENERATED BODY macros. It’s about reflection. Read this: Unreal Property System (Reflection) - Unreal Engine It explains a lot.

Thanks TimGS! Good information. That answers my questions

yes providing the link was no help at all as it didn’t answer any of my questions either, thank you for finally having an answer that seemed to be no where else. +1