I am trying to include a struct to my code, but gets some compile errors that I dont understand:
The code:
/*
* Spaceship_Stats.h
* A container for information regarding the ship
*/
#pragma once
USTRUCT()
struct FSpaceship_Stats
{
GENERATED_BODY(BlueprintType)
UPROPERTY()
FString mName; // Name of the ship
UPROPERTY()
float mMaxSpeed; // Maximum speed of the ship
UPROPERTY()
float mAccelration; // Accelration const for the ship
UPROPERTY()
int32 mMaxCargoWeight; // Maximum weight of the cargo
UPROPERTY()
int32 mMaxWeapons; // Maximum number of weapons
};
When added to the project, I get the following errors:
1> [1/5] Spaceship_Stats.gen.cpp
1>D:\Unreal Projects\GalaxyTrader\Sandbox\SpaceShip\Source\SpaceShip/Spaceship/Spaceship_Stats.h(11): error C4430: missing type specifier - int assumed. Note: C++ does not support default-int
1>D:\Unreal Projects\GalaxyTrader\Sandbox\SpaceShip\Intermediate\Build\Win64\UE4Editor\Inc\SpaceShip\Spaceship_Stats.gen.cpp(19): error C2039: ‘StaticStruct’: is not a member of ‘FSpaceship_Stats’
1> D:\Unreal Projects\GalaxyTrader\Sandbox\SpaceShip\Source\SpaceShip/Spaceship/Spaceship_Stats.h(9): note: see declaration of ‘FSpaceship_Stats’
1>D:\Unreal Projects\GalaxyTrader\Sandbox\SpaceShip\Intermediate\Build\Win64\UE4Editor\Inc\SpaceShip\Spaceship_Stats.gen.cpp(31): error C2039: ‘StaticStruct’: is not a member of ‘FSpaceship_Stats’
1> D:\Unreal Projects\GalaxyTrader\Sandbox\SpaceShip\Source\SpaceShip/Spaceship/Spaceship_Stats.h(9): note: see declaration of ‘FSpaceship_Stats’
1>D:\Unreal Projects\GalaxyTrader\Sandbox\SpaceShip\Intermediate\Build\Win64\UE4Editor\Inc\SpaceShip\Spaceship_Stats.gen.cpp(33): error C2039: ‘StaticStruct’: is not a member of ‘FSpaceship_Stats’
1> D:\Unreal Projects\GalaxyTrader\Sandbox\SpaceShip\Source\SpaceShip/Spaceship/Spaceship_Stats.h(9): note: see declaration of ‘FSpaceship_Stats’
1> [2/5] SpaceShip.init.gen.cpp
I moved the code to another .h file, which was created from within UE4, based on a pawn class. That solved it.
But it really would have it in its own .h file.
/*
* Spaceship_Stats.h
* A container for information regarding the ship
*/
#pragma once
#include "CoreMinimal.h"
**// File name before .generated.h**
**#include "Spaceship_Stats.generated.h"**
USTRUCT(BlueprintType)
struct FSpaceship_Stats
{
GENERATED_BODY()
UPROPERTY()
FString mName; // Name of the ship
UPROPERTY()
float mMaxSpeed; // Maximum speed of the ship
UPROPERTY()
float mAccelration; // Accelration const for the ship
UPROPERTY()
int32 mMaxCargoWeight; // Maximum weight of the cargo
UPROPERTY()
int32 mMaxWeapons; // Maximum number of weapons
};
Yeah that <filename>.generated.h include is an Unreal-generated file, it defines the “StaticStruct” member that the compiler is complaining about being missing.
@tannic1973 when I create structs, I create them in Visual Studio instead of Unreal. I usually just use the .h file and as you said, there is no option to create a C++ struct in Unreal.
Unreal’s build process creates that generated.h file when it comes across USTRUCT/UCLASS/UENUM etc declarations in headers. There’s nothing special about C++ classes created from the editor, it’s just giving you a template to work from. You don’t need to tell Unreal about your header file beyond just #including it in a cpp file in your project somewhere.
I’ve always struggled to create structs directly in Visual studio, always throws a bunch of errors about nothing in particular.
These are my steps:
Create a new c++ class in the UE4 editor (normally an actor but it doesn’t matter.)
Then just change everything in the header file (except the generated.h include) and deleted the cpp file.
This is a convoluted error that happens when you introduce custom structs with other custom structs. I don’t think it’s a user specific struct though, and this just happened to me yesterday. If you import another library struct for example a gameplayTag, and then include this struct in an actorComponent or UActorComponent, unreal’s going to throw that confusing compile error while giving you no clear reason why it’s throwing the error. Be careful about what you include in your structs, the compiler is struct for it’s blueprint types, and it will break in some of these scenarios