Download

Struct not working

Hi!

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

Where did I get it wrong ?

Pls help :slight_smile:

Remove BlueprintType from GeneratedBody and use


USTRUCT(BlueprintType)

instead.

Same result :frowning:

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.

You could try:


/*
* 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
};

Unreal code standard does not use m<Variablename> when naming variables, just for curiosity:
https://docs.unrealengine.com/en-US/…ard/index.html

Yeah that <filename>.generated.h include is an Unreal-generated file, it defines the “StaticStruct” member that the compiler is complaining about being missing.

I have just created the SpaceShip_stats.h in Visual Studio. Should I have created it through UE4 instead? (if so, then based on what?)

@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.

Works fines then. Hope this might help anyone.

Hi!

That seems to do the trick :slight_smile: created an Actor class. Removed the .cpp file, and changed the class to a struct in the .h file.

Thanks! :slight_smile: