Creating a struct issue, please help

So I have been searching through the internet on how to create a struct that I could call and edit in blueprints for showing text to screen… This issue is with the struct, after I created it I was given multiple errors. This is all in the same header file, my struct is above my class but under the #include’s.

-FString doesn’t declare for some reason.

-identifier “UnrealTest2_Source_UnrealTest2_PlayerHUD_h_11_GENERATED_BODY” is undefined UnrealTest2,
which is an error on the GENERATED_BODY() inside the struct.

-My class is giving me the error that the declaration has no storage class or type specifier.

-And in both the class and GENERATED_BODY() in the struct

I have no clue how to fix this, I checked multiple tutorials and multiple questions and even found one that was very similar to mine and yet the fix for that person didn’t work for this and I’ve been transitioning from Unity to Unreal for the past week or so. I appreciate any help greatly!
#pragma once

#include "GameFramework/HUD.h"
#include "PlayerHUD.generated.h"

USTRUCT(BlueprintType)
struct UNREALTEST2_API Message
{
	GENERATED_BODY()

public:
	UPROPERTY(VisibleAnywhere)
	FString textMessage;
	UPROPERTY(VisibleAnywhere)
	float time;
	UPROPERTY(VisibleAnywhere)
	FColor color;

	//Default Constructor
	Message()	
	{
		//Set the default time
		time = 5.0f;
		color = FColor::White;
		textMessage = "";
	}

	//Param Constructor
	Message(FString iMessage, float iTime, FColor iColor)	
	{
		textMessage = iMessage;
		time = iTime;
		color = iColor;
	}
};

/**
 * 
 */
UCLASS()
class UNREALTEST2_API APlayerHUD : public AHUD
{
	GENERATED_BODY()
	
public:
	
	UPROPERTY(EditAnywhere, BlueprintReadWrite, Category = "Font")
	UFont* hudFont;
	TArray<Message> messages;
	virtual void DrawHUD() override;

	void AddMessage(Message msg);

};

So I changed GENERATED_BODY() TO GENERATED_USTRUCT_BODY(), still gives me an error but I’m assuming its just VS and should be fine. Thank you for letting me know about the first UPROPERTY not being visible and for the “messages” array. My class is also having the issue(along with another error) of not being built and in the cpp. I can’t reference anything from the header file at all.

cpp.

#include "UnrealTest2.h"
#include "PlayerHUD.h"


void APlayerHUD::DrawHUD()
{
	Super::DrawHUD();

	//iterate from back to front through the list, so if we remove
	//an item while iterating, there won't be any problems
	for (int c = messages.Num() - 1; c >= 0; c--)
	{
		//draw the background box at the right size for the message
		float outputWidth, outputHeight, pad = 10.0f;
		GetTextSize(messages[c].textMessage, outputWidth, outputHeight, hudFont, 1.0f);

		float messageH = outputHeight + 2.0f*pad;
		float x = 0.0f, y = c*messageH;

		//black background
		DrawRect(FLinearColor::Black, x, y, Canvas->SizeX, messageH);

		//draw text using the font selected in BP
		DrawText(messages[c].textMessage, messages[c].color, x + pad, y + pad, hudFont);

		//reduce lifetime by time that has passed since last frame
		messages[c].time -= GetWorld()->GetDeltaSeconds();

		//when the message's time runs out, Remove it.
		if (messages[c].time < 0)
		{
			messages.RemoveAt(c);
		}

	}
}

void APlayerHUD::AddMessage(Message msg)
{
	messages.Add(msg);
}

Within your struct change “GENERATED_BODY()” to “GENERATED_USTRUCT_BODY()”

You’re getting the “no storage class or type specifier” because the struct isn’t being built properly, so it doesn’t recognize the struct type within your class.

The first UPROPERTY in a struct often doesn’t register as valid with VS, but it will compile fine; just ignore the squiggly line. VS has difficulty with many of the UE4 macros and the lack of semicolons.

In general, ignore the “Error” tab when compiling and use the “Output” tab for troubleshooting. Many of the errors generated by VS are misleading or incorrect when working within UE4.

I would also strongly suggest adding a UPROPERTY macro to your “messages” array within your class. If you don’t use the UPROPERTY macro for a variable the UE4 reflection system is unaware of it and can’t track it for garbage collection. You’d have to perform your own memory management and are exposing yourself to memory leaks.

“So I changed GENERATED_BODY() TO GENERATED_USTRUCT_BODY(), still gives me an error but I’m assuming its just VS and should be fine.”

What error are you getting and where? Is it a compile error? Runtime error?

I’m sorry, but I don’t understand what you mean here:

“My class is also having the issue(along with another error) of not being built and in the cpp.”

Going forward, I would suggest deleting the “Binaries”, “Intermediate”, and “Saved” directories within your project folder; then regenerating the project files (Right-Click on .uproject file → Generate Project Files).

Then open your solution in VS and compile after VS has had time to parse your project.

If you’re still having trouble, post the contents of the Output tab and we can go from there.

Pretty much the class isn’t being referenced from the header because its having an error that the class isn’t being built. and it is of course asking for “;”

The first few errors in the cpp. are because it can’t get the class: PlayerHUD
After compiling the error in Unreal is: Struct ‘Message’ is missing a valid Unreal prefix, expecting ‘FMessage’

What’s the text of the error(s)? Are you missing a semi-colon? Where’s it asking for one? Is this happening when you attempt to compile?

Do not use the Error tab; it will mislead you.

Do use the Output tab.

Unreal is telling you it doesn’t like the type name “Message” for your struct. Unreal enforces its class and type prefixes. Structs should be prefixed with “F”, Enums with “E”, UObjects with “U” and actors with “A”.

Change the name of your struct type from “Message” to “FMessage”.

struct UNREALTEST2_API FMessage
1 Like

Yes that worked, thank you! I appreciate your help sooo much!