Breaking lines in strings at the appropriate spots

Questions about the LÖVE API, installing LÖVE and other support related questions go here.
Forum rules
Before you make a thread asking for help, read this.
Post Reply
User avatar
baconhawka7x
Party member
Posts: 491
Joined: Mon Nov 21, 2011 7:05 am
Location: Oregon, USA
Contact:

Breaking lines in strings at the appropriate spots

Post by baconhawka7x »

I think I posted something similar to this a while ago.

I am making a "speech bubble" system in my game Heroic, that allows all of the npc's to have assigned dialogs that they say.

When you walk up and talk to them by pushing some button, a little rectangular speech window pops up by there face and text is displayed, (This text is there dialog).

So, let's say I have something like this:

Code: Select all

-- Let's say I have a table for each npc's data..
npc_data = {}
npc_data[1] = {name = "Bob", Image = "image/directory/here", dialog = "Hello there, I am Bob!"}
I want to make it, so that if the dialog text is wider than the dialog bubble, that the line breaks. I have been looking at lua's string functionality, and found the syntax for breaking a line: /n. Which is probably something everyone knows, but I kind of skipped learning about strings in lua when I was first starting (It's Probably the reason I am having this problem!)

So It is possible to do something like this,

Code: Select all

dialog = "Hello there, /n I am bob"
But I was wondering if there was a way to automate it? Just so that the game can break the lines for me.


I'd Appreciate any and all help, thanks!:D
User avatar
slime
Solid Snayke
Posts: 3133
Joined: Mon Aug 23, 2010 6:45 am
Location: Nova Scotia, Canada
Contact:

Re: Breaking lines in strings at the appropriate spots

Post by slime »

baconhawka7x wrote:I want to make it, so that if the dialog text is wider than the dialog bubble, that the line breaks. I have been looking at lua's string functionality, and found the syntax for breaking a line: /n.
love.graphics.printf will do word-wrapping for you. Also, newlines in strings are \n, not /n. :P
User avatar
baconhawka7x
Party member
Posts: 491
Joined: Mon Nov 21, 2011 7:05 am
Location: Oregon, USA
Contact:

Re: Breaking lines in strings at the appropriate spots

Post by baconhawka7x »

Ah, thank you, also wondering, is there a way to get the total number of lines in a string? I'm planning on making multiple "paged" dialogs if the text is taller than the rectangle. For example, you can push next to see the rest of the dialog.
User avatar
slime
Solid Snayke
Posts: 3133
Joined: Mon Aug 23, 2010 6:45 am
Location: Nova Scotia, Canada
Contact:

Re: Breaking lines in strings at the appropriate spots

Post by slime »

If you want to get the number of newlines ('\n' characters) in a string, you can do something like: local _, count = str:gsub("\n", "\n").
If you want to get how many lines love.graphics.printf will wrap the text to, you can use [wiki]Font:getWrap[/wiki].
User avatar
baconhawka7x
Party member
Posts: 491
Joined: Mon Nov 21, 2011 7:05 am
Location: Oregon, USA
Contact:

Re: Breaking lines in strings at the appropriate spots

Post by baconhawka7x »

I still need just a bit more guidance.

Image

I can figure out how to make a system of pages myself, But what I'm really struggling with is how to select, and move the last few runoff lines.

How would I do that?
User avatar
Robin
The Omniscient
Posts: 6506
Joined: Fri Feb 20, 2009 4:29 pm
Location: The Netherlands
Contact:

Re: Breaking lines in strings at the appropriate spots

Post by Robin »

You can use love.graphics.setScissor with the coordinates of the box before drawing the text, and after drawing the text, call the same function again without arguments.
Help us help you: attach a .love.
Post Reply

Who is online

Users browsing this forum: Bing [Bot] and 228 guests