Creating a header file with ustructs

I’ve spent a few hours looking up documentation on how to create ustructs in header files, though I’m always faced with some sort of error. 's an example of some code I’ve been using.


#pragma once

#include "GameFramework/Actor.h"
#include "TestActor.generated.h"

USTRUCT()
struct Coordinate //Used as a coordinate, has X/Y value. 
{

	UPROPERTY()
		int32 X, Y;

	int32 GetX()
	{
		return X;
	}

	int32 GetY()
	{
		return Y;
	}

	Coordinate(int32 X, int32 Y)
	{
		this->X = X;
		this->Y = Y;
	}

	//Implement garbage collection later on

};

UCLASS()
class ROGUEPROJ2_API ATestActor : public AActor
{
	GENERATED_BODY()

public:
	ATestActor(); // Sets default values for this actor's properties
	virtual void BeginPlay() override; // Called when the game starts or when spawned
};

I created an Actor and attempted to write in some structs into the header file, but I’m still getting errors. Am I supposed to use another type of base class, such as a component? Furthermore, if I want a separate file containing my structs (which I could probably use #include in other classes), how can I implement it through UE4? All the documentation I’ve seen goes over the code itself, but I’m still unsure as to the specifics from within the editor (such as the base class chosen to build this code upon, where things are saved, etc)

You forgot the USTRUCT macro and prefix “F”.



USTRUCT(/*meta tags*/)
struct FCoordinate 
{
	GENERATED_USTRUCT_BODY()

};

Hope it helps.

Thanks, it appears that the lack of the F and the GENERATED_USTRUCT_BODY() had been causing some issues. However, I’ve run into another error.


USTRUCT()
struct FCoordinate
{
	GENERATED_USTRUCT_BODY()

public:
	int32 X, Y;
	FCoordinate(int32 TestInteger)
	{

	}


};

Without the inclusion of the TestInteger as an argument, the struct is just fine. However, when I try to make a default constructor with arguments, I’m given an error along the lines of “no appropriate default constructor available”. Any idea how I can fix this?

Yes you need to supply it with a default constructor. :slight_smile:
e.g:


USTRUCT()
struct FCoordinate
{
	GENERATED_USTRUCT_BODY()

public:
	int32 X, Y;

        FCoordinate() // Default Constructor.
        {
            x = 0;
            y = 0;
        }

	FCoordinate(int32 TestInteger) // Overloaded Constructor
        {
            x = 0;
            y = TestInteger;
        }
};

Also i recomend not declaring members like that.
You whould probeboly want to have them seperate.

I believe i read somewhere that declaring members like that can cause problems.
And that Epic recommend declaring members separate.