Chaos Project

RPG Maker => RPG Maker Scripts => Script Troubleshooting => Topic started by: bigace on February 12, 2013, 01:37:56 pm

Title: [RESOLVED][RGSS] Trying to create a new line with this "\n"
Post by: bigace on February 12, 2013, 01:37:56 pm
Okay so I was trying to get the string to break to the new line at every "\n".

	module ACE
module CORE
MESSAGE ={
# Do you want the message aligned to the right, center, or left.
:align => 0,
# This is a message that will be displayed when you open the shop.
:quote => 'Welcome to the ACE Shopping System by Bigace360! All ' +
'transactions are processed when you exit the shop. \n ' +
'Press Right or Left to increase or reduce by 1. Hold Shift to augment ' +
'the number by 10. Shift + Ctrl for all or none.',
}
end
end

def create_dummy_window
wy = @category_window.y + @category_window.height
dw = 640 - (640 * 2 / 5)
wh = 480 - wy
@dummy_window = Window_Base.new(0, wy, dw, wh)
@gold_window.y = @dummy_window.y
@dummy_window.contents = Bitmap.new(@dummy_window.width - 32,
@dummy_window.height - 32)
dw = @dummy_window.width
intro_message = MESSAGE[:quote]
intro_message.split("\n")
quote = @dummy_window.contents.slice_text(intro_message, dw)
quote.each_with_index do |i, y|
@dummy_window.contents.draw_text(0, y*32, dw, 32, i, MESSAGE[:align])
end
end


However it doesn't work. So then I just the code to this

	module ACE
module CORE
MESSAGE ={
# Do you want the message aligned to the right, center, or left.
:align => 0,
# This is a message that will be displayed when you open the shop.
:quote => 'Welcome to the ACE Shopping System by Bigace360! All ' +
'transactions are processed when you exit the shop. \n ' +
'Press Right or Left to increase or reduce by 1. Hold Shift to augment ' +
'the number by 10. Shift + Ctrl for all or none.',
}
end
end

def create_dummy_window
wy = @category_window.y + @category_window.height
dw = 640 - (640 * 2 / 5)
wh = 480 - wy
@dummy_window = Window_Base.new(0, wy, dw, wh)
@gold_window.y = @dummy_window.y
@dummy_window.contents = Bitmap.new(@dummy_window.width - 32,
@dummy_window.height - 32)
dw = @dummy_window.width
intro_message = MESSAGE[:quote]
quote = @dummy_window.contents.slice_text(intro_message, dw)
quote.split("\n") # quote.puts("\r\n")
quote.each_with_index do |i, y|
@dummy_window.contents.draw_text(0, y*32, dw, 32, i, MESSAGE[:align])
end
end

and I thought it would work but then I get an error:

Quoteprivate method 'split' called for #<Array:0x32cab78>


I know I'm forgetting something but I don't know what.
Title: Re: [RGSS] Trying to create a new line with this "\n"
Post by: KK20 on February 12, 2013, 06:36:00 pm

quote = @dummy_window.contents.slice_text(intro_message, dw) #-> Returns an array
quote.split("\n")                                            #-> quote is no longer a string


I've made an edit to the slice_text method for my Organized Quest System. It uses /n instead of \n though, forgot why I did that.
Spoiler: ShowHide

Quote from: KK20 on June 14, 2012, 07:32:11 pm
I've made an edit for that before. Found it in one of my old scripts:
class Bitmap
  def slice_text(text, width)
    words = text.split(' ')
    return words if words.size == 1
    result, current_text = [], words.shift
    #loop
    words.each_index {|i|
      if self.text_size("#{current_text} #{words[i]}").width > width or words[i] == "/n"
        #Checks if '/n' is the current word.
        if words[i] == "/n"
          result.push(current_text)
          current_text = ""
        else
          result.push(current_text)
          current_text = words[i]
        end
      else
        # True if the previous word was '/n'
        if current_text == ""
          current_text = words[i]
        else
          current_text = "#{current_text} #{words[i]}"
        end
      end
      #Push the last word in
      result.push(current_text) if i >= words.size - 1}
    #end loop
    return result
  end

end
Test string on how to use it:
'I Forgot Today was the day of the big fair! /n /n I was supposed to go and see Lucca\'s new invention.'




