Problem with putting a String in a String

Status
Not open for further replies.

Evgenij

Veteran
Veteran
Joined
Aug 28, 2013
Messages
349
Reaction score
100
First Language
German
Primarily Uses
N/A
Hi, I wanted to write a function, which gets a text and will automaticly put an \n in the text if the text is bigger than the screen.

Here is the code:

def draw_text_with_n(text, x, y) t = text.split(" ") n_text = "" t.each do |word| line = "" if line.length + word.length < self.width line << "#{word} " else n_text = line + "\n" line.clear end end draw_text_ex(x, y, n_text) endNow i have the Problem that this line: 

line << "#{word} "Makes this:

"This "

"is "

"an "

"example"

But it should look like this: "This is an example"

So my code dont work.

Could someone help me pls?
 
Last edited by a moderator:

Tsukihime

Veteran
Veteran
Joined
Jun 30, 2012
Messages
8,564
Reaction score
3,846
First Language
English
Assuming your input is valid the only problem I can see is this condition not doing what you think it's doing.

Code:
if line.length + word.length < self.width
You should check that your logic/math is correct.
 
Last edited by a moderator:

Napoleon

Veteran
Veteran
Joined
Dec 29, 2012
Messages
869
Reaction score
97
First Language
Dutch
Primarily Uses
I believe that line = "" is placed wrong. You clear the line variable for every word.

Also the return draw_text_ex(x, y, n_text) will bug if you only have 1 line. n_text is "".

