Can you access an ustruct from another class
So I am trying to make an struct that I want to use in different classes, but I don't want to have the decleration of the struct in every class. How can I achieve an ustruct that is accessable from multiple classes and possibly even blueprints? This is what I tried in an empty class in which I removed everything except for the headers and #pragma once:
I have tried the blueprintype, GENERATED_USTRUCT_BODY(), GENERATED_BODY(), the GAME_API, the struct name with and without F. But whatever I try I can't access it from another header file to declare a variable.
Any help would be greatly apreciated.
All you have to do is include the header that holds the struct definition in whatever source you want to have access to the struct; just like if you want a class to have access to an external class you have to include the header file defining the external class.
You can create a header file that only holds your struct (or multiple structs, and/or enums) if you don't want to include the class as well, and then you don't need to create a .cpp file associated with it.
If you want it accessible in blueprints you need to use the BlueprintType specifier in the USTRUCT macro:
With the above struct, in whatever class you want to have access to it include:
If you use it in the header (like if you define a class variable using the struct, or have a function that returns your struct or accepts the struct as an argument), include it in the header. If you just use the struct within a method but it's never exposed outside, you can just place it in the source file.
Same with enums, or any other datatype you may define.
Follow this question
Once you sign in you will be able to subscribe for any updates here