Title: Re: [RGSS] Trying to create a new line with this "\n"
Post by: bigace on February 12, 2013, 07:27:37 pm
Cool  :^_^': , If I've known about this modification, I would've been using it a long time ago. Also how come "/n" works and not "\n". :???:
Title: Re: [RGSS] Trying to create a new line with this "\n"
Post by: ThallionDarkshine on February 12, 2013, 08:16:25 pm
Cause that's how someone made the slice_text method.

Edit - Because that's how KK20 modified the method.
Title: Re: [RGSS] Trying to create a new line with this "\n"
Post by: KK20 on February 12, 2013, 08:46:59 pm
I think he meant when you change /n to \n in the code itself.
Now I don't know the whole reasoning behind it (it's all this background stuff and why things work certain ways that I want to learn), but it's a matter of using double and single quotes. If you look in the helpfile, search for String Literals.
Title: Re: [RGSS] Trying to create a new line with this "\n"
Post by: ThallionDarkshine on February 12, 2013, 09:10:48 pm
So its just to allow the use of it in single quoted strings?
Title: Re: [RGSS] Trying to create a new line with this "\n"
Post by: KK20 on February 12, 2013, 09:24:09 pm
Now this has me thinking if
intro_message.split('\n')

would work in his example. I think somewhere in the process, "\n" changes the string while '\n' will keep it as is. I dunno; 90% of the things I learned in Ruby has just been from experimenting.
Title: Re: [RGSS] Trying to create a new line with this "\n"
Post by: Blizzard on February 13, 2013, 02:24:44 am
'\n' does not work, you have to use "\n". That's because single-quoted strings are raw strings for increased performance, but they don't support embedding or escape sequences.
Title: Re: [RGSS] Trying to create a new line with this "\n"
Post by: KK20 on February 13, 2013, 02:41:55 am
So is my edit practical or is there a better way? :\
Title: Re: [RGSS] Trying to create a new line with this "\n"
Post by: Blizzard on February 13, 2013, 02:50:07 am
I don't think your way works, judging from the code.
But here is how it should work:

1. Split the original string by "\n".
2. Iterate through the new array of strings and call slice_text on each. Put each result into a new array.
3. Now iterate through all the strings in the new array and render them.

EDIT: If this is actually necessary, that is. I don't remember if I added \n support in Bitmap#slice_text back then.
Title: Re: [RGSS] Trying to create a new line with this "\n"
Post by: KK20 on February 13, 2013, 06:58:29 pm
Code

Edit: Oh, wait...forgot to test it some more...

2nd Edit: Okay, now I think I got it.
class Bitmap
 
  # Blizzard's slice_text method. This method can be removed if you have another
  # script that already uses it.
  def slice_text(text, width)
    lines = text.split("\n")
    result = []
    lines.each{|text_chunk|
      words = text_chunk.split(' ')
      current_text = words.shift
      if words.empty?
        result.push((current_text == nil ? "" : current_text))
        next
      end
      words.each_index {|i|
        if self.text_size("#{current_text} #{words[i]}").width > width
          result.push(current_text)
          current_text = words[i]
        else
          current_text = "#{current_text} #{words[i]}"
        end
        result.push(current_text) if i >= words.size - 1
      }
    }
    return result
  end
end
Title: Re: [RGSS] Trying to create a new line with this "\n"
Post by: bigace on February 14, 2013, 01:32:25 pm
Quote from: ThallionDarkshine on February 12, 2013, 08:16:25 pm
Cause that's how someone made the slice_text method.

Edit - Because that's how KK20 modified the method.

Ya, that's not even what I asked

Quote from: KK20 on February 13, 2013, 06:58:29 pm
Code

Edit: Oh, wait...forgot to test it some more...

2nd Edit: Okay, now I think I got it.
Spoiler: ShowHide
class Bitmap
 
  # Blizzard's slice_text method. This method can be removed if you have another
  # script that already uses it.
  def slice_text(text, width)
    lines = text.split("\n")
    result = []
    lines.each{|text_chunk|
      words = text_chunk.split(' ')
      current_text = words.shift
      if words.empty?
        result.push((current_text == nil ? "" : current_text))
        next
      end
      words.each_index {|i|
        if self.text_size("#{current_text} #{words[i]}").width > width
          result.push(current_text)
          current_text = words[i]
        else
          current_text = "#{current_text} #{words[i]}"
        end
        result.push(current_text) if i >= words.size - 1
      }
    }
    return result
  end
end



Cool this works, thanks for help KK20.