Shorter version (doesn't take words into account):

def wrap_long_string(text,max_width = 10)(text.length < max_width) ?text :text.scan(/.{1,#{max_width}}/).join("\n")endhttps://gist.github.com/Amitesh/1160287

Better sample:

Code:
TEXT = "Lorem ipsum dolor sit amet, consectetur adipiscing elit. Donec placerat, lacus et lacinia congue, mi ipsum tempus mauris, id mollis ligula nulla ut ligula. Curabitur vestibulum enim sed ante bibendum sagittis. Donec blandit a mi quis blandit. Sed ultrices, lectus ac tincidunt vestibulum, libero dolor rutrum lectus, vel feugiat felis augue volutpat tortor. Fusce orci velit, malesuada eget quam quis, malesuada ullamcorper lorem. Pellentesque aliquam magna lorem, eu eleifend libero adipiscing quis. Donec posuere laoreet libero, vitae pretium dolor fermentum pulvinar. Praesent bibendum, nunc et auctor porta, velit magna egestas orci, ac varius tellus libero in metus. Pellentesque ac sem at mauris facilisis interdum. Aenean non metus vitae orci sodales aliquam. Aliquam tincidunt turpis a mi congue, ac condimentum libero lacinia. Morbi laoreet rhoncus ipsum, in cursus urna viverra at."def wrap_text(txt, col = 80)  txt.gsub(/(.{1,#{col}})( +|$\n?)|(.{1,#{col}})/,    "\\1\\3\n") end  p wrap_text TEXT, 40p wrap_text 'Hello world. This is a sample line.', 10
 
Last edited by a moderator:

Evgenij

Veteran
Veteran
Joined
Aug 28, 2013
Messages
349
Reaction score
100
First Language
German
Primarily Uses
N/A
There is one other thing which I found out. If the text is too small and never reaches the self.width. It won't be drawn at all^^ Hmm, must look how to fix this.

I have made this to test the condition:

def draw_text_with_n(text, x, y) t = text.split(" ") n_text = "" t.each do |word| line = "" temp = line + "#{word} " p(temp.length) if temp.length < self.width line << "#{word} " else n_text = line + "\n" line.clear end end draw_text_ex(x, y, n_text) endThe console window looks like this(For the word: "The" and "hello": 

4

6

But it should look like this: 10

The text is put verticaly into the variable and I dont understand why..
 
Last edited by a moderator:

Tsukihime

Veteran
Veteran
Joined
Jun 30, 2012
Messages
8,564
Reaction score
3,846
First Language
English
Can we assume self.width is greater than those values? Just from looking at the code, it should only be possible to have line breaks if the length of your temp line is greater than the width.
 

Evgenij

Veteran
Veteran
Joined
Aug 28, 2013
Messages
349
Reaction score
100
First Language
German
Primarily Uses
N/A
Yes self.width is much greater then a word in the array. I think you dont understand what i mean. The problem is not in the condition, the problem is because this

temp = line + "#{word} "

and

line << "#{word} "

dont put the words horizontally in the variables but vertically and I dont know why... 

Haha I found the problem. I have defined line = "" in the loop.
 
Last edited by a moderator:

Another Fen

Veteran
Veteran
Joined
Jan 23, 2013
Messages
564
Reaction score
275
First Language
German
Primarily Uses
Remember that String#length returns a number of characters while Bitmap#width and Window#width (I assume you put your code in) return a number of pixels.
 

As Napoleon mentioned, you reset "line" every time you begin with a new word, so line << "#{word} " does not have any effect.

In addition, "n_text" cannot store more than one line since a new line replaces the previous content.

Considering that, I am surprised your code works so far anyway... ^^
 

Tsukihime

Veteran
Veteran
Joined
Jun 30, 2012
Messages
8,564
Reaction score
3,846
First Language
English
Yes self.width is much greater then a word in the array. I think you dont understand what i mean. The problem is not in the condition, the problem is because this


temp = line + "#{word} "


and


line << "#{word} "


dont put the words horizontally in the variables but vertically and I dont know why...
Are you sure that's what the problem is? Because you can't store strings "vertically".


The reason why your words are coming up like that is either


1: your x,y values are acting funny


2: you have line-breaks after every line


3: possibly something else I am not seeing


Try printing out each text before you draw them.
 
Last edited by a moderator:

Evgenij

Veteran
Veteran
Joined
Aug 28, 2013
Messages
349
Reaction score
100
First Language
German
Primarily Uses
N/A
I didnt seen that napoleon have edited his post(There was only "Never mind"). But this are the problems I have. But the first was solved already I had edited my post before.

edit*

I will use this text_size().width instead of .length
 
Last edited by a moderator:

Napoleon

Veteran
Veteran
Joined
Dec 29, 2012
Messages
869
Reaction score
97
First Language
Dutch
Primarily Uses
I feared that you might overlook it. But forum rules... require me to edit it... But the first post that I made I quickly edited because right after I posted I saw how 'dumb' my original post was haha.
 

Evgenij

Veteran
Veteran
Joined
Aug 28, 2013
Messages
349
Reaction score
100
First Language
German
Primarily Uses
N/A
Ok, thanks all for the help, this is the new code and it works without problems(I haven't seen any atm):

Code:
  def draw_text_with_n(text, x, y)    t = text.split(" ")    n_text = ""    line = ""    t.each do |word|      if text_size(line + word).width < (self.width - 32)        line << "#{word} "      else        n_text += line + "\n"        line = "#{word} "      end    end       n_text = line if n_text == ""     draw_text_ex(x, y, n_text)  end
 

Napoleon

Veteran
Veteran
Joined
Dec 29, 2012
Messages
869
Reaction score
97
First Language
Dutch
Primarily Uses
line << "#{word} " will add one space too much at the end of a line, for each line. Not a biggie but still not 'proper' ;) . You need an extra check.

Word Wrap alternative (added it to my first post, but prints out one \n too much at the end):

Code:
def wrap_text(txt, col = 80)  txt.gsub(/(.{1,#{col}})( +|$\n?)|(.{1,#{col}})/,    "\\1\\3\n") end
 
Last edited by a moderator:

Evgenij

Veteran
Veteran
Joined
Aug 28, 2013
Messages
349
Reaction score
100
First Language
German
Primarily Uses
N/A
Ohh, yes. Thanks
 

Shaz

Veteran
Veteran
Joined
Mar 2, 2012
Messages
40,098
Reaction score
13,704
First Language
English
Primarily Uses
RMMV
This thread is being closed, due to being solved. If for some reason you would like this thread re-opened, please report this post and leave a message why. Thank you.
 
Status
Not open for further replies.

Users Who Are Viewing This Thread (Users: 0, Guests: 1)

Latest Threads

Latest Posts

Latest Profile Posts

Couple hours of work. Might use in my game as a secret find or something. Not sure. Fancy though no? :D
Holy stink, where have I been? Well, I started my temporary job this week. So less time to spend on game design... :(
Cartoonier cloud cover that better fits the art style, as well as (slightly) improved blending/fading... fading clouds when there are larger patterns is still somewhat abrupt for some reason.
Do you Find Tilesetting or Looking for Tilesets/Plugins more fun? Personally I like making my tileset for my Game (Cretaceous Park TM) xD
How many parameters is 'too many'??

Forum statistics

Threads
105,864
Messages
1,017,056
Members
137,573
Latest member
nikisknight
Top