#C++ Structs/Enums
1 messages · Page 1 of 1 (latest)
// Inside a SomeSystemTypes.h file.
/// Ensures file isn't included more than once. Not necessarily interesting for you but should be added.
#pragma once
/// That's a file that Unreal Engine generates when you use these USTRUCT, etc. macros. It has extra code in it and you need it whenever you have those macros in a header file. A file with pure C++ types wouldn't need it fwiw.
/// The file always follows the same naming <HeadFilerName>.generated.h
#include "SomeSystemTypes.generated.h"
/// Enum exposed to Blueprints.
UENUM(BlueprintType)
enum class ESomeEnum : uint8
{
None,
/// UMETA macro can be used to adjust the name a bit if you want, but might be redundant in most of your cases.
/// So can be ingored if you don't need that. It will just use the "SomeValue" then.
SomeValue UMETA(DisplayName = "Some Better Name"),
AnotherValue
};
/// Struct exposed to Blueprints.
USTRUCT(BlueprintType)
struct FSomeStruct
{
/// Needed. Unreal Engine will inject some extra code via the generated.h for this.
GENERATED_BODY()
/// Property Exposed to Blueprints.
/// Can be edited "anywhere" (Blueprint Defaults and on Instances placed into the level.)
/// Can be read from and written to. If you would work more in C++ then there are cases where you might not want Blueprint users to write the values but only read it.
UPROPERTY(EditAnywhere, BlueprintReadWrite)
float SomeFloat = 0.f; /// Make sure to give it a default value. UE will otherwise annoy you. Not all types need a default value, but I can't explain here how to spot that.
/// Can also give them a category if wanted.
UPROPERTY(EditAnywhere, BlueprintReadWrite, Category = "Some Category")
bool bSomeBoolean = false;
/// Can also do lots of fancy meta stuff.
UPROPERTY(EditAnywhere, BlueprintReadWrite, meta = (DisplayName = "Some Better Name"))
FString SomeBadName = FString("");
};
This is written by hand into the chat here, so could be that I have typos in it.
For a list of what those macros can actually have added, you can check this: https://unreal-garden.com/docs/uproperty/
(you can select at the top what other ones you want to get the specifier for)
Small additional thing:
/// If you want to use a Struct for a DataTable, you need to inherit from FTableRowBase.
/// That will ensure it shows up when selecting what struct to create the DT from in the Editor.
USTRUCT(BlueprintType)
struct FSomeDataTableStruct : public FTableRowBase
{
/// If you find old tutorials that use GENERATED_USTRUCT_BODY(), just replace that with the GENERATED_BODY() macro.
GENERATED_BODY()
UPROPERTY(EditAnywhere, BlueprintReadWrite)
int32 SomeInteger = 0;
};
this is super dope, everything you wrote is actually making sense, and im making connections on some of the syntax that i've seen a lot but have never fully known exactly what it does/means but this helps that be more clear, thank you
definitely going to attempt this today and update
assuming type, but maybe not, for the float value you put as defult 0.f should it be 0.0?
maybe that's indicating single / double point?
.f is float, yes
hmm ok so would defualt being 0 be an issue? as most of the time i see floats show up as 0.0 , and im assuming no matter what i put even if 0.34 , i'd need the .f" at the end. so 0.34.f?
also, if im understanding "meta" concept correclty, it's more like a quality of life boost to your variable names, right?
edit: ok i see the comment about the Umeta specifically, so yea makes sense
.34 0.34 double
.34f 0.34f float
This is to help the compiler. It won't cause issues if you uses them mixed fwiw.
also in ref to this message, this is my source folder, having pretty much nothing in there, but would like to make one where i can put all my basic creation of structs/enums in, even if crowded in the end.
I wouldn't mix them all fwiw, I would sort them by what they are used for.
Public/Systems/Inventory/InventoryTypes.h
Public/Systems/Damage/DamageTypes.h
... etc.
semi confused on this still but are you saying that the .34 OR 0.34 = double float? , as in i could use either one, syntax wise? and same with the latter example .34f or 0.34f = float (single persiion)
Yes, you can drop the 0 before the .
that makes sense, i don't een have a systmes folder it seems so guess i'll make one. and i could call it "WorldGames.h" , meaning this is like the "folder" that all my bps would have gone into for that thing, but not a folder of course, just trying to conceptualize that
and then i'd do
#include "WorldGames.generated.h" for the struct ?
or anything related to that ?
// Float
0.f
.0f
0.0f
// Double
0.
.0
0.0
seems more like a "foldering" concpet to me like a way for the engine to organize things
You can use whatever you like. The clearest is of course to not drop any of the 0
The folder would just be a simple folder name. Nothing with .h
In the folder you can place the WorldGamesTypes.h file
but the "f" in the top example, is for single persision then
Yes
got you, i suppose that would be used in most cases . that's just 1 single .0 right? nothing less than* than a tenth (or 0)
is the "types" needed ? or is that just a naming convention that is typically used ?
It just sets it to 0 by default, yes.
Naming convention. WorldGames.h doesn't say much.
i guess i would have figured they are all "types" at that point if they all use the "types" suffix but makes sense
That goes into the file at the top below the #pragma once. This not directly related to the structs, but more to the usage of the all caps macros that you need to expose the enums and structs.
as far as public vs private , what would be the difference or if i should include the folder in the private as well as public for whatever reason
It's more about being able to see it when looking at the file itself.
You usually place .cpp into private and .h into public. Atleast in most cases.
Since your structs won't have any functions for now, there is not need for a .cpp file, so the private folder isn't relevant.
Functions would have their declaration in the .h and their implementation in the .cpp, that's why it's not relevant for now.
hmm interesting, that makes perfect sense tho
ok im going to give it a go, and take a structure i have and turn it into a cpp one, will report back in a bit-ish
the intial folder creation tho, does the placement matter? source / systems / wahtever , then files inside that "wahtever" folder
so for the "WorldGamesTypes.h" file, i add all the stuff to that ? like enums related, structs, any additional variables
also, do i only need that file? i think it auto created me a .cpp version as well, but it's essnetially blank apart from the #include part
You can delete the .cpp
ok nice, making some progress
#pragma once
include "WorldGamesType.generated.h"
USTRUCT(BlueprintType)
struct FGameRules
{
GENERATED_BODY()UPROPERTY(EditAnywhere, BlueprintReadWrite) bool bTeams=false; UPROPERTY(EditAnywhere, BlueprintReadWrite) int32 MinNumberOfTeams = 0; UPROPERTY(EditAnywhere, BlueprintReadWrite) int32 MaxNumberOfTeams = 0; UPROPERTY(EditAnywhere, BlueprintReadWrite) int32 MinNumberOfPlayers = 0; UPROPERTY(EditAnywhere, BlueprintReadWrite) int32 MaxNumberOfPlayers = 0; UPROPERTY(EditAnywhere, BlueprintReadWrite) FText GameName = TheNameOfTheGame; UPROPERTY(EditAnywhere, BlueprintReadWrite) bool bStake = false; UPROPERTY(EditAnywhere, BlueprintReadWrite) bool b1V1 = false; UPROPERTY(EditAnywhere, BlueprintReadWrite) float TimeLimit = 0.0f; UPROPERTY(EditAnywhere, BlueprintReadWrite) int32 ScoreLimit = 0; UPROPERTY(EditAnywhere, BlueprintReadWrite) int32 MinStake = 0; UPROPERTY(EditAnywhere, BlueprintReadWrite) int32 MaxStake = 0; // how'd we do};
screenshots if helps
bit slow as i had to learn a bit of each one, but i find it strange that the "bool" , "float" and prob others and blue while the rest are purple, not sure if this indicates anything, but i tried to do Fboolproperty on 1 and it kidna worked i think but not sure if it makes a difference / has different use case. same goes for the float
also, for the ftext, it doesn't give error but gives a strange thing for the the name i used , not sure if matters either
also, not the greatest ordering / whatever, i just tried to rememer what i had, and honestly forgot so just started to add things to test
will need to learn the proper things for each thing, if that maeks sense lol like how enums would be called
also , is it possible to ever access something like a Blueprint created Enum?
Apologies in advance for all the Qs but really appreciate the help here this has actually been a nice unlock for me.. lots to learn still in order to get the real benefit here but it’s nice to feel somewhat connected to what i need to learn from here
0>WorldGamesType.h(34,19): Error : use of undeclared identifier 'WorldGameName' 0> 34 | FText GameName = WorldGameName;
tried to build and think it failed and gave me this error, not sure what it means
assuming it's refering to this, but not sure why it would be giving error unless i need to do something specific here
i just deleted that one but not sure as to why the text , name, and string all were giving me issues, like they woudln't build, idk what i'd need to do , but i think i just was typing it incorreclty, i think learning all thos leittle things is what will be the experience curve to being able to do things quickly and effectively
Update: i did solve the majroity of my questions in the #cpp channel thanks to some more peeps, but i again appreicate you steering me in this direction
Ah sorry, I wasn't at my PC.
I would remove the new lines between UPROPERTY and the property itself. You can keep the new lines between 2 sets of UPROPERTY and property though.
And the FText would just be initialized with FText::GetEmpty();
It's easier to then set that properly in the DataTable/BP.
At least for now.
No, that's why it's better to have types in C++.
all good man, i appreciate this kind of help, it's usually the moments of unlock and in my case, it really is. i feel slightly more confident now, and i also understand a bit more about errors when building now that i understand the application of these terms. otherwise, all trial and error lol
ok cool, i have this currently which did work , but perhaps i'll do the one you mentioned if better for the datatable
nice, legimtatley my first time even writing code like this, so organization will be something i'll need to learn
just to share, i also took the enums and made some and added one to the struct, was a bit of a process, as i didn't know that you needed to write it like that so it can be inlcuded in a struct (i leanred can use "class" still, instead of "struct" but the typename=0 stuff was needed to have it show up in the struct properly for some reason)
Should be enum class though. And the number shouldn't be needed. It assignes those in that same order anyway.
You should get rid of these new lines.
cleaned that up a bit
hmm is there any difference to the class vs the struct? i was told they are essentially the same but the struct is public by default and the class isn't but i also don't really know the importance / context of this info
i just cahnged it all back and it does in fact work still. will go with class and the method you suggested , much easier too
For Struct vs Class that is true, but when using it to expose an enum I only ever see Epic using enum class