ENIGMA Forums

Contributing to ENIGMA => Function Peer Review => Topic started by: kkg on February 12, 2011, 01:52:29 AM

Title: [GML] Word-breaking, multi-line RPG chat script
Post by: kkg on February 12, 2011, 01:52:29 AM
Okay. So I have a function here that I made by myself, but I have a feeling it is unhealthily inefficient.
Basically, It is a function that takes the string you give it and the maximum length of characters allowed on a line, and breaks the message up into segments that will fit into two lines. If it goes over two lines of text, it creates another portion of it that will carry over to the next "message".
Example:
Max allowed characters: 10
String = "Here is a typical message."
This will output:
string1: "Here is a#typical"
string2: "message."

Typical was placed on the second line because although 10 characters had not been reached on the first line, it was a whole word in itself (makes sense obviously)

ANYWAY, here is the script I'm using (feel free to optimise it and/or write your own in replacement :p)
Code: (GML) [Select]
///scr_text_filter_word(string text, real character_line_limit)
text = argument0;
var maxlength, textlength, usestring, usetext, i, currentpos, endword, newline, attemptbreak;
maxlength = argument1;
usetext = text;
usestring = "";
done = 0;
ii = 0;
currentpos = 1;
endword = 0;
newline = 1;
attemptbreak=0;

while (done == 0) {
    attemptbreak=0;
    if(string_length(usetext) > maxlength) {
        for (i = 1; i <= string_length(usetext); i += 1) {
            if(attemptbreak == 1) break;
            currentpos = i;
            if(string_char_at(usetext,i) == " ") {
                for (iii = 1; iii < string_length(usetext)-currentpos; i+=1) {
                    if(string_char_at(usetext,i+iii) == " ") {
                        endword = iii;
                        break;
                    }
                }
                if(currentpos + endword > maxlength) {
                    var newtext;
                    newtext = string_copy(usetext,1,currentpos);
                    if(newline == 1) {
                        global.g_showmessage[ii+1] = newtext;
                        ii+=1;
                        newline=0;
                    }
                    else {
                        newtext = string_insert("#",newtext,1);
                        global.g_showmessage[ii] = global.g_showmessage[ii]+newtext;
                        newline=1;
                    }
                    usetext = string_copy(usetext,currentpos+1,string_length(usetext)-currentpos);
                    attemptbreak = 1;
                }
            }
                               
        }
    }
    else {
        if(newline == 1) {
            global.g_showmessage[ii+1] = usetext;
            ii+=1;
        }
        else {
            usetext = string_insert("#",usetext,1);
            global.g_showmessage[ii] = global.g_showmessage[ii]+usetext;
        }
                       
        done = 1;
        global.g_msgtot = ii;
    }
}
Title: Re: [GML] Word-breaking, multi-line RPG chat script
Post by: RetroX on February 12, 2011, 09:47:56 AM
It really is unhealthily inefficient.

If a message isn't going to change, why does the computer have to break it up?  Why can't the programmer do this?
Title: Re: [GML] Word-breaking, multi-line RPG chat script
Post by: luiscubal on February 12, 2011, 09:54:09 AM
@RetroX I can see why just providing a function to automatically switch lines(not just on newline, but also when the width is too long to fit in a line) is very helpful, in particular if you have variable messages("Hello " + name). If not integrated, then a separated function would do.
Title: Re: [GML] Word-breaking, multi-line RPG chat script
Post by: RetroX on February 12, 2011, 11:11:36 AM
Eh, probably.
Title: Re: [GML] Word-breaking, multi-line RPG chat script
Post by: kkg on February 12, 2011, 12:11:11 PM
Particularly because the game will mostly external resources in order to update it without needing to provide a new executable. So a function for automatically deciphering these strings for me is pretty necessary.

edit: It's also helpful because I have a window manager that you are able to resize at will (chat, quest logs etc), and it's always good to have the text line-break etc accordingly to the window