1. Hey! Guest! The 35th GMC Jam will take place between November 28th, 12:00 UTC - December 2nd, 12:00 UTC. Why not join in! Click here to find out more!
    Dismiss Notice
  2. 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

GMS 2 Special characters in a string?

Discussion in 'Programming' started by Kiwi, Mar 18, 2019.

  1. Kiwi

    Kiwi Member

    Joined:
    Aug 31, 2016
    Posts:
    5
    I'm fairly new to GML, so this question may sound a little nooby. It's also my first post! :)

    So I've made a typer object which types out a string, letter by letter, which works fine. I've also made it so that when it detects a certain character, it does some kind of action. Like pausing or changing colour or whatever.

    Code:
    switch(string_char_at(msg[page], count)){
       
           case ".":
               stopped = true;
               alarm[0] = 15; 
               break;
    
    While the above works, I thought it'd be better if I could do some kind of rich text thing, where it removes the special symbols from the actual text but still carries out the action.

    Code:
    msg[0] = "Hello, !3 this is a test"; 
    where !3 indicates a delay for 3 seconds. !5 could be 5 seconds, !10 could be 10, etc.

    How would I go about this? :/
     
  2. NightFrost

    NightFrost Member

    Joined:
    Jun 24, 2016
    Posts:
    1,981
    Exclamation mark might not be the best choice as you may need it in normal text. Use something less likely such as pipe symbol (|) or underscore (_). It is best to wrap the special command with the symbol so you know exactly where it starts and ends, like |100|. Then you can write their handling with roughly the following logic:
    Code:
    1. While writing out text, look for special symbols that start an action.
    2. Upon detecting a special symbol, note its position in string and start another loop from that position onwards to find the end.
    3. Once end is found, copy everything between the symbols and process the command with it. For example, |10| would grab the 10 and set a ten second wait with it.
    4. Cut the command sequence out of the string and continue as normal.
    
    Cutting out the command sequence should be as simple as using string_replace where you replace it with nothing. It only replaces the first found instance of a string, which always should be the command currently being processed.
     
    Kiwi and SnotWaffle Studios like this.
  3. dannyjenn

    dannyjenn Member

    Joined:
    Jul 29, 2017
    Posts:
    568
    GameMaker also supports 16-bit characters... I think you enter them into the string by doing "\u" followed by four hex digits (e.g. "\u0100" for character #256). This could make the parsing a lot easier, since GameMaker considers "\u0100" to be a single character.

    So if you had your string:
    Code:
    msg[0] = "Hello,\u0103 this is a test";
    You could loop through the string, and set up a simple if statement...
    Code:
    if(ord(string_char_at(msg[0],i))<256){
        print(string_char_at(msg[0],i));
    }
    else if((ord(string_char_at(msg[0],i))&$FF00)==1){ // <-- if you don't understand this, you need to do some research on "bit masking"
        delay_for_seconds(ord(string_char_at(msg[0],i))&$00FF);
    }
     
  4. Kiwi

    Kiwi Member

    Joined:
    Aug 31, 2016
    Posts:
    5
    Thanks for the suggestions, I'll play around with it and see how it turns out :)
     

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