Save-Point
(Regular Expression) Letter Count - Printable Version

+- Save-Point (https://www.save-point.org)
+-- Forum: Games Development (https://www.save-point.org/forum-4.html)
+--- Forum: Code Support (https://www.save-point.org/forum-20.html)
+--- Thread: (Regular Expression) Letter Count (/thread-5062.html)



(Regular Expression) Letter Count - MechanicalPen - 03-13-2014

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?


RE: (Regular Expression) Letter Count - DerVVulfman - 03-14-2014

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.


RE: (Regular Expression) Letter Count - MechanicalPen - 03-15-2014

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.


RE: (Regular Expression) Letter Count - DerVVulfman - 03-15-2014

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.