GMS 2 Parts of a string not being removed

Misael644

Member
I am creating a function that serves as a textbox generator. The function works like this:
GML:
//the function
dialogue_create("Hello,/60 /03this is a test!","character",0,"left",3);

//description
/// dialogue_create(text,character,expression,pos,speed)
/// @arg text
/// @arg character
/// @arg expression
/// @arg pos
/// @arg speed

//arguments
var text = argument0;
var char = argument1;
var expr = argument2;
var pos = argument3;
var txtspeed = argument4;
Note that in the middle of this text there are some numbers preceded by slashes. They serve to change the speed in the middle of the text.
Here's how the text formation part of this function works:
GML:
//le texto
global.DebugText = text;

if obj_game.TextInit == 1 {
    global.count = 0;
    obj_game.alarm[0] = 1;
    obj_game.TextInit = 0;
};

var CharCheck = string_copy(text,global.count+1,1);
if CharCheck == "/" {
    global.Bar = global.count+2;
    global.Text2Remove = string_copy(text,global.count+1,3);
    if obj_game.alarm[1] == -1 {obj_game.alarm[1] = 1};;
};


if obj_game.BarPosCount == 0 {
    var text2 = text;
} else {
    for(var i = 0; i < obj_game.BarPosCount; i += 1){
        var text2 = string_replace_all(text,obj_game.TxtRemove[i],"");
    };
};

if global.SpeedChange == false {
    global.txtspeed = txtspeed;
} else {
    global.txtspeed = real(string_copy(global.txt,global.Bar,2));
};

var textfinal = string_copy(text2,0,global.count);
global.txt = text;
global.txt2 = string_copy(text,0,string_length(text)-3*obj_game.BarPosCount);

draw_set_halign(fa_left);
draw_set_valign(fa_top);

draw_text_ext(40,((room_height/4)*3)+10,string(textfinal),20,room_width-80);
And here are the two alarms that participate in the function:
GML:
//Alarm 0
/// @desc Typewriter effect
global.count += 1;

if string_length(string_copy(global.txt2,0,global.count)) < string_length(global.txt2) {
    alarm[0] = global.txtspeed;
};

//Alarm 1
/// @description Remove slashes
if BarPosCount > 0 {
    if BarPos[BarPosCount-1] != global.Bar {
        BarPos[BarPosCount] = global.Bar;
        TxtRemove[BarPosCount] = global.Text2Remove;
        BarPosCount += 1;
        global.SpeedChange = !global.SpeedChange;
    };
} else {
    BarPos[BarPosCount] = global.Bar;
    TxtRemove[BarPosCount] = global.Text2Remove;
    BarPosCount += 1
    global.SpeedChange = !global.SpeedChange;
};
Note: I ended up calling the slashes "bars" in the code because of my English.

The typewriter effect part works ok. The problem is that when I need to use more than two numbers preceded by slashes to set the speed, not all numbers are erased correctly. Here is an example:
1637710210113.png
So, what am I doing wrong to make the code work correctly?
 

Misael644

Member
Seems like you need to run the function again on what's left of the string.
You mean run the function one more time to erase what's left in the string? Well... this was supposed to happen in real time in theory... Even if I do this, a part of the code will be missing because of the character limit set by the global.count variable.
 

Nidoking

Member
I mean you need to think about what you want the function to do and when, and tell it to do that. This whole thing with using global variables to track values related to an input is just asking for trouble. If you're really going to do what you seem to want to do, you should probably look at each character in the string as you go, and when you find a control character, process the following control and then move on from there. Any limit set by your own variables is a limit you set with your own variables, so you can unset it whenever you choose. You've designed functions specifically to process a single control embedded in a string, so that's what they do. If you want to process more than one control, you need to plan for that.
 

Misael644

Member
By the way, I also tried using this code to remove the numbers:
GML:
if obj_game.BarPosCount == 0 {
    var text2 = text;
} else {
    for(var i = 0; i < 100; i += 1){
        var numberss = string_replace(string_format(i,2,0)," ","0");
        var text2 = string_replace_all(text,"/"+string(numberss),"");
    };
};
Even so, there will be no effect on the string, it will stay the same way it was input into the function. What could I be doing wrong?
 

Nidoking

Member
I wrote an excellent answer to your questions, and then deleted it. Then I wrote another excellent answer, and deleted that too. I proceeded to write a total of one hundred brilliant lessons for you, and deleted every one of them and replaced them with the post you're reading now. That is what you're doing wrong.

If you can't figure out how to determine if the next character of a string is a slash and then read the digits from the start of the rest of the string, please find another way to encode what you're trying to encode. You're doing things in a very wrong way, then trying to solve the wrong problem and making even more of a mess.
 
Top