GML Get Built in GM Color Name from String and Translate to Use in draw_set_color

I'm working on textbox that allows for different colored text in one message. I draw each letter one by one and repeatedly loop each frame. I want to be able to change part of a string's color by writing something like /c_red/ in the text and checking for /c and then getting the rest of the color name with a while loop until I find the second / symbol. Once I have a string of the color name with the end and beginning slashes removed how do I put this string of the predefined gm color name into draw_set_color() translating the string to what it needs to be so it does not hang, and recolors the text. Is it possible, and is there a better way to do this?
 

Yal

🐧 *penguin noises*
GMC Elder
How about this?
Code:
switch(input){
 case "c_red": return c_red;
 case "c_blue": return c_blue;


 /*and so on for every color*/
  default: return c_gray;
}
 
How about this?
Code:
switch(input){
 case "c_red": return c_red;
 case "c_blue": return c_blue;


 /*and so on for every color*/
  default: return c_gray;
}
Does that mean there's no way to turn the string name into into a predefined Gm color value even if it has the same name as the value? I was hoping on not having to hard code every individual color into logic, and just put any predefined color names in the string that I wanted at the time. If that's impossible I do want to know though.
 

Yal

🐧 *penguin noises*
GMC Elder
Does that mean there's no way to turn the string name into into a predefined Gm color value even if it has the same name as the value?
A string is fundamentally different from a hexadecimal color value, no matter what human-friendly name is used to refer to it in the IDE... you could've done this in GM6 / GM8 when it was still interpreted (since you could construct arbitrary strings and execute them as code) but that functionality opened a whole other can of worms and also made game performance much worse.


There's actually another way you could do this that's slightly less hardcoded, though... a ds_map data structure can use string keys, so you could create associations for every possible string in advance and then use the map to get the values.

Something like this to set values:

global.colorstringmap[? "c_red"] = c_red;


And something like this to use them:

draw_set_color(global.colorstringmap[? input_color]);
 
A string is fundamentally different from a hexadecimal color value, no matter what human-friendly name is used to refer to it in the IDE... you could've done this in GM6 / GM8 when it was still interpreted (since you could construct arbitrary strings and execute them as code) but that functionality opened a whole other can of worms and also made game performance much worse.
Ah so that's what the fuss about string execute was. While I'm at it, just to be sure, can you look up a variable either local, global, or built in from a string name that's the name of the variable eg(hp, or status) or does that require hard code for each and every one too. Is it possible to look these up by number instead?
 

Yal

🐧 *penguin noises*
GMC Elder
As far as I know, the only stuff you can natively look up from its name is game assets (sprites, scripts etc, using the function asset_get_index), variables aren't accessed internally by their names anymore but by memory references, so there's no way to map a string to a variable unless you do it yourself.

You might be interested in investigating using an array or such if you need to dynamically access data, it's what they're meant for.
 
As far as I know, the only stuff you can natively look up from its name is game assets (sprites, scripts etc, using the function asset_get_index), variables aren't accessed internally by their names anymore but by memory references, so there's no way to map a string to a variable unless you do it yourself.
.
Thanks for answering my questions more than just once, like some people do.

P.S. I think a while back you said you did an object spawning/ de-spawning system different than the built in deactivation method. I'd be interested, but don't know exactly where it is.
 
Last edited:
Studio 2 added back the ability to check for the existence of lical/global variables, though this will still not solve your original issue, as the color codes are constants. I would still advise against using them for most applications, as for one, it's bad programming practice, and for another, depending on how it works under the hood, it may not be very efficient.
 
Top