#C++ Structs/Enums

1 messages · Page 1 of 1 (latest)

paper anvil
#
// 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.

#

(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;
};
keen torrent
#

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

keen torrent
#

maybe that's indicating single / double point?

paper anvil
#

.f is float, yes

keen torrent
# paper anvil .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

paper anvil
keen torrent
#

#multiplayer message

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.

paper anvil
#

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.

keen torrent
paper anvil
#

Yes, you can drop the 0 before the .

keen torrent
#

and then i'd do

#include "WorldGames.generated.h" for the struct ?

#

or anything related to that ?

paper anvil
#
// Float
0.f
.0f
0.0f

// Double
0.
.0
0.0
keen torrent
#

seems more like a "foldering" concpet to me like a way for the engine to organize things

paper anvil
#

You can use whatever you like. The clearest is of course to not drop any of the 0

paper anvil
#

In the folder you can place the WorldGamesTypes.h file

keen torrent
#

but the "f" in the top example, is for single persision then

paper anvil
#

Yes

keen torrent
#

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)

keen torrent
paper anvil
paper anvil
keen torrent
paper anvil
keen torrent
#

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

paper anvil
paper anvil
#

Functions would have their declaration in the .h and their implementation in the .cpp, that's why it's not relevant for now.

keen torrent
#

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

paper anvil
#

You can delete the .cpp

keen torrent
#

ok nice, making some progress

keen torrent
#

#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?

keen torrent
#

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

keen torrent
#

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

keen torrent
#

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

paper anvil
#

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.

paper anvil
keen torrent
# paper anvil Ah sorry, I wasn't at my PC.

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

keen torrent
keen torrent
#

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)

paper anvil
#

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.

keen torrent
keen torrent
#

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

paper anvil