how to struct on c++

I honestly don’t understand why there’s almost no proper explanation about this, I am new to coding on the engine. So far I got that structs help me organize custom variable data and that it basically looks like this:

USTRUCT()
struct StructName
{
GENERATED_USTRUCT_BODY()
};

I looked at the wiki already, what is this? how do I use it, where do I put it? why am I even doing it? Do people don’t use it at all?

How exactly do I add it to headers?

you can find the answer in the comments under the answer

Yes, structs are used. What you wrote is literally all that is needed, just put it in a header file. Here is a tutorial link.

structs

okay I will work on that, thank you for your answer

just one thing though, whatever I do, GENERATED_USTRUCT_BODY() gives ‘unidentified’ exception, what do I need to do for that?

Here’s an example from the engine.

USTRUCT(BlueprintType)
struct FAnalyticsEventAttr
{
	GENERATED_USTRUCT_BODY();

	UPROPERTY(EditAnywhere, BlueprintReadWrite, Category="Analytics")
	FString Name;

	UPROPERTY(EditAnywhere, BlueprintReadWrite, Category="Analytics")
	FString Value;
};

okay but I keep receiving the following error about GENERATED_USTRUCT_BODY();

“This declaration has no storage class or type specifier”

Why does this happen? Sorry for the inconvenience.

What is the whole .h file that this is in?

I copied and pasted what you sent to an empty .h file, or copied it to the .h file of an actor, I suppose I am doing it wrong, should I be ‘including’ something for it?

What data do you need in your struct?

That snippet is already in the engine.

Well I was actually trying to do this tutorial https://docs.unrealengine.com/latest/INT/Programming/Tutorials/AutoCamera/4/index.html
I have cameras and I am to use struct instead of actor

Turns out I just needed to ignore the warning

Do I must create my structrs inside existing .h files (i.e. my classes where those structs are used) or there are some special place to store structs? Where I must place structs which I wan to use for multiple classes?

How did you organised your work with structure data? I got so many errors when trying to access AActor stored in struct from .cpp file.

I honestly dont know the answer to that, share it with me if you find out please.

You can place structs in existing .h files, but remember the need to be declared before you use them, or what I like to do for organization, is create a new .h , header files can exist without code (.cpp) files. The .h file will only hold the structs. You can create a new .h file in VS by right clicking on a filter (folder) in the solution, Add New Item (change the directory to the appropriate source folder, I find it easier to do this instead of copying from intermedate folder) and name it (just remember to select the Header (.h) option). The build tool will pick up on the files and include them in the build.

You will need to use #pragma once and still include the filename.generated.h, then write all your structs, then just include the header file where you need it.

thanks for that great info, so is the actualy job of structs holding in some variables or sort of functions that can be used in different situations? is there anything more to it?

You might want to read this before I start to just repeat everything:

USTRUCTS are Awesome

Thanks a lot for such nice info! I already dig it by myself in different places by small pieces, but you’ve gived us a very helpful and fullfilled compilation!

The structs may (and must) be used everywhere you need to store a typical data collections and work with each of them as with solid object. Also your structs can contain differend constructors and collection of methods to work with struct data, which can greatly increase coding speed and help you to beautefully organise your codespace. The easiest and best example of structs are the FVector, that contain 3 float variables and a lot of methods to change it or translate to something else.

that’s where I am troubled, I mean we put the variables with the struct inside an .h file but we define them inside a .cpp right? so how could I even use them in another cpp?