Download

Sweet LORD, why is writing a simple Struct such a Hellscape?!

I am fairly new to both Unreal and C++.
I have only dabbled in both.
I’ve started going through Unreal’s programming tutorials…

Anyway, I am trying to make a struct, and simple struct… It’s not a complex concept. It’s a struct.
Most forum threads crying woefully over structs seem to point to this page at some point, which I am using for reference.

However I write it, I get an Error.

Here is the struct,
It is declared in a “None” C++ class created via Unreal.


#pragma once

#include "CoreMinimal.h"
#include "Engine.h"


USTRUCT([Atomic])
struct PRACTICEPROJECT_API FCameraChangeData
{
    GENERATED_BODY()

public:

    UPROPERTY(EditAnywhere)
    AActor* Camera;

    UPROPERTY(EditAnywhere)
    float BlendTime;

    UPROPERTY(EditAnywhere)
    float TimeToNextCameraChange;

    FCameraChangeData()
    {
        Camera = NULL;
        BlendTime = 0.0f;
        TimeToNextCameraChange = 0.0f;
    }
};

The above gets*(Line 9 - USTRUCT([Atomic]))*** Error: Missing ‘,’ in Struct declaration specifier**

replacing


USTRUCT([Atomic])

with


USTRUCT(Atomic)

returns **error C4430: missing type specifier (etc…) **so that is obviously a bad idea.

Using


GENERATED_USTRUCT_BODY()

as instructed in the Unreal Documentation returns **Error: Missing ‘,’ in Struct declaration specifier **also

which is kinda why I have mostly been using


GENERATED_BODY()

So, I thought I’ll just comment out the GENERATED_BODY() and Unreal gives me:

Error: Expected a GENERATED_BODY() at the start of struct

:expressionless: :expressionless: :expressionless:

All Errors seem to point to my Specifiers (which I have also tested between the brackets of the GENERATED_BODY() line) but at my GENERATED_BODY() line

I have googled wildly trying to see how other people write their structs, to see what I am doing wrong but to no avail.

Can any better person out there help me?

Never seen a struct using the Atomic USTRUCT macro :expressionless:

Besides that part, struct looks fine at first glance.

Fixed up all the things I saw:



#pragma once
#include "CoreMinimal.h"
#include "YourFileName.generated.h"

class AActor;

USTRUCT(Atomic)
struct PRACTICEPROJECT_API FCameraChangeData
{
    GENERATED_BODY()

public:
    UPROPERTY(EditAnywhere)
    AActor* Camera = nullptr;

    UPROPERTY(EditAnywhere)
    float BlendTime = 0.f;

    UPROPERTY(EditAnywhere)
    float TimeToNextCameraChange = 0.f;
};


  • Don’t include the monolithic Engine.h file. Forward declare the things you need in headers instead, and include individual files in cpp files. Improves your compile times.
  • Missing generated.h include. That’s why you’re getting errors from the UHT macros.
  • ] is not allowed inside UHT macro. Atomic is a valid specifier, but rarely used. Are you sure you need it?
  • No need to declare custom constructor when in-class initialization works fine.