Envision, Create, Share

Welcome to HBGames, a leading amateur game development forum and Discord server. All are welcome, and amongst our ranks you will find experts in their field from all aspects of video game design and development.

String Paragraphs?

Is it possible to make a string to have more than one line of text?
If it is possible, how can I do that?
I have tried to use the "\n" method, but it just replaces it with a blank and the string still shows
in one line.


Solved.
 
Well, a string is always just a range of characters. The \n symbol is a commonly used marker to tell the interpreting code to put in a line break, however you have to program that. Since you can't just replace \n's with <br>s like in HTML, but instead need to draw text, you could split at that symbol, like so:

[rgss]string = "Hello\nWorld!"
output = string.split(/\\n/) # populates an array with individual lines, seperated by the \n symbol
for i in 0..output.size
  self.contents.draw_text(line, 4, i * 32, 200, 200, output)
end
[/rgss]
 
Then you need to get size of each letter when you're drawing them on a sprite/window (whatever you're gonna use it for).

If goes something like this:

[rgss] 
def draw_paragraph(bitmap, input_string, start_x  = 0, start_y = 0, letter_spacing = 5)
  x, y = start_x, start_y
  output = string.split(/\\n/)
  output.each{|line|
     for i in 0..line.size - 1
        letter = line[i, 1]
        let_r = bitmap.text_size(letter)
        bitmap.draw_text(x, y, let_r.w, let_r.h, letter)
        x += let_r.width + letter_spacing
 
        if x >= bitmap.width
         x = start_x
         y += let_r.height + letter_spacing
        end
     end
     x = start_x
     y += bitmap.text_size(line).height + letter_spacing
  }
end
 
[/rgss]

Something like this. Hope I gave you idea what to do next. :)
 
MarioSuperStar":2nclhlw1 said:
I had to put "\n" instead of the backslashes, but it worked.
Oh, really? I thought split uses a regular expression pattern... oops ^^"

And yeah, getting the letter size is a cool thing if you want automatic line breaks, letter-by-letter systems or the like. If you know where your line breaks are going to be, that is not necessary.
Coming to think of it, there is a quite awesome method by Trickster (out of his MACL) that, among others, my latest Multiple Language script takes advantage of. It's pretty simple and straightforward.

Code:
class Bitmap

  #-------------------------------------------------------------------------

  def draw_wrap_text(x, y, width, height, text, lineheight)

    strings = text.split

    strings.each do |string|

      word = string + ' '

      word_width = text_size(word).width

      x, y = 0, y + lineheight if x + word_width > width

      self.draw_text(x, y, word_width, height, word)

      x += word_width

    end

  end

  #-------------------------------------------------------------------------

end
 

Thank you for viewing

HBGames is a leading amateur video game development forum and Discord server open to all ability levels. Feel free to have a nosey around!

Discord

Join our growing and active Discord server to discuss all aspects of game making in a relaxed environment. Join Us

Content

  • Our Games
  • Games in Development
  • Emoji by Twemoji.
    Top