Download

Accessing Struct in Another Class.

Hi,
So I Created an Inventory Component In which I created a Struct that holds Information about the Items such as Price,Weight,Name etc and In the Variables I made an Array of that Struct So I can Store the Item Information.
Now I have my BasePickupClass in which I also Need to Have The Same Struct So I can Call a Function That Copies that Data Over to the Array in Inventory Component.
I Included #include “InventoryComponent.h” in my base Pickup class but when ever i declare the Variable of Type Struct(ItemInfo) It fails to Compile giving tons of Errors first one is “unknown override specifier” I can provide more Information If Needed.

Code would be a general bonus when asking for help in a C++ project, as generally, when reading code, people are more often likely to catch the issue based off of looking at the code rather than guessing off of bland information.

Here is how I am currently approaching an Inventory(In Blueprints, but can easily be implemented into C++). I have an array of structures, which I add to the Player, which will store my inventory information, such as a ptr to the instanced item(derived from base item), I also have a few other variables such as Item Name, Item Category and a few irrelevant variables. My items implement an interface that has functions retaining to the inventory system(Interfaces do not declare functionality, only describe the function). I call the interface function from where ever I need to, which calls that implemented function, right up the chain to the parent class, which happens to be my base item. When I implement the interface functions, I add all the base functionality I need.

I haven’t done much C++ in the last year(except for research into DataTables Json and K2Nodes, but if you post some code, I’d be more than happy to take a look.

Update on the Issue, Also Asked this question on Unreal Discord and got some help, apparently it is better to create a separate file for Struct.

The way I solved my Issue is I am used to include " " in my .cpp file and there is no way to forward declare a struct like we do with class classname; So the Solution is to #include in the .h file then you can access to the Struct Type created in your other class.

Absolutely you can forward declare structures. How I do it is like so:

In a .h file



//#includes

struct FExampleStruct;

//Example Class
UCLASS()
class AExampleActor : public AActor
{
public:
            void PrintDebugStruct( FExampleStruct StructToDebugPrint );
}


and in my .cpp file



#include ExampleStruct.h

USTRUCT()
struct FExampleStruct 
{
    GENERATED_BODY();

public:
    FString ExampleStringMember;

};

void AExampleActor::PrintDebugStruct( FExampleStruct StructToDebugPrint )
{
    UE_LOG( LogTemp, Warning, TEXT("Printing Structure Debug: %s"), *StructToDebugPrint.ExampleStringMember);
}


I just wrote that out, so it’s just pseudo code.

I Tried This Method the first time I was confused that did not work, I will try again maybe the files were too bloated with code and I might have missed something. Thanks.

Just make sure to include the file where the structure is located in your .cpp file.