GameMaker Get a string out of enum

S

Sahibjot Cheema

Guest
Not sure if this is possible or not but I have an enum and i want a variable to change to a random string from that list. For example if i have this enum.
Code:
enum item {
none        = 0,
test1        = 1,
test2        = 2,
test3        = 3,
height      = 11,
}
and i want a variable to change to "test1", not the number 1 but the string. I know theirs a workaround with arrays but I want to know if this is possible?
 
As far as I know, not possible in GML. I know some other languages have a feature to extract the enum as a string.

You'll need to use an array or similar method.
 

samspade

Member
Thx, I geuss i'll use arrays
You could also use a ds_map. GMS 2 allows you to use numbers as the key in a ds_map, though I've been told it's better practice (for reasons I don't understand to use a string). But that means that either of the following methods should work:

Code:
//create
enum_map = ds_map_create();

ds_map_replace(enum_map, item.none, "None");
ds_map_replace(enum_map, string(item.none), "None");

item_type = item.none;

//lookup
text = enum_map[? item.none];
text = enum_map[? item_type];

text = enum_map[? string(item.none)];
text = enum_map[? string(item_type)];
 
S

Sahibjot Cheema

Guest
You could also use a ds_map. GMS 2 allows you to use numbers as the key in a ds_map, though I've been told it's better practice (for reasons I don't understand to use a string). But that means that either of the following methods should work:

Code:
//create
enum_map = ds_map_create();

ds_map_replace(enum_map, item.none, "None");
ds_map_replace(enum_map, string(item.none), "None");

item_type = item.none;

//lookup
text = enum_map[? item.none];
text = enum_map[? item_type];

text = enum_map[? string(item.none)];
text = enum_map[? string(item_type)];
Yes I already know about that method and i'm using it for storing items and their amount and such. But I think using arrays would be better here cause I've already done it :D
 
Top