Using Enums in GDScript for Better Code in Godot 4

preview_player
Показать описание
Enums in GDScript are a great way to manage state and limited options for variables. This video goes over how to define them, use them, and work with them in the Inspector in Godot 4. The concept is illustrated by adding a fire state to a play that handles firing one bullet or two. An anti-example is provided, then the code is refactored to show the value of enums.

Godot version used: 4.0

Chapters
00:00 - Intro
02:24 - Counter-Example Using a String Variable
05:50 - Refactor to Using an Enum
11:10 - Referencing an Enum in Other Scripts
12:33 - Code Clean Up
14:16 - Easier Testing
15:14 - Outro

#Godot #Godot4 #GrowingWithGodot #GodotEngine
Рекомендации по теме
Комментарии
Автор

if you want to print an enum so you can read the name instead of seeing the number,

furroy
Автор

In case anyone is wondering, at 8:37 you have to reload the project to show the documentation

matheus
Автор

Thanks for this video. I understood how to declare an enum, but I wasn't aware that I had to set a separate non-constant variable that defines the current enumeration value.

Example:

enum PlAYER_MOVEMENT_STATES{IDLE, WALKING, RUNNING, CROUCHING}

var Current_Player_Movement_State : PlAYER_MOVEMENT_STATES = PlAYER_MOVEMENT_STATES.IDLE

Now that I know this, enumerations (and state machines by proxy) are very easy to use now.

MonkanicsDev
Автор

This was helpful and helped me make up my mind over how to handle some damage calculations for my own game, thank you for a very detailed and informative approach. I knew a lot of it, but you did show me a few neat tricks I definitely did not lol

xeroterragoth
Автор

Bonus points for teaching me about Ctrl+D

Slipy
Автор

At 8:44 the doc string isn't being added to the inspector because there are outstanding syntax errors that have to be resolved first.

ozbonus
Автор

Brett your tutorials are invaluable. Thank you for contributing to godot community

entei
Автор

A good thing about doing it this way, is that you can expand the list - *backwards*. That is, imagine you have firetype double and triple, and only later decide to have a single-shot fire type too; you can put it in the enum list, *before* the double, and because everything is using the enum, it breaks nothing at all.
You can also do things like have an enum and an array at the top of your file that are in parity, and use the enum as an index for the array, so that you can always just quick-access the correct array element from another class no matter whether you've changed things. I found that really useful when making some temporary debugging tools.

charliecharliewhiskey
Автор

Very helpful as always. And wow you make a lot of videos fast!

unrealingenium-izds
Автор

Thanks for the vid! Really well explained.

Delta_Greed
Автор

8:50 - I like how you didn't edit it out. :D

-cancode-
Автор

I finally understood enums thanks to you

HanyElsafty-snoc
Автор

this is a great advice! First we make it work, than we polish

D.E.Nicolas.Goncalves
Автор

Thank You for showing this. You mad it informative and easy to get

Seraph
Автор

This is a really well made tutorial! Thank you!!

PorChorrr
Автор

Thanks, this is exactly what I needed

PokemanZN
Автор

the fainting is actually funny because it gives emotion to the mistake.type

nvanderhoff
Автор

I found enum a few days back, using it for game state, really needed to know how to access it globally across scenes - thanks. I now know class_name :)

vizionthing
Автор

I was just making UI prototype that has a bunch of toggles and moves a few frames of some sprites with one button, this seems a lot better way to select from a list of things.

Автор

I’m a beginner so forgive me but can someone explain why the index of an enum value would ever be needed if enum values are constant and hard coded? When would I ever not reference it by name?

Also follow up question: wouldn’t directly setting the integer value (do you even call it the index?) functionally turn your enum into a dictionary of constants?

Edit: kind of realizing the nature of my question is really just what use the integer values have? This video is immensely helpful but it only contains an example of using enums as mini state machines and I can’t help but feel there’s more to them, possibly?

anonymous
join shbcf.ru