Help with Enums

K

Kululu17

Guest
Hi All,

I was hoping someone could explain briefly how enums are useful. I've read the manual, and I think I understand HOW to use them, but I just don't get WHY to use them. Here is an example, where I thought they might be useful, but maybe I don't understand their functionality.

I am developing an AI routine for some NPCs, and I thought it would be useful to assign them states to define what they should be doing, so for example camping, exploring, building. I could assign a variable that keeps track of the state, either by assigning it a number, or a string to define the state, and then based on the state, assign behaviors. So either:

NPC_State = 0 //and I know this is camping

or

NPC_State = "camping" //which is clear, and can be shown with the draw command for debugging, but requires a little more work to increment (for the first example, I could switch states by just using NPC_State +=1, here I would have to use some if statements to check the strings.)

or I could use enums, where I define NPC_States with camping, exploring, building, and then I could use

NPC_State = NPC_States.camping // which would equal 0

But I can no longer get the text out of it for debugging like in the second method where I use strings. If I show the value for this, I simply get "0". So it works, but the end result is just like the first example, where I use a number only. So it seems like extra code, but no benefits.

What am I missing here?

Thanks!
 

TsukaYuriko

☄️
Forum Staff
Moderator
Using enums will ensure that you will be unable to specify a state that doesn't exist. Therefore, it makes your code less prone to manual errors such as setting the state to 6 when the highest available state is 5, or spelling "running" as "runnign". Attempting to set something to an enum entry that doesn't exist throws a proper error message rather than just silently failing (and thus leaving you in the dark about what caused the wrong behavior in the first place).

It also adds a layer of convenience, since enum entries are taken into consideration for auto-complete.
 
Enums can be basically thought of as grouped constants that can be created via code instead of through the (in my opinion) horrendous "Macro" editor. Unlike "macros" however, they can only contain numeric values. Not much more to say about them really. They just exist mainly to provide better code readability, and a place to centralize "magic numbers".
 
K

Kululu17

Guest
OK, thanks! Is there a way to read out their name in the game. Let's say in the above example I wanted to show the enum name that the state is currently in... so whereas draw_text(x,y,string(NPC_States.camping)) would produce "0", is there a way to instead draw the text "camping".
 

TsukaYuriko

☄️
Forum Staff
Moderator
No. Similarly to variable names, enums and their items are not strings and can not be used interchangeably in any way... or at least there is no built-in way. You can create arrays corresponding to each enum and fill them with opposing values (so that index 0 contains the name of the 0th state), then retrieve the name that way.
 

TheouAegis

Member
Code:
if debug_mode
switch NPC_State
{
    case 0: draw_text(0,0,"camping"); break;
    case 1: draw_text(0,0,"walking"); break;
}
If you don't like it, sorry; but that's how programmers have been debugging their games for decades.
 
K

Kululu17

Guest
Hey, no problem, thanks for the tip. I just want to make sure I don't add a bunch of unnecessary code!
 
Top