How to reference Ustruct as a variable type?

I’ve had a similar issue with structures and linking. For now, I define my structure in the gamemode header file, then if I have to use the structure in another file (.cpp/.h) then I simply add the gamemode header and use it without the using the gamemode’s namespace beforehand.




#include GameMode.h

FMyStruct structureInstance;

There may be a better way of doing this or TArray may attempt to do something different.

Hi All

Apologies if this is a dumb question- how do you reference a UStruct as a variable type for a TArray etc? I’ve declared a USTRUCT in the GameMode class, and am referencing the type in other classes- is this possible? Obviously I’ve included the GameMode header in the classes I’m referencing it from. I’m getting an error- C2653 FMyStruct is not a class or namespace name. I’m referencing it like this;

TArray <struct MyGameMode::FMyStruct> MyFunction(Function arguments);


From what I’m beginning to understand, a struct is what class inherits from. It’s defined in a single header. So you add a header to your project and put;

# pragma once
# include "FMyStructName.generated.h"

Then the rest of the struct declaration. Then you #include it wherever you use it. So its a mini class without the cpp file and you declare functions and stuff inline- if you want to have them inside the struct. (You can’t use “add code to project”).

It looks like the documentation assumes we already know this. But Stack Overflow has a load of info about structs.

Take a look at this:

And maybe try

// I'm assuming this is in your header file
TArray<struct FMyStruct> MyFunction(Function arguments);

Hey Green, thanks! I sorted it- see comment below. The problems were occurring when I defined it in the header file of a different class and included that classes header file. Now I’m making structs in their own header files and including them where they’re used and all is well.

Okay, glad you solved it.

yeah you’re right- I was putting it within the class definition, which is more than usually stupid…