• Hey Guest! Ever feel like entering a Game Jam, but the time limit is always too much pressure? We get it... You lead a hectic life and dedicating 3 whole days to make a game just doesn't work for you! So, why not enter the GMC SLOW JAM? Take your time! Kick back and make your game over 4 months! Interested? Then just click here!

Legacy GM String to enum?

Anixias

Member
Is there any easy way to reference an enum value using a string? For example:
Script:
Code:
///get_enum(string)
var a = get_enum_from_string_here("item.sword_bronze");
return a;
As in, pass a string and return an enum with the exact match.
Like in the example: I passed "item.sword_bronze", and want the value held in item.sword_bronze.
Is that possible? Thanks.
 
Do you mind if I ask why this is necessary? What I mean is why are you working with the string in the first place, and not just the value returned from item.sword_bronze?

You could easily associate the two things using a map, if you need to.
 

Anixias

Member
Do you mind if I ask why this is necessary? What I mean is why are you working with the string in the first place, and not just the value returned from item.sword_bronze?

You could easily associate the two things using a map, if you need to.
I was adding items in the game by manually copy-pasting tons of code and scripts per item and want to delete this system and switch to loading external files into the game at start. This way, to add a new item, I can copy an existing item file and change a couple of stats. I use an enum item to return the ID of any given item. I have a script that, given an item name, returns the value in the enum. However, I am attempting to remove hard-coded values like this, and switch to a mod-friendly and developer-friendly approach with the files, as new items cannot be predicted and hard-coded into the game. I MUST read the name from the item file, and convert said name to a new ID.

Actually, after reading that, I would have to hard-code new items into the enum itself. :( So I need to change my approach...
Maybe manually define an item ID in each item file? And on item loads, check to make sure this item ID is unused, and if it is, throw an error?
 
WOAH:

I had the exact same problem. I have item.sword_bronze too, can you believe it?
https://forum.yoyogames.com/index.php?threads/string-input-for-an-enumerator.11951/

The answer is no, you need to hard code per case if you want to convert a string to enumerator.

I would try initializing all of the items one at a time, using an incriminating variable for the item id. I'm storing my items in a ds_grid index where each row is an item, and each column holds a piece of information about that particular item.

If you use a grid, you might even be able to use a string as an item id... hm, that's interesting...

Also, PM me if you want, we seem to be working on very similar projects, it'd be kind of cool to compare ideas.
 

GMWolf

aka fel666
Use maps instead. They will do what you need.
Yeah. As i said, use maps.
With maps you can have the key be the string, and the value be the item index.
Ie, items[? "Bronze sword"] = 1;

Better yet: have your file be in the json format. That way, you can load your file easily into a map directly, and store that into a map of strings.
So upon loading an item file, do something like this:
Code:
var json = //read all text from file
var map = json_decode(json);
var itemName= map[? "name"];
items[? itemName] = map;
Your item files could look like this:
Code:
{
 "name": "bronze sword",
 "strength": "52",
 "Colour": "bronze coloured",
 "attack speed" : 4
}
As you can see, this makes loading files really easy, and allows you to reference your items with strings.
 

Anixias

Member
Yeah. As i said, use maps.
With maps you can have the key be the string, and the value be the item index.
Ie, items[? "Bronze sword"] = 1;

Better yet: have your file be in the json format. That way, you can load your file easily into a map directly, and store that into a map of strings.
So upon loading an item file, do something like this:
Code:
var json = //read all text from file
var map = json_decode(json);
var itemName= map[? "name"];
items[? itemName] = map;
Your item files could look like this:
Code:
{
 "name": "bronze sword",
 "strength": "52",
 "Colour": "bronze coloured",
 "attack speed" : 4
}
As you can see, this makes loading files really easy, and allows you to reference your items with strings.
I like this approach. Thank you, truly.
 
Top