how to struct on c++

Let me write example as I understood the subject (all wors fine for me, so… :wink: ):

//myClass.h
//We can declare structs in any .h file (but not .generated.h!)
//that belongs to any existing class. The "F"-prefix is necessary!
//Place struct body just after all #include strings and before class declaration
    
    #pragma once
    
    #include "GameFramework/Actor.h"
    #include "myClass.Generated.h"
    
    USTRUCT()
    struct FMyStruct {
    	
    	GENERATED_USTRUCT_BODY()
    	int Integer;
    	bool Boolean;
    	UPROPERTY()
    	AActor* myActor;
    
    	void MyMethod(){
    
    	}
    };
    
    UCLASS()
    class MY_PROJECT_NAME_API AmyClass :  AActor
    {
    	GENERATED_BODY()
    	//Other .h class stuff here
    };
    
    //Otherwise you can create an empty .h file in Visual Studio,
    //place it in Source folder/any subfolder and name it, for example,
    //MyCustomStructs.h and write any number of structs here.
    //Do not forget to #include this .h file in any .h/.cpp that you wish to use your structs.
    
    #pragma once
    
    USTRUCT()
    struct FMyStruct {
    	
    	GENERATED_USTRUCT_BODY()
    	int Integer;
    	bool Boolean;
    	UPROPERTY()
    	AActor* myActor;
    
    	void MyMethod(){
    
    	}
    };

It is very good material, but it has one weak point - it is not covering where to declare structs for C+±beginners (or for C#-programmers, where the structs can be declared in any separated .cs file as and became available from anywhere). It is a problem for a lot of people and for me it was too before I read some forum posts and your answer. I am only one week in Unreal and C++ just after Unty and C# :wink:

thanks a lot, this actually helps really :slight_smile: so when we want to use these structs we just “include” the .h file to the .cpp, right? but everytime we need to define what it is in the .cpp file. I am not paranoid about whether I get it correct :smiley:

yes, that has exactly been my problem, I cant tell how many times I read that tutorial already

Here is another quick example that uses Initializer lists

USTRUCT()
struct FSomeStruct
{
    GENERATED_BODY() // btw this is all that is needed in 4.8

    UPROPERTY()
    float someFloatValue;
    UPROPERTY()
    FVector someVectorValue;

    // FSomeStruct Default Constructor
    FSomeStruct() 
        : someFloatValue(1.0f), 
          someVectorValue(FVector::ZeroVector)
    {}

    // FSomeStruct Overlaoded Constructor setting one value
    FSomeStruct(float floatValue)
        : someFloatValue(floatValue),
          someVectorValue(FVector::ZeroVector)
    {}

    // FSomeStruct Overloaded Constructor
    FSomeStruct(float floatValue, FVector vector)
        : someFloatValue(floatValue),
          someVectorValue(vector)
    {}
}

This will allow you to create default values for structs and properly initialize the values. This should always be done (IMO) as c++ and c can initialize values to non zero, or non default values, it is just good practice.

You would create the struct (the definition) and then when you create the struct and assign the values you then pass that around (perferably as a Reference)

void AClass::SomeFunction()
{
    // Create the struct
    FSomeStruct struct; // This would create it with default values to all members you will need to manually set the values or you could get undefined behaviour
    struct.ValueOne = value;

    // Create a different struct with constructor values
    FSomeStruct struct2(valueone, valuetwo);

    // Now you have the structs, what do we do with them...
    SomeFucntionToUseTheStruct(struct);
}

void AClass::SomeFunctionToUseTheStruct(const FSomeStruct& struct)
{
    // Use the values in the struct...
}

// Or, what I like to do... is make data objects (this can be done with Datastructs in the editor, but I like to manually set these

USTRUCT()
struct FDataStruct
{
    // could and probably should set these with values here... mah...
    FSomeOtherStruct struct1;
    FSomeOtherStruct struct2;

    FStatStruct()
    {
        struct1 = FSomeOtherStruct(values);
        struct2 = FSomeOtherStruct(differentValues);
    }
}

As mentioned in the data structures in the second example can be done with DataTableRows (DataTables in the editor), so as you can see there are many different uses for Structs…

thanks a lot these are really great examples! I didn’t expect this much help :smiley:
I still have some blank parts like how to use it and all and I am still not sure why I would need this but I will probably see why by time, so thanks a lot

Thank you. This just saved me from a headache I’ve been faced with for a while now while learning C++. The tutorials online are great, but a lot of them don’t mention little things like “Hey idiot, you need to put in a generated.h line in your struct header!”

I was getting errors until I added #include “MyHeaderName.generated.h”

This is super important, should be somewhere top :wink:

i love u. took me forever