Download

how to declare a global struct ?

I need to declare a struct to let it be accessed in all my classes and blueprints, i saw and answerhub to this question, and the first answer says this:

“To create global structs I would encourage you to just create a simple header file in your public code, then include that header in your main project header to you will be able to have access to the structs from every where. This way you have visibility in both native code and BluePrints.”

i tried to adding a simple header file from the visual studio, but i’m getting and error when i use the include file in all my classes, i think that is because i didn’t added the header file from the unreal editor using adding c++, what option i need to use to add the simple header file from the unreal editor to let the project identify my c++ header file in the right way ?

sorry if this is a noob question, but i need help.

thank you in advance for all the help.

After adding the header, right-click on your .uproject file and “Generate Visual Studio project files”. Rebuild.

also check your intermediate folder, i have a global .h file and it was created in there and had to be moved manually

Xenome thank you, that worked fine!

CNKIT, very useful, that happened to me too, moved the file to the right folder and worked fine! thank you too

i need a little help on this, need to declare the global struct, so far i have added the new .h file and include it in the main .h of the game, now i’m tryting to compile the game without adding anything else to the .h file and i’m receiving some errors.



this declaration has no storage class or type specifier	Hellrift	c:\Users\Administrator\Documents\Unreal Projects\Hellrift\Source\Hellrift\HellriftBase.h	3
expected a ';'	Hellrift	c:\Users\Administrator\Documents\Unreal Projects\Hellrift\Source\Hellrift\HellriftBase.h	4	
Expected a base class name	Hellrift	C:\Users\Administrator\Documents\Unreal Projects\Hellrift\Source\Hellrift\HellriftBase.h	1	


the code that i have in the .h file is this



#pragma once

UCLASS()
class HELLRIFT_API AHellriftBase : public 
{
	GENERATED_BODY()


};


sorry to write two weeks later but my computer crashed and i spent a week in fixing it.

hope someone can help me.

p.s.: sorry if this is kind of noob question.

Something is missing after the keyword :



#pragma once

#include "GameFramework/Actor.h"
#include "HellriftBase.generated.h"

UCLASS()
class HELLRIFT_API AHellriftBase : public AActor
{
	GENERATED_BODY()


};


:slight_smile:

thank you for your help RPod, that worked, i’m trying to declare the struct, but i checked a lot of info on the forum, answerhub, even youtube, but i can’t figure out why i can’t compile the code when i declare the struct, this is my struct code



USTRUCT(BlueprintType)
struct HellriftAffixes
{
	GENERATED_USTRUCT_BODY()

	UPROPERTY(EditAnywhere, BlueprintReadWrite, Category = "Affixes")
	FString AffixeName;

	UPROPERTY(EditAnywhere, BlueprintReadWrite, Category = "Affixes")
	int32 HandleArray;

	UPROPERTY(EditAnywhere, BlueprintReadWrite, Category = "Affixes")
	int32 RarityCode;

	//0=damage, 1=armor
	UPROPERTY(EditAnywhere, BlueprintReadWrite, Category = "Affixes")
	int32 EnhancedProperty;

	UPROPERTY(EditAnywhere, BlueprintReadWrite, Category = "Affixes")
	int32 EnhancedValue;

};


i tried to put this code on the c++ .h file that i have created outside of the ue4 editor, also i have tried using that code in the main project .h file, even in others .h from the project, is the same, always get error and don’t build.

i’m receiving an error on the FString line



this declaration has no storage class or type specifier	Hellrift	c:\Users\Administrator\Documents\Unreal Projects\Hellrift\Source\Hellrift\BaseItem.h	16	


any help will be very useful.

thanks in advance.

try this:




#pragma once

#include "HellriftAffixes.generated.h" // must be last include in te HellriftAffixes.h file


USTRUCT(BlueprintType)
struct FHellriftAffixes // Struct use F in prefix. 
{
	GENERATED_USTRUCT_BODY()

	UPROPERTY(EditAnywhere, BlueprintReadWrite, Category = "Affixes")
	FString AffixeName;

	UPROPERTY(EditAnywhere, BlueprintReadWrite, Category = "Affixes")
	int32 HandleArray;

	UPROPERTY(EditAnywhere, BlueprintReadWrite, Category = "Affixes")
	int32 RarityCode;

	//0=damage, 1=armor
	UPROPERTY(EditAnywhere, BlueprintReadWrite, Category = "Affixes")
	int32 EnhancedProperty;

	UPROPERTY(EditAnywhere, BlueprintReadWrite, Category = "Affixes")
	int32 EnhancedValue;

};



i don’t have a “HellriftAffixes.h” file, i’m declaring the struct in the main project .h, in this way



// Copyright 1998-2015 Epic Games, Inc. All Rights Reserved.
#ifndef __HELLRIFT_H__
#define __HELLRIFT_H__

#include "EngineMinimal.h"

#endif
#include "HellriftAffixes.generated.h"

USTRUCT(BlueprintType)
struct FHellriftAffixes
{
	GENERATED_USTRUCT_BODY()

		UPROPERTY(EditAnywhere, BlueprintReadWrite, Category = "Affixes")
		FString AffixeName;

	UPROPERTY(EditAnywhere, BlueprintReadWrite, Category = "Affixes")
		int32 HandleArray;

	UPROPERTY(EditAnywhere, BlueprintReadWrite, Category = "Affixes")
		int32 RarityCode;

	//0=damage, 1=armor
	UPROPERTY(EditAnywhere, BlueprintReadWrite, Category = "Affixes")
		int32 EnhancedProperty;

	UPROPERTY(EditAnywhere, BlueprintReadWrite, Category = "Affixes")
		int32 EnhancedValue;

};


i’m getting a 1083 error on the #include “HellriftAffixes.generated.h” line.

The #endif preprocesseur instruction should be placed a the end of you header file.

But anyway you should put the struct in HellriftAffixes.h like I show you in first place and add an:


#include "HellriftAffixes.h"

In The main project header file.

A tips, don’t use :

#ifndef HELLRIFT_H
#define HELLRIFT_H
#endif // HELLRIFT_H

its an old way.

Use just:
#pragma once

It works under linux too :wink:

Thank you for all your help RPod, i got it working.

Thank you for your patience and help!

I think 4.12 something has changed
RPOD could you please give a simple example of just making the file without using enzoravo’s stuff.

I’d like to have a file with enums and struct declared that could be used globally thanks