Enums & Structures worth it?

Hey guys,

so recently I started thinking if it is worth to use Enums & Structures for storing and “developing” Character Stats like Health,exp and Values like attack dmg etc. instead of doing it “standard way”. Anyone who can throw their opinion about it?

Second question:

If I am trying to do multiplayer/online game (just as learning experience) should I put all movement BPs in Pawn or PlayerController currently I’m doing it in pawn?

Thanks for answers,

Regards,

Enums and structures are generally used for organizing and consolidation, enums are useful as en example, in naming a series of states such as AIEnum: Safe, Caution, Danger. While you could use an integer, it’s easier to use a custom named enum so you simply use Caution instead of having to remember Index 1 is for caution. It’s also nice for organizing things like weapons, e.g. I use an enum for weapon types which I use to check conditions, such as WeaponType: Pistol, Shotgun, Rifle, Rocketlauncher, etc. You can also read the enum name and output it to a string which is useful as well.

Structures can literally be used for anything to make your life and organization easier in handling data. A vector3 is simply a structure of 3 floats. I also use structures as an example in weapon data. So I’ll Have a WeaponStructure containing Enum WeaponType, float AmmoInventory, float AmmoCountInMag, bool IsInfiniteAmmo, float DamageToGive, float RandVarationInDamage, etc. You could then make a structure of your structure if you’d like, so Structure PlayerData would contain float Health, transform ActorTransform (which transform is an included struct of the structs rot, vector3, etc.), WeaponInventory (An array of WeaponType), etc.

Super useful when doing things like constantly needing a collection of data at once in a function, storing/saving data, doing loops, etc.

One thing to note for blueprint Enums is there is I believe a limit of 256ish entries, while the blueprint enum editor will let you add more, it’ll actually forget/loop back to index 0 of an enum for any values higher than the hardcoded limit, though I think you can use unlimited enum entries in C++. Learned that the hard way when I sillily manually entered an enum of 1000 names only to find out the first 256 was all that it would read. Problem solved for that particular situation (making NPCs have random names from a list) by reading a csv datatable. As long as you don’t need over 256 specifically named items in one enum, you’re good to go.

Enums are also nice in using the switch node to create a readable flow of your graph, instead of using a bunch of if/else boolean checks, such as shown in my included image.
Here it’s used basically as a human-readable integer that determines which action my AI should take upon the binded event of Movement Completed. Caution being to move towards a random point around the sound they heard and moved to already in order to create a search radius, Hostile meaning the AI is directly engaging an enemy and so will instead just keep moving to a random point around itself once it reached the last point/had stopped moving.

“worth” implies there’s a cost / reward relationship. There is none if you don’t compare Enum and Structures with something else.

They are basically just tools among other tools the programmation give us.

As Deathstick mentionned, you can use them for many purposes.

Are they worth it or not depends on what tools you want to use instead of them. You could say “Maybe one stat like HEALTH in my game is a STRUCTURE, but maybe it can be an ACTOR COMPONENT as well”. In this case, you can ask if it’s worth it.
The pros of using a structure is the performance cost (lighter than actor component), the cons are that you can’t add functions to structures (in blueprint) but you can in components (meaning you can build up a logic around your stat).
But if you just want pure data storage ability, structures is the way to go in this case.

Enums are usually used just to categorize datas depending on NAMES everyone can understand in the team regardless of the game you’re creating.

Honestly I don’t think any game has ever been shipped without using them (from the moment they became part of our main programming languages).

Usually you put the input listening logic in the Controller which then calls a function on your character which does the action.
It would look like this:

Controller -> Tell Character to move
Character -> Move

But it often all come down to: will I switch character, are there different character types, do they share the same input logic etc…

If you always have ONE character and never switches, you don’t care much where you put stuff. But if you want to keep the option available to create more character types in the future, you’re better off thinking modular right off the bath and keep the Controller / Character logic clean.

Ok, thanks guys!

@Yun-Kun I’ve one more question.

Lets say I’ve One character just like lets say Witcher but this class is summoner and I can send commands for my pets to attack or stay etc. Should I set it all in player controller? Not sure if it counts as character switch

What input does your character use to command its pets? Is it the same input for every character class you have but with different consequences or is it an input that only your witcher class has?

If every class have this input but do different things with it, I guess the input listener is in the controller and the actions to take are in the character class.

To me it looks like you have:

  • PlayerController
  • MainClassCharacter
  • SubClassCharacter 1
  • SubClassCharacter 2

SubClassCharacter is a child class of MainClassCharacter.

PlayerController calls functions in the MainClassCharacter when you press inputs.

All actions shared by your SubClasses are coded in the MainClass.

As soon as you have a SubClass that does something different than the MainClass when pressing a specific input, you can override the function in the SubClassCharacter to do what you want it to do.