Quick Tip: Use Enums for State handling (DON'T USE STRINGS - Unity Tutorial)

Quick Tip: Use Enums for State handling (DON'T USE STRINGS - Unity Tutorial)

Code Monkey

5 лет назад

49,460 Просмотров

Ссылки и html тэги не поддерживаются


Комментарии:

@thebirdgang2240
@thebirdgang2240 - 07.02.2023 04:38

I know this video is old, but say I wanted to use the enum values as parameters for functions in the script, functions that would be referenced in other scripts. Would it be a good idea to make the enum public so these other scripts can use them for the functions?

Ответить
@y01cu_yt
@y01cu_yt - 13.08.2022 10:04

Thanks!

Ответить
@zeke9775
@zeke9775 - 07.07.2022 07:29

Do you still believe, as you say in the beginning of the video, that you should always use enums for state handling? I don't use string references I use individual classes that I attach as components.

Ответить
@kingdom6479
@kingdom6479 - 05.03.2022 12:28

Really useful I like it

Ответить
@chocochipcookiealert
@chocochipcookiealert - 02.10.2021 23:26

Hi! Just curious to make sure this is right. Is the origonal way you set it up bad because it was just a string representation of the weapon vs enum? Is enum better because of ease of swtiching AND last, it looked like you srt a property in your weapon class, acalled weapon type to an enum but how could these change? Sbould it be a get/set?

Ответить
@TacticalProgrammer
@TacticalProgrammer - 29.04.2021 01:17

This is exactly what I was looking for!
Awesome!!!

Ответить
@Giannis_Krimitzas
@Giannis_Krimitzas - 06.04.2021 14:19

How can you iterate through the values of an enum?

Ответить
@ir3dyplayz78
@ir3dyplayz78 - 02.03.2021 04:03

How do I go into another state while in a different state, so not your base movement.. i.e. Going from sprint to dodge without letting off sprint

Ответить
@restushlogic5794
@restushlogic5794 - 07.02.2021 12:35

if you want to select enum

public enum YourEnum
{
Dog=1,
Cat=2
}

public YourEnum YourEnumToChange;

public void YourMethod(int yourInt)
{
YourEnum selectEnum = (YourEnum)yourInt;
YourEnumToChange = selectEnum;
}

public void TestChageEnum()
{
YourMethod(2); // YourEnumToChange become Cat.
}

Ответить
@Deadener
@Deadener - 30.12.2020 01:41

Quicker Tip (and another reason to use enums instead of strings (not that more convincing was needed)): Learn the magic of double-tapping Tab in Visual Studio

1: Make your enum, and currentState value
2: In your chosen code block, type "switch" and double-tap Tab
3: Fill in the name of your currentState variable and double-tap Tab again
4: Watch in amazement as VS automatically fills out your switch statement with all the cases

Double-tapping Tab also works for other things like loops and 'if' statements. For loops, you can also (single)Tab through the arguments instead of manually highlighting them and typing over them. Unchanged arguments should be highlighted in orange. Once the arguments are filled out, just hit Enter, and your in the block.

Ответить
@mehdidev9926
@mehdidev9926 - 03.10.2020 05:08

hii code monkey .. i just want to know the name of this part after the instance of the class

new weapon() { weaponType = "pistol" };

Ответить
@amik0547
@amik0547 - 12.09.2020 03:35

Hi! I was wondering how you would have to reference the different States. Thanks

Ответить
@haarissheikh3187
@haarissheikh3187 - 11.08.2020 02:00

Hi, thank you for this video. really helped me get an understanding of how enums work. but what i'm struggling with is accessing an enum for another code. Could you possibly help me. i created this on the enum script 'case GhostStates.FRIGHTEND:' and i want to activate that 'State' in another code.

the other code has: 'public Petrol.GhostStates Gstate;' on top with ontriggerenter, once the player collects the item:

void OnTriggerEnter(Collider col)
{
if (col.tag == "Player")
{
Gstate = Petrol.GhostStates.FRIGHTEND;
}
}
im not getting any errors but it just doesn't work. Thank you

Ответить
@wendten2
@wendten2 - 02.08.2020 23:24

A neat little trick for visual studio, is that if you have your enum setup with all the options, you can just write "switch(EnumName)" and pres tap, and the editor will automatically write a case for every option

Ответить
@pranjalbajpai9956
@pranjalbajpai9956 - 15.07.2020 16:46

WHAT A LEGEND! ......short and on point , why not all the tutorials are like this

Ответить
@lonewolfgaming6752
@lonewolfgaming6752 - 07.07.2020 20:25

Woah this guy is actually replying to all the problems, i am doing a course for 99$ i didn't have to pay but the instructor doesnt even reply. I subbed.

Ответить
@aibachesterchay7886
@aibachesterchay7886 - 28.03.2020 00:22

The typing speed at some moments though... Galaxy Brain Time!

Ответить
@kitthekat6844
@kitthekat6844 - 08.12.2019 08:37

Quick, short, precise. Love it

Ответить
@Matthew-game
@Matthew-game - 31.10.2019 12:51

Thank you, very simple and easy to understand

Ответить
@morneerasmus1789
@morneerasmus1789 - 26.04.2019 17:38

Thanks for the tip! Question on the state handling, what if my code uses both update and fixed update to handle input and physics, how are you handling the switch statement if specific code needs to run on a state and on a specific update hook?

Ответить
@cakepunch8137
@cakepunch8137 - 25.03.2019 13:35

You should make a Discord server! :D

Ответить
@reaporofdragon744
@reaporofdragon744 - 10.03.2019 23:02

could you do a video about the 64bit requirements for google playstore because i thought of exporting as an apk and opening it up with android sdk and then exporting it again.

Ответить
@jasonroelants4632
@jasonroelants4632 - 10.03.2019 23:00

hey i just started with making my first game and im getting a problem its a error CS1513 expected and i dont know what to do about it does somebody know what i need to do thx

Ответить
@siddharth7441
@siddharth7441 - 09.03.2019 18:07

first time saw a programmer with white colored skin

Ответить
@gelopisaminasotaripitanasi5921
@gelopisaminasotaripitanasi5921 - 09.03.2019 13:51

Great Video! Can you do a video about ping pong ball physics like reflects the wall and contienue to move? I have it as challenge I did the PlayerMovement and now I need the Ball physics reflection but I habe no idea how to code it

Ответить
@zaxe4689
@zaxe4689 - 08.03.2019 13:48

Hey can u guide me which gpu i should buy with
Ryzen 5. 1600
With 16 gb ram
For unreal engine
(plz reply if u can)

(i am confuse b/w rx 580
And rtx 2060)

Ответить
@322ss
@322ss - 08.03.2019 13:33

Thanks, how about doing a second video about enum Flags / bitmasks? Not many who start coding with Unity know how to use them.

Ответить
@ajpritchard7287
@ajpritchard7287 - 08.03.2019 09:31

What’s the advantage of using an enum over a string array?

Ответить
@aishakhatoon325
@aishakhatoon325 - 08.03.2019 09:21

Make a video on tower defence game

Ответить
@dennisgonzales9521
@dennisgonzales9521 - 08.03.2019 02:43

Enums ftw!

Ответить
@bobb6924
@bobb6924 - 08.03.2019 02:06

I used to use integers for state management, but it sucked to forget which number meant what. This is really helpful!

Ответить
@hayurizahard9083
@hayurizahard9083 - 07.03.2019 22:41

Can I use this for leveling up system in my character? Right now I'm trying to figure out how to use leveling up system on unity. Like for example you need to have 200 experience for level up to 2

Ответить
@lee1davis1
@lee1davis1 - 07.03.2019 22:31

I learned this trick the hard way...thanks

Ответить
@cristiano4826
@cristiano4826 - 07.03.2019 21:37

A video about Events pls

Ответить
@erictr3234
@erictr3234 - 07.03.2019 21:34

ai tutorial??

Ответить
@themirlabs
@themirlabs - 07.03.2019 21:34

i find enums useful. in some cases i get confused on how to implement them. such as can i use a button to change a enum. if i can i haven't figured it out yet.

Ответить
@logiis2377
@logiis2377 - 07.03.2019 21:22

Thanks :D

Ответить
@kavarod101
@kavarod101 - 07.03.2019 21:01

Hii bro love your vids (even those which I don't understand quite a lot)

Ответить
@kavarod101
@kavarod101 - 07.03.2019 21:00

Finally

Ответить
@kavarod101
@kavarod101 - 07.03.2019 21:00

1st

Ответить