How do I create a variable from a struct?

I’m really new to c++ but I do have a lot of experience with python. I’m trying to basically just create a struct, then create and fill a variable with that struct that I can access in Blueprint.

I don’t know if I need to declare the variable beforehand or what, but this is what I have (which is failing to build). The error I’m getting is “Incomplete type is not allowed” for FBaseCharacterStats.

I want to then serialize the data from the struct into a string like so:

// Serialize Base Character Stats to an FString
    UFUNCTION(BlueprintCallable, Category = "Stats")
        FString SerializeBaseCharacterStats();
    // Deserialize an FString to Base Character Stats
    UFUNCTION(BlueprintCallable, Category = "Stats")
        void LoadBaseCharacterStatsFromJSON(FString BaseCharacterStatsJSON);

This is in the Character.h file:

struct FPPBaseCharacterStats
{
	GENERATED_BODY()

public:

		FPPBaseCharacterStats()
	{
		Strength = 5;
		Perception = 5;
		Endurance = 5;
		Charisma = 5;
		Intelligence = 5;
		Agility = 5;
		Luck = 5;
		Willpower = 5;
	}

	UPROPERTY(EditAnywhere, BlueprintReadOnly, Category = "Stats")
		int32 Strength;
	UPROPERTY(EditAnywhere, BlueprintReadOnly, Category = "Stats")
		int32 Perception;
	UPROPERTY(EditAnywhere, BlueprintReadOnly, Category = "Stats")
		int32 Endurance;
	UPROPERTY(EditAnywhere, BlueprintReadOnly, Category = "Stats")
		int32 Charisma;
	UPROPERTY(EditAnywhere, BlueprintReadOnly, Category = "Stats")
		int32 Agility;
	UPROPERTY(EditAnywhere, BlueprintReadOnly, Category = "Stats")
		int32 Intelligence;
	UPROPERTY(EditAnywhere, BlueprintReadOnly, Category = "Stats")
		int32 Luck;
	UPROPERTY(EditAnywhere, BlueprintReadOnly, Category = "Stats")
		int32 Willpower;

// create variable that can be accessed in BP and fill with struct

	UPROPERTY(EditAnywhere, BlueprintReadOnly, Category = "Stats")
		FPPBaseCharacterStats BaseCharacterStats;
};


hope it helps:

USTRUCT(BlueprintType) struct Fchess_cell
{
	GENERATED_BODY()
	UPROPERTY(BlueprintReadWrite)
	int32 Coord_X;

	UPROPERTY(BlueprintReadWrite)
	int32 Coord_Y;

	UPROPERTY(BlueprintReadWrite)
	TEnumAsByte<chess_piece> piece;

	UPROPERTY(BlueprintReadWrite)
	TEnumAsByte<player_color> Player;
};

then

UPROPERTY(BlueprintReadWrite)
Fchess_cell myVariable;

yup, this is the right way.

1 Like

eldany.uy is right.

Also:

That is an error when you forgot its header file or when you made a typo. SHould it be “FPPBaseCharacterStats”?

This is not valid:

Because that would infinitely nest the struct inside itself.

Also the initialization should be written as an initializer list instead of a constructor, because:

stackoverflow

FPPBaseCharacterStats()
{
	Strength = 5;
	Perception = 5;
	Endurance = 5;
	Charisma = 5;
	Intelligence = 5;
	Agility = 5;
	Luck = 5;
	Willpower = 5;
}

// Should be

FPPBaseCharacterStats()
: Strength (5)
, Perception (5)
, Endurance (5)
// Etc...
{
}
1 Like