1. Hello Guest! It's with a heavy heart that we must announce the removal of the Legacy GMC Archive. If you wish to save anything from it, now's the time! Please see this topic for more information.
    Dismiss Notice

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

Discussion in 'Programming' started by 2d_warrior, Jun 14, 2017.

  1. 2d_warrior

    2d_warrior Member

    Joined:
    Jul 14, 2016
    Posts:
    201
    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?
     
  2. Yal

    Yal GMC Memer GMC Elder

    Joined:
    Jun 20, 2016
    Posts:
    3,958
    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;
    }
    
     
  3. 2d_warrior

    2d_warrior Member

    Joined:
    Jul 14, 2016
    Posts:
    201
    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.
     
  4. Yal

    Yal GMC Memer GMC Elder

    Joined:
    Jun 20, 2016
    Posts:
    3,958
    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]);
     
    2d_warrior likes this.
  5. 2d_warrior

    2d_warrior Member

    Joined:
    Jul 14, 2016
    Posts:
    201
    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?
     
  6. Yal

    Yal GMC Memer GMC Elder

    Joined:
    Jun 20, 2016
    Posts:
    3,958
    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.
     
    2d_warrior likes this.
  7. 2d_warrior

    2d_warrior Member

    Joined:
    Jul 14, 2016
    Posts:
    201
    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: Jun 14, 2017
  8. stainedofmind

    stainedofmind Member

    Joined:
    Jun 20, 2016
    Posts:
    701
    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.
     

Share This Page

  1. This site uses cookies to help personalise content, tailor your experience and to keep you logged in if you register.
    By continuing to use this site, you are consenting to our use of cookies.
    Dismiss Notice