Download

Simple Struct Question

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.
3 Likes

Writing my first ever CPP blueprint in unreal right now… Kind of wondering the same thing… Glad it’s not just me… LOL

Yes, the structs are bull…

I found a cheat to have my structs appear in the c++ content folder.

Make a c++ class from a simple Object as usual from the IDE and compile it. Then delete the code and replace it with the struct, and its easy to find it from the editor.

Maybe it helps?

I have a question too. What is the best/official way to create an instance of the struct. Im using one to pass data around between Actors, and I have had difficulty. Should I use NewObject on it?

Structs can just be instantiated with their constructor:

FMyStruct StructVar = FMyStruct();

Alternatively you can default initialize them:

FMyStruct StructVar;

For the original poster here is how you can define the default constructor and a constructor for setting all the member variables.

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

class AActor;

USTRUCT(BlueprintType)
struct FCameraChangeData
{
	GENERATED_BODY()

public:

	FCameraChangeData() = default;

	FCameraChangeData(AActor* InCamera, float InBlendTime, float InTimeToNextCameraChange)
		: Camera(InCamera),
		BlendTime(InBlendTime),
		TimeToNextCameraChange(InTimeToNextCameraChange)
	{}

	UPROPERTY(EditAnywhere, BlueprintReadWrite)
	AActor* Camera;

	UPROPERTY(EditAnywhere, BlueprintReadWrite)
	float BlendTime = 0.f;

	UPROPERTY(EditAnywhere, BlueprintReadWrite)
	float TimeToNextCameraChange = 0.f;
};
1 Like