Chaos Project

RPG Maker => RPG Maker Scripts => Topic started by: legacyblade on June 24, 2010, 05:55:10 pm

Title: Get the width of a line of text
Post by: legacyblade on June 24, 2010, 05:55:10 pm
I'm working on a (very VERY simple) message system for my game. And I've gotten to the hardest part. How do I find out the width of a string of text? It isn't crucial or anything, but I'd hate to have a huge window to display a single sentence (such as "alright..."). Anyone have ANY idea how I'd go about this?
Title: Re: Get the width of a line of text
Post by: WhiteRose on June 24, 2010, 08:29:55 pm
Quote from: legacyblade on June 24, 2010, 05:55:10 pm
I'm working on a (very VERY simple) message system for my game. And I've gotten to the hardest part. How do I find out the width of a string of text? It isn't crucial or anything, but I'd hate to have a huge window to display a single sentence (such as "alright..."). Anyone have ANY idea how I'd go about this?

Have you looked at Ccoa's UMS? It has a feature that does this. If you examine the code, you'll probably be able to figure out how he (/she/them/it) made it work.
Title: Re: Get the width of a line of text
Post by: G_G on June 24, 2010, 08:41:38 pm
try this
width = self.bitmap.text_size(string here).width

Or self.contents if you're making a window class
Title: Re: Get the width of a line of text
Post by: legacyblade on June 25, 2010, 02:35:36 am
@WhiteRose, Ccoa's UMS is a HUGE script with tons of options, so it's hard to figure out how she did stuff. I already tried looking through hers :P Thanks for the suggestion though.

@g_g, that worked perfectly :D I didn't know it would be that simple. I found a way to scan through each line of code and get which one is the longest (and it's length in one go!)

@text.each{|string|width = [self.bitmap.text_size(string).width, width].max}


(width is used as the variable for use when creating the overall width of the window. Height is easy, as you just need to multiply the amount of lines by however far apart each line is). Cool how simple it is, no? Thanks again :D

@earthine, that'll be a useful reference. Thanks :D