USTRUCT() and UCLASS() in same file

Hey guys, so I’m trying to define and declare a USTRUCT() inside a file that contains a UCLASS() and that UCLASS() will use the USTRUCT()

Like this:


// Copyright 2015 Dirt Productions. All rights reserved.

#pragma once

#include "GameFramework/SaveGame.h"
#include "DHSaveSettings.generated.h"

/** FDHGameplaySettings
 * Declared: DHSaveSettings.h
 *
 * Gameplay settings
 */
USTRUCT(BlueprintType)
struct FDHGameplaySettings
{
	GENERATED_USTRUCT_BODY();

public:
	/** Default constructor */
	FDHGameplaySettings() {}

	/* ///////////////////////// VARIABLES ///////////////////////// */
public:
	/* ////////// VIEW CONTROL OPTIONS ////////// */

	/** True to invert yaw input, otherwise false */
	UPROPERTY(EditAnywhere, BlueprintReadWrite)
		bool bInvertYaw;

	/** Intensity of yaw input as modifier */
	UPROPERTY(EditAnywhere, BlueprintReadWrite)
		float YawInputModifier;

	/** True to invert pitch input, otherwise false */
	UPROPERTY(EditAnywhere, BlueprintReadWrite)
		bool bInvertPitch;

	/** Intensity of pitch input as modifier */
	UPROPERTY(EditAnywhere, BlueprintReadWrite)
		float PitchInputModifier;
};

/** UDHSaveSettings : USaveGame
 * Declared: DHSaveSettings.h
 *
 * Saved user settings
 */
UCLASS()
class DISTANTHOME_API UDHSaveSettings : public USaveGame
{
	GENERATED_BODY()
	
public:
	/** Default constructor */
	UDHSaveSettings(const FObjectInitializer& ObjectInitializer);
	
	/* ///////////////////////// VARIABLES ///////////////////////// */
public:
	/* ////////// SETTINGS ////////// */

	/** Gameplay settings */
	UPROPERTY(EditAnywhere, BlueprintReadWrite, Category = "Settings")
		FDHGameplaySettings GameplaySettings;
};

However, this does not work, and compiler keeps spitting out errors. Is it possible to define & declare a USTRUCT() inside a file that contains a UCLASS() and have that UCLASS() use the USTRUCT()?

Attempted solutions:

  • Forward declare the UCLASS() and define it after struct body - UCLASS() seems to require body at declaration
  • Put UCLASS() under USTRUCT() - Compiler errors

Yes. I’ve done that few dozens of times already.

Post the list of errors you get.

Try remove the semi-colon from GENERATED_USTRUCT_BODY()

A list of errors would indeed be helpfull

Wierd… No errors this time; I’ll reply once I get them again :smiley:

No errors this time, read my other reply

Got 'em again


1>------ Rebuild All started: Project: DistantHome, Configuration: Development_Editor x64 ------
1>  Cleaning DistantHomeEditor Binaries...
1>  Compiling game modules for hot reload
1>  Compiling game modules for hot reload
1>  Parsing headers for DistantHomeEditor
1>  D:/Library Storage/Unreal Projects/DistantHome/Source/DistantHome/Public/Gameplay/Settings/DHSaveSettings.h(71) : LogWindows:Error: Windows GetLastError: The operation completed successfully. (0)
1>Error : Failed to generate code for DistantHomeEditor - error code: CrashOrAssert (3)
1>  UnrealHeaderTool failed for target 'DistantHomeEditor' (platform: Win64, module info: D:\Library Storage\Unreal Projects\DistantHome\Intermediate\Build\Win64\DistantHomeEditor\Development\UnrealHeaderTool.manifest).
1>C:\Program Files (x86)\MSBuild\Microsoft.Cpp\v4.0\V120\Microsoft.MakeFile.Targets(43,5): error MSB3073: The command ""C:\Program Files\Epic Games\4.8\Engine\Build\BatchFiles\Rebuild.bat" DistantHomeEditor Win64 Development "D:\Library Storage\Unreal Projects\DistantHome\DistantHome.uproject" -rocket" exited with code 3.
========== Rebuild All: 0 succeeded, 1 failed, 0 skipped ==========


Here’s the file:


// Copyright 2015 Dirt Productions. All rights reserved.

#pragma once

#include "GameFramework/SaveGame.h"
#include "DHSaveSettings.generated.h"

/** FDHGameplaySettings
 * Declared: DHSaveSettings.h
 *
 * Gameplay settings
 */
USTRUCT(BlueprintType)
struct FDHGameplaySettings
{
	GENERATED_USTRUCT_BODY();

public:
	/** Default constructor */
	FDHGameplaySettings() {}

	/* ///////////////////////// VARIABLES ///////////////////////// */
public:
	/* ////////// VIEW CONTROL OPTIONS ////////// */

	/** True to invert yaw input, otherwise false */
	UPROPERTY(EditAnywhere, BlueprintReadWrite)
		bool bInvertYaw = false;

	/** Intensity of yaw input as modifier */
	UPROPERTY(EditAnywhere, BlueprintReadWrite)
		float YawInputModifier = 1.0f;

	/** True to invert pitch input, otherwise false */
	UPROPERTY(EditAnywhere, BlueprintReadWrite)
		bool bInvertPitch = false;

	/** Intensity of pitch input as modifier */
	UPROPERTY(EditAnywhere, BlueprintReadWrite)
		float PitchInputModifier = 1.0f;
};

/** FDHVideoSettings
* Declared: DHVideoSettings.h
*
* Video settings for game
*/
USTRUCT(BlueprintType)
struct FDHVideoSettings
{
	GENERATED_USTRUCT_BODY();

public:
	/** Default constructor */
	FDHVideoSettings() {}

	/* ///////////////////////// VARIABLES ///////////////////////// */
public:
	/* ////////// SETTINGS ////////// */

	/** Field of view */
	float FieldOfView = 80.0f;
};

/** UDHSaveSettings : USaveGame
 * Declared: DHSaveSettings.h
 *
 * Saved user settings
 */
UCLASS()
class DISTANTHOME_API UDHSaveSettings : public USaveGame
{
	GENERATED_BODY()
	
public:
	/** Default constructor */
	UDHSaveSettings(const FObjectInitializer& ObjectInitializer);
	
	/* ///////////////////////// VARIABLES ///////////////////////// */
public:
	/* ////////// SETTINGS ////////// */

	/** Gameplay settings */
	UPROPERTY(EditAnywhere, BlueprintReadWrite, Category = "Settings")
		FDHGameplaySettings GameplaySettings;

	/** Video settings */
	UPROPERTY(EditAnywhere, BlueprintReadWrite, Category = "Settings")
		FDHVideoSettings VideoSettings;
};

There are no C++ errors in the log file.

Instead, UnrealHeaderTool crashes.

I’m not sure why that could happen. A bug, perhaps?

Ensure that the header file is readable (isn’t locked by Google Drive, antivirus software, your text editor or anything else), aside from that, I don’t really have any idea.

I’m working around this issue by creating dummy UCLASSes :smiley:

Hey!
If you’re still having issues, I noticed two errors in your code:

  • Remove the semi-colon from GENERATED_USTRUCT_BODY();
  • If you declare BlueprintReadWrite in the UPROPERTY() macro. You must also define a category. Example:


UPROPERTY(EditAnywhere, BlueprintReadWrite, Category = "FDHGamePlaySettings | Struct Variables")
		bool bInvertYaw;


Good luck!