I’m trying to familiarize myself more with UE’s C++ side of things. What code body should I generate for creating classes or structures? For example, let’s say I want to create an item class/struct for an inventory system. Do I create a new C++ file from within UE? If so, what type? Or do I just create an empty file inside of Visual Studio? What is the recommended method? Should I use the function library for this or struct factory? Does it matter?
I read the documentation here, but it says nothing about how to generate the file.
I usually create an empty file from the unreal editor called “GenericStructs” and put all the structs inside it (I do the same thing with enums).
There is no official rule on where to store them, my personal rule is “if it’s needed only in one class I Place it on the top of the class file, if it’s used in more than one class it goes inside GenericStructs file” (so you have to #include it in order to use it).
with empty file you mean this?
nice tip! thanks
Yes, I mean an empty class.
Another important thing to remember is to mark both enums and structs as “BlueprintType”, otherwise they won’t be visible in the editor (Only in C++, but this could be a desired behaviour in certain cases).
Example for structs:
class ADungeonRoom *AdjacentRoom;
AdjacentRoomIndex = -1;
AdjacentRoom = nullptr;
BorderType = EBorderTypes::EBT_Wall;
DoorType = EDoorTypes::EDT_Wall;
bShowRoadOnMap = false;
You can declare functions inside structs, in this case you can see a constructor, but you can also init values directly with “=”, you can also have more than one constructor (for example FVector is a struct that you can init with a single float or with three floats obtaining different results)
Example for enum:
enum class EDirections : uint8
If you want to implement a function you can do it inside the class:
class YOURPROJECTNAME_API GenericEnums
static EDirections GetOppositeDirection(EDirections Direction);
Implementation (.cpp file):
EDirections GenericEnums::GetOppositeDirection(EDirections Direction)
Wow! If it were a snake, it would have bitten me Thanks!
Very thorough! Thank you!
If my answer helped you please mark it as solution, in this way you’ll help other user interested in this topic (seeing it marked as “resolved” is an incentive to click)
Have a nice day!