• Hey Guest! Ever feel like entering a Game Jam, but the time limit is always too much pressure? We get it... You lead a hectic life and dedicating 3 whole days to make a game just doesn't work for you! So, why not enter the GMC SLOW JAM? Take your time! Kick back and make your game over 4 months! Interested? Then just click here!

[RESOLVED] Best way to prevent words breaking up while displaying text

W

whale_cancer

Guest
Er, so that title is a mouthful.

Basically, I am using this - along with increment char_count by 1 every step - to display text; that is, to have the text display one character at a time:
Code:
draw_sprite_ext(sprite_index, image_index, x, y, 2, 2, 0, c_white, 1);

display_message = string_copy(message, 0, char_count);

draw_text_ext(x + 40, y, display_message, 8, 128 + 64);
However, when a word is being written out near the end of a line, it will jump to the next line once it is long enough that it won't fit on the line it started out on. This kind of jitter is annoying. Any established ways to do this?

I had a really convoluted way of doing this in an old project, but I'm thinking this is a common enough thing that a best approach has already been established.
 

jo-thijs

Member
You would think so, but not as far as I know.
I've had this problem as well, but the codes I wrote for it aren't that good.

One way of doing it would be to loop through every character of your string, checking the width and adding a line break at the correct spot once the width is too large.
Another way would be to keep track of both the width for every character and the width_ext and every time they don't match, you look back at which previous width does match the current width_ext and add a line break there.
Of course, that way isn't fool proof if the line break splits the string in a shorter piece, line break, longer piece.
You would also need to reset your width every time you come across # that is not a \# or an actual line break.

There are probably people out there that have some scripts for it already, but I couldn't find a good one yet.

PS:
String indices start from 1 in GameMaker, so the 0 in string_copy should be a 1.
It won't change things now, but it might in the future.
 

NightFrost

Member
Can't get to my projects to copypaste any code right now, but whenever I am displaying text that's potentially multiline, I use the following process:
  1. Set current position in textblock as previous position, and position of next space in textblock as current position
  2. Measure length from beginning of textblock to current position
  3. If length is longer than line length, copy from start of textblock to previous position into line array
  4. After copying, remove text from beginning of textblock to previous position (copy from previous position to end back to the same string) and set current/prev positions to match
  5. Loop until done, then add the remaining text as final line, loop didn't copy it as it is shorter than a single line
  6. Timesaving exception at the very beginning: if the entire textblock is shorter than a single line, put it as the first line and skip all the looping and cutting
After this you have all the text in lines array, and you can display it whichever way you want.

EDIT - by calling this in step event (reconstructing lines every step) you can let text display area be altered as you go, and the text refits itself into it - you may need a scrollbar.
 
Last edited:
C

CedSharp

Guest
In my CTB asset, I added features like auto-wrapping and auto-overflow. ( you can ignore last one haha )

1. I parse the string to be displayed and I break it into words ( seperated by space or some punctuations like '-', '.', ',', ':', etc )
2. I then start building my list of lines by measuring the next word to add, and if it fits, add it to current line, otherwise create a new line.
3. I save also the position of each word, but that's to do more stuff with the words, in your case you might not need that.

So yeah, gamemaker's auto-wrap script is good, but because you don't give it the final string, it doesn't know that it has to wrap a word before it's too late.
You have to do that yourself sadly.

Regards,
CedSharp
 
W

whale_cancer

Guest
Alright, seems like the best way to do it in my specific application is to, as some have suggested, loop through each line and find the last space and replace it with a line break. I have a fixed width bitmap font so it should be fairly simple. I am just surprised there isn't some neat way to do it, given this is an extremely common way to display text in a game.

Thanks all!
 
Top