GM:S 1.4 Can you help with skipping string_insert()?

Discussion in 'Programming' started by Edwin, May 28, 2019.

  1. Edwin

    Edwin Member

    Joined:
    Jul 15, 2018
    Posts:
    387
    I need to use string_insert() function with specific variable that keeps the position of where I need to insert my string with string_insert(). The string what I'm gonna insert is an alpha tag. Other script will delete this tag and make the text transparent from position keeping variable to end. In this simple way, I will make the typewriter effect.

    The problem is that I've failedly tried to create the script that gonna skip my other tags and insert an alpha tag using string_insert() function.

    So my script looks like this:
    Code:
    /// string_insert_tag(substr,str,index)
    
    // Declare variables
    var substr, str, index, cst, cet, cct, l, cstl, i;
    
    // Set variables
    substr = argument[0];
    str = argument[1];
    index = round(argument[2]);
    
    cst = '[c=';
    cet = ']';
    cct = '[/c]';
    
    l = string_length(str);
    cstl = string_length(cst);
    
    // To get the string inside the tags
    for (var p = 1; p < l; p ++) {
       if (string_char_at(str, p) == "[" && string_char_at(str, p+1) == "c" && string_char_at(str, p+2) == "=") {
            i = string_copy(str, p + cstl, string_pos(cct, string_delete(str, 1, p + cstl)));
       }
    }
    
    // Return the string with substring
    return string_insert(substr, str, index);
    
    Tags that script should skip are [c=...] and [/c]. Please answer if you know how to implement this. I tried to make it by my own but did not work.

    Thank you for your care.
     
  2. jo-thijs

    jo-thijs Member

    Joined:
    Jun 20, 2016
    Posts:
    2,844
    It looks like you had the right idea, right up until this line:
    Code:
            i = string_copy(str, p + cstl, string_pos(cct, string_delete(str, 1, p + cstl)));
    You assign a value to i, but you never do anything else with this variable.
    This makes it so the for loop never does anything.
    Try this:
    Code:
    /// string_insert_tag(substr,str,index)
    
    // Declare variables
    var substr, str, index, cst, cet, cct, l, cstl, cctl;
    
    // Set variables
    substr = argument[0];
    str = argument[1];
    index = round(argument[2]);
    
    cst = '[c=';
    cet = ']';
    cct = '[/c]';
    
    l = string_length(str);
    cstl = string_length(cst);
    cctl = string_length(cct);
    
    // To get the string inside the tags
    for (var p = 1; p <= l - cstl; p ++) {
       if (string_copy(str, p, cstl) == cst) {
            var tag_length = string_pos(cct, string_delete(str, 1, p + cstl - 1)) + cctl - p;
            str = string_delete(str, p, tag_length);
            l -= tag_length;
            p --;
            continue;
       }
    }
    
    // Return the string with substring
    return string_insert(substr, str, index);
    
     
    Edwin likes this.
  3. Edwin

    Edwin Member

    Joined:
    Jul 15, 2018
    Posts:
    387
    Thank you for replying.

    I know that my code doesn't do anything. I simply just took the i function to get the cet and string whatever inside the tag.
     
  4. Edwin

    Edwin Member

    Joined:
    Jul 15, 2018
    Posts:
    387
    Hello again, I think you didn't understand my question correctly. Your script deletes the tag and value inside it, but I mean a script that inserts substring and skips certain strings (literally skips).

    Something like this:
    [​IMG]

    It still not perfect because it can not skip the [/c] tag.

    So this is code that I made:
    Code:
    /// string_insert_tag(substr,str,index)
    
    // Declare variables
    var substr, str, index, cst, cet, cct, l, cstl, cctl, tag_string, tag_length;
    
    // Set variables
    substr = argument[0];
    str = argument[1];
    index = round(argument[2]);
    
    cst = '[c=';
    cet = ']';
    cct = '[/c]';
    
    l = string_length(str);
    cstl = string_length(cst);
    cctl = string_length(cct);
    
    // Get symbols that inside the tags
    for (var p = 1; p <= l; p ++) {
        if (string_copy(str, p, cstl) == cst) {
            i = string_copy(str, p + cstl, string_pos(cct, string_delete(str, 1, p + cstl)));
            if (index > string_pos(cst, str)) {
                return string_insert(substr, str, (index + string_length(i) - cstl) - 1);
            } else {
                return string_insert(substr, str, index);
            }
        }
    }
    
    If there are comments, I’ll be happy to hear them
     
  5. jo-thijs

    jo-thijs Member

    Joined:
    Jun 20, 2016
    Posts:
    2,844
    Oh, I indeed misinterpreted what you asked for.

    Try this instead:
    Code:
    /// string_insert_tag(substr,str,index)
    
    // Declare variables
    var substr, str, index, cst, cet, cct, l, cstl, cctl, tag_string, tag_length;
    
    // Set variables
    substr = argument[0];
    str = argument[1];
    index = round(argument[2]);
    
    cst = '[c=';
    cet = ']';
    cct = '[/c]';
    
    l = string_length(str);
    cstl = string_length(cst);
    cctl = string_length(cct);
    
    // Get symbols that inside the tags
    var p = 0;
    while (index > 0) {
        if (string_copy(str, p, cstl) == cst) {
            p += cstl;
            p += string_pos(cet, string_delete(str, 1, p - 1));
        } else if (string_copy(str, p, cctl) == cct) {
            p += cctl;
        } else {
            p ++;
            index --;
        }
    }
    
    return string_insert(substr, str, p);
    
     
    Edwin likes this.
  6. Edwin

    Edwin Member

    Joined:
    Jul 15, 2018
    Posts:
    387
    Edit: oops sorry my mistake...

    Thank you, I'll check it out.
     
  7. Edwin

    Edwin Member

    Joined:
    Jul 15, 2018
    Posts:
    387
    Oh yeah, that works! Thank you so much again!
     
  8. Edwin

    Edwin Member

    Joined:
    Jul 15, 2018
    Posts:
    387
    Hey, I don't want to disturb you but if you aren't busy, I tried to add other tags except [c=..] but it caused some inaccuracies in the inserting position.

    So I simply copypasted that loop you send me.
    Code:
    /// string_insert_tag(substr,str,index)
    
    // Declare variables
    var substr, str, index, cst, cet, cct, ast, aet, act, l, cstl, cctl, astl, actl, p;
    
    // Set variables
    substr = argument[0];
    str = argument[1];
    index = round(argument[2]);
    
    cst = '[c=';
    cet = ']';
    cct = '[/c]';
    
    ast = '[a=';
    aet = ']';
    act = '[/a]';
    
    l = string_length(str);
    
    cstl = string_length(cst);
    cctl = string_length(cct);
    
    astl = string_length(ast);
    actl = string_length(act);
    
    p = 0;
    
    // Skip colour tag
    while (index > 0) {
        if (string_copy(str, p, cstl) == cst) {
            p += cstl;
            p += string_pos(cet, string_delete(str, 1, p - 1));
        } else
        if (string_copy(str, p, cctl) == cct) {
            p += cctl - 1;
        } else {
            p ++;
            index --;
        }
    }
    
    // Skip alpha tag
    while (index > 0) {
        if (string_copy(str, p, astl) == ast) {
            p += astl;
            p += string_pos(aet, string_delete(str, 1, p - 1));
        } else
        if (string_copy(str, p, actl) == act) {
            p += actl - 1;
        } else {
            p ++;
            index --;
        }
    }
    
    return string_insert(substr, str, p);
    
    I also tried to merge alpha and colour tags skipping inside
    Code:
    while (index > 0) {
        // ...
    }
    
    this while loop but that drove the game crazy.
     
  9. jo-thijs

    jo-thijs Member

    Joined:
    Jun 20, 2016
    Posts:
    2,844
    It needs to be one loop:
    Code:
    /// string_insert_tag(substr,str,index)
    
    // Declare variables
    var substr, str, index, cst, cet, cct, ast, aet, act, l, cstl, cctl, astl, actl, p;
    
    // Set variables
    substr = argument[0];
    str = argument[1];
    index = round(argument[2]);
    
    cst = '[c=';
    cet = ']';
    cct = '[/c]';
    
    ast = '[a=';
    aet = ']';
    act = '[/a]';
    
    l = string_length(str);
    
    cstl = string_length(cst);
    cctl = string_length(cct);
    
    astl = string_length(ast);
    actl = string_length(act);
    
    p = 0;
    
    // convert "index" (the logical index, the one from the input) to "p" (the internal index, of the actual representation)
    while (index > 0) {
        // Skip colour tag
        if (string_copy(str, p, cstl) == cst) {
            p += cstl;
            p += string_pos(cet, string_delete(str, 1, p - 1));
        } else
        if (string_copy(str, p, cctl) == cct) {
            p += cctl - 1;
        } else
        // Skip alpha tag
        if (string_copy(str, p, astl) == ast) {
            p += astl;
            p += string_pos(aet, string_delete(str, 1, p - 1));
        } else
        if (string_copy(str, p, actl) == act) {
            p += actl - 1;
        } else
        // Count single character
        {
            p ++;
            index --;
        }
    }
    
    return string_insert(substr, str, p);
     
    Edwin likes this.

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