Pages: [1]
  Print  
Author Topic: [GML] Word-breaking, multi-line RPG chat script  (Read 2400 times)
Offline (Male) kkg
Posted on: February 12, 2011, 01:52:29 AM

Member
Location: Australia
Joined: Nov 2009
Posts: 84
MSN Messenger - kamikazigames@gmail.com
View Profile Email
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;
    }
}
« Last Edit: February 16, 2011, 12:57:44 AM by kkg » Logged
PC: Core i7-2600 @ 3.8ghz | 4x 4gb G.Skill RipjawZ DDR3-2000 | GTX580 | Win7 x64
Time is the greatest teacher, however it kills every single one of its pupils.
Offline (Male) RetroX
Reply #1 Posted on: February 12, 2011, 09:47:56 AM

Master of all things Linux
Contributor
Location: US
Joined: Apr 2008
Posts: 1055
MSN Messenger - classixretrox@gmail.com
View Profile Email
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?
Logged
My Box: Phenom II 3.4GHz X4 | ASUS ATI RadeonHD 5770, 1GB GDDR5 RAM | 1x4GB DDR3 SRAM | Arch Linux, x86_64 (Cube) / Windows 7 x64 (Blob)
Quote from: Fede-lasse
Why do all the pro-Microsoft people have troll avatars? :(
Offline (Unknown gender) luiscubal
Reply #2 Posted on: February 12, 2011, 09:54:09 AM
Member
Joined: Jun 2009
Posts: 452

View Profile Email
@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.
Logged
Offline (Male) RetroX
Reply #3 Posted on: February 12, 2011, 11:11:36 AM

Master of all things Linux
Contributor
Location: US
Joined: Apr 2008
Posts: 1055
MSN Messenger - classixretrox@gmail.com
View Profile Email
Eh, probably.
Logged
My Box: Phenom II 3.4GHz X4 | ASUS ATI RadeonHD 5770, 1GB GDDR5 RAM | 1x4GB DDR3 SRAM | Arch Linux, x86_64 (Cube) / Windows 7 x64 (Blob)
Quote from: Fede-lasse
Why do all the pro-Microsoft people have troll avatars? :(
Offline (Male) kkg
Reply #4 Posted on: February 12, 2011, 12:11:11 PM

Member
Location: Australia
Joined: Nov 2009
Posts: 84
MSN Messenger - kamikazigames@gmail.com
View Profile Email
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
Logged
PC: Core i7-2600 @ 3.8ghz | 4x 4gb G.Skill RipjawZ DDR3-2000 | GTX580 | Win7 x64
Time is the greatest teacher, however it kills every single one of its pupils.
Pages: [1]
  Print