(Regular Expression) Letter Count
#1
So long story short, I have this script in my game's message boxes that pauses rendering for a bit after a . ! or ? (A sentence-by-sentence script instead of letter-by-letter) The regular expression that does this is:

Code:
@text.gsub!(/([?.!][^\n\z"])/) { "#{$1}\005[#{$game_system.message.end_delay}]" }

I want to change it so the pause before the next sentence to be based on the length of the sentence. How would I modify this regular expression to capture the sentence length into a variable?
Reply }
#2
The returned value of @text... is that the segment that will print, or the leftover text of the previously saved text (thinking the latter).
I'm at work so Pseudo-Code here:
Code:
old_length=@text.size
@text.gsub(/([?.!][^\n\z"])/) { "#{$1}\005[#{$game_system.message.end_delay}]" }
new_length=@text.size
printed_length = old_length - new_length
Note that I used gsub and not gsub! so I don't actually 'CUT' the string. That would be for later when you do the printing of the message.
Up is down, left is right and sideways is straight ahead. - Cord "Circle of Iron", 1978 (written by Bruce Lee and James Coburn... really...)

[Image: QrnbKlx.jpg]
[Image: sGz1ErF.png]    [Image: liM4ikn.png]    [Image: fdzKgZA.png]    [Image: sj0H81z.png]
[Image: QL7oRau.png]    [Image: uSqjY09.png]    [Image: GAA3qE9.png]    [Image: 2Hmnx1G.png]    [Image: BwtNdKw.png%5B]
  Above are clickable links
Reply }
#3
The gsub! method just replaces all instances of .!? with .\005[4] !\005[4] ?\005[4] which is my control code for "stop text rendering for a bit (4 frames)" it works exactly like all the other special codes for gold, hero name, etc. So printed_length in this case would always return 7.

I need to come up with a different solution, possibly capturing the sentence into $2 and using that as the frames to wait.

Edit: 10 minutes of playing around on http://rubular.com/ got me to /(.+?)([?.!][^\n\z"])/

so
Code:
@text.gsub!(/(.+?)([?.!][^\n\z"])/) { "#{$1}#{$2}\005[#{$1.length}]" }
Works with $2 containing the .!? character and $1 containing the sentence.
Reply }
#4
That could work. Better than having to use an old_style parse (from X to Y) technique. Man, it's been months since I handled @gsub replacement stuff for messages.
Up is down, left is right and sideways is straight ahead. - Cord "Circle of Iron", 1978 (written by Bruce Lee and James Coburn... really...)

[Image: QrnbKlx.jpg]
[Image: sGz1ErF.png]    [Image: liM4ikn.png]    [Image: fdzKgZA.png]    [Image: sj0H81z.png]
[Image: QL7oRau.png]    [Image: uSqjY09.png]    [Image: GAA3qE9.png]    [Image: 2Hmnx1G.png]    [Image: BwtNdKw.png%5B]
  Above are clickable links
Reply }




Users browsing this thread: 1 Guest(s)