Download

USTRUCT declaration

Hi guys,
I want to declare this USTRUCT in my new Actor C++ Class.


USTRUCT(BlueprintType)
struct FRoom
{
	GENERATED_USTRUCT_BODY()

	UPROPERTY(EditAnywhere, BlueprintReadWrite)
		int32 roomNumber;
	UPROPERTY(EditAnywhere, BlueprintReadWrite)
		int32 roomType;
	UPROPERTY(EditAnywhere, BlueprintReadWrite)
		int32 startX;
	UPROPERTY(EditAnywhere, BlueprintReadWrite)
		int32 startY;
	UPROPERTY(EditAnywhere, BlueprintReadWrite)
		int32 width;
	UPROPERTY(EditAnywhere, BlueprintReadWrite)
		int32 length;
};


As soon as I put it in my header like this:


// Fill out your copyright notice in the Description page of Project Settings.

#pragma once

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

USTRUCT(BlueprintType)
struct FRoom
{
	GENERATED_USTRUCT_BODY()

	UPROPERTY(EditAnywhere, BlueprintReadWrite)
		int32 roomNumber;
	UPROPERTY(EditAnywhere, BlueprintReadWrite)
		int32 roomType;
	UPROPERTY(EditAnywhere, BlueprintReadWrite)
		int32 startX;
	UPROPERTY(EditAnywhere, BlueprintReadWrite)
		int32 startY;
	UPROPERTY(EditAnywhere, BlueprintReadWrite)
		int32 width;
	UPROPERTY(EditAnywhere, BlueprintReadWrite)
		int32 length;
};

UCLASS()
class TEST1_API AMyActor : public AActor
{
	GENERATED_BODY()
	
public:	
	// Sets default values for this actor's properties
	AMyActor();

	// Called when the game starts or when spawned
	virtual void BeginPlay() override;
	
	// Called every frame
	virtual void Tick( float DeltaSeconds ) override;
	
};


Visual Studio fires different errors, all seemingly caused by this line: #include "MyActor.generated.h"
How do I fix this?

Searching around I understood that USTRUCTs are giving hell to a lot of people starting with UE4’s C++, but I couldn’t find any clear answer.

Any help would be appreciated!
Thanks.

Have you try to compile even if Visual Studio highlighted those lines ? If compile fail, give us output log :slight_smile:

And declare an empty constructor for your struct like this:


FRoom(){}

You need it especially for replication ^^

I hope this help you ! :slight_smile:

When compiling gives Error …]MyActor.h(10) : struct: ‘Room’ conflicts with class name. I tried changing name to FRoomRoom for example and it works, very strange.

Anyway, on a side note, where should I declare, and what is the purpose of declaring the empty constructor you suggested?

It’s normal, you got an ARoom actor ? And for the empty constructor, it is a good practise to initialize default values of your variables, and you need this constructor if you want to replicate your struct (Send data to client or server).

You can declare it like this:




USTRUCT(BlueprintType)
struct FDataRoom
{
	GENERATED_USTRUCT_BODY()

	UPROPERTY(EditAnywhere, BlueprintReadWrite)
		int32 roomNumber;
	UPROPERTY(EditAnywhere, BlueprintReadWrite)
		int32 roomType;
	UPROPERTY(EditAnywhere, BlueprintReadWrite)
		int32 startX;
	UPROPERTY(EditAnywhere, BlueprintReadWrite)
		int32 startY;
	UPROPERTY(EditAnywhere, BlueprintReadWrite)
		int32 width;
	UPROPERTY(EditAnywhere, BlueprintReadWrite)
		int32 length;

        FDataRoom(){}
};


Edit: You can use Enum for your RootType variable instead of a int32 :wink:

I noticed that I have a c++ class that has a struct with the same name, could it be that they are conflicting?

And by the way thanks for teaching me the best practices. The replication thing is not yet totally clear to me… Is client-server communication the only scenario where this would be needed?

Yes you can have a name conflict with struct name and also with object and struct, like URoom and FRoom will generate a name conflict, so be careful ^^

For replication, yes I guess it is the only one and if you need to specify default variable of your struct.

Well, good to know then!
Thank you very much Jackblue.