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