I'm attempting to create an alias to the message window that will remove \L(or R)[ (Some alphanumeric code representing a Character and facial expression) ] from the text of the message, and add/replace/remove the corresponding portrait to the message window on the corresponding side (Left or Right)
I also intend to use a similar method to display the characters' names to the message window (just below the portrait, or on one side if no portrait exists)
But first, I need to understand what I can and cannot do with text.gsub!.
(I'm using this process because I don't want to have to use a custom script call for all my message windows; so I can just add a tag like \n[1]. (Only it would be \L[1,1] for ["First Character","Angry"] or something.))
EDIT: this is what I have thusfar...
text.gsub!(/\\[Ll]\[([0-9]+)\]/) do
<insert/change picture[#] on the left side of the message window>
text.gsub!(/\\[Rr]\[([0-9]+)\]/) do
<insert/change picture[#] on the right side of the message window>
One thing that confuses me is whether or not \[([0-9]+)\] means any number from 0 to 9 or any number composed of digits 0 through 9...
Another thing I'm not sure about is whether I can even do a multi-dimensional array lookup using this... (Remember I want it to work like \L[1,12] )
Also the slashes are a bit confusing...
Ok, first of all, gsub! substitutes all matching substrings and it changes the original string. The other things you're asking about are regular expressions. Zeriab made a tutorial on that. I suggest you check it out. Slashes are used to mark the beginning and the end of the regular expressions. Think of them as double quotes or quotes for strings.
I see... I will have to look it over once more...
I assumed they were completely proprietary to the gsub! method, namely because I didn't see any that I immediately recognized as regex...
I'll no doubt have more questions once my script if further underway, but thanks for the time being.
gsub! also works with normal strings. There are also the related methods gsub, sub! and sub.
This is my current method... or the relevant part anyway (the rest is working and has no volatile code so I've ruled it out as having an impact at all)
if $game_temp.message_text != nil
text = $game_temp.message_text
text.gsub!(/\\[Rr]\[([0-9]+)\,([0-9]+)\], /i) {|r| ""}
p rpic = $&
end
What is strange with this is that it ONLY finds the pattern (which should look like this: \R[0,0]) if it is trailed by a comma, like this: \R[0,0],
With the comma it seems to work fine, but without it, it ignores that entire pattern...
To better illustrate...
"\R[0,0] I like pie."
will output "\R[0,0] I like pie."
"\R[0,0], I like pie."
will output "I like pie"
So what's the deal?
EDIT: I forgot to mention that I read Zeriab's regular expression tutorial and mostly understand how gsub is supposed to work now... but mine isn't working like the other gsub's in the window_message script do (despite being almost identical... I only added a second number section and a comma to the pattern)
EDIT2: Also, am I posting my questions wrong? Should I be asking this type of question in a different section until I finish writing the actual script?
EDIT3: For some strange reason I'm uncertain of, \i was causing my comma issues... no more questions YET...
Regular expression are quite nasty. I'd need to sit down and test various combinations until I figure out the right one. >.<
/\\r\[([0-9]+?)\,([0-9]+?)\]/i
Works for me. After you replace that, you'll probably want to use that one method in String (can't remember its name) that removes whitespace from the start of a string; you should be able to find it by Googling "string class ruby."
You regex isn't working for the second case. -_-
Quote from: Blizzard on November 11, 2009, 08:18:30 am
You regex isn't working for the second case. -_-
What do you mean?
Actually it was the comma. Commas apparently don't need escape sequences, so "\," and "," are effectively the same. It was looking for two commas. And I forgot why I even put the \i in there anyway, the original scripts don't have it and I certainly don't see a need for case-insensitive when I'm only working with r and R...
Incidentally, I am far (sort of) past this issue now, but I will likely have more issues with the rest of the script. Thanks for the help thusfar.
Either /i at the end or \\[Rr].
@LF: Your regex doesn't handle an optional comma and any number of white spaces after the \R[A, B] expression.
Ok, I have a script that is mostly working... (I get no syntax problems) but there are a few issues I'm not sure how to deal with.
First, here are my scripts and Window_Message inserts
def sat_init
@messchar = ['','Senna/','Az/']
@messface = ['','norm.png','angry.png','happy.png']
if $game_temp.message_text != nil
text = $game_temp.message_text
text.gsub!(/\\[Rr]\[([0-9]+),([0-9]+)\]/) {|r| ""}
@rpic = $&
if @rpic != nil
@rpic = @rpic.scan(/\d+/)
@rpic = [@rpic[0].to_i, @rpic[1].to_i]
end
text.gsub!(/\\[Ll]\[([0-9]+),([0-9]+)\]/) {|l| ""}
@lpic = $&
if @lpic != nil
@lpic = @lpic.scan(/\d+/)
@lpic = [@lpic[0].to_i, @lpic[1].to_i]
end
if (@lpic != nil && @lpic[0] != 0 && @lpic[1] != 0)
@lefty = Left_Face.new unless @lefty != nil
@lefty.bitmap = Bitmap.new("Graphics/Faces/"+@messchar[@lpic[0]]+@messface[@lpic[1]])
end
if (@rpic != nil && @rpic[0] != 0 && @rpic[1] != 0)
@righty = Right_Face.new unless @righty != nil
@righty.bitmap = Bitmap.new("Graphics/Faces/"+@messchar[@rpic[0]]+@messface[@rpic[1]])
end
end
end
#Window_Message code truncated...(The code is not modified, just removed for forum display)
# If waiting for a message to be displayed
if $game_temp.message_text != nil
sat_init###########################################
text = $game_temp.message_text
# Control text processing
#more text removed for space saving
# Open gold window
if @gold_window != nil
@gold_window.dispose
@gold_window = nil
end
if @lefty != nil
@lefty.dispose
@lefty = nil
end
if @righty != nil
@righty.dispose
@righty = nil
end
The issue is this:
I need to figure out how to KEEP the displayed faces when they are not changed, BUT dispose them when there are no more text messages in the event.
As the code is now, I can keep the images by using the same tags with each message, but they blink (the sprite is disposed and recreated).
Can anyone help me out with this?
I'm not quite sure of the syntax, but if I'm correct, events store (for each event page) an array of interpreter commands to be called. So all you'd have to do is search through the array for the call message interpreter command. Like I said, I'm not sure of the syntax, but that should get you started on the right route. Look at the event class and the interpreter class, that should help you figure things out :D
I didn't go through your code, but...
1. If you draw the face on the Window_Message bitmap, you don't need to worry about it.
2. If you are using a separate sprite for the face, you can just use something like this in the refresh method:
if @face_sprite != nil
@face_sprite.dispose
@face_sprite = nil
end
I figured it out. I just had to change the dispose method to only happen if the "\K" kill command exists in the message... If it does it will dispose of the sprite after the message is closed... a simple fix, I declare this script finished.
Now I can work solely on my Smithery skill... which I could probably do with an event of sorts... but... I need the RGSS experience.
Expect to see me again XD
If you want to understand regular expression then I can recommend writing a tutorial about them :^_^':
The special character \s is a white space character, that is, space, tab, new line, carriage return and uhm... line feed I think. ( \t\n\r\f)
An optional comma and trailing whitespace will match /,?\s*/
The ? means 0 or 1 repetitions. * means 0, 1 or more repetitions.
I suggest using this regular expression: /\\[Rr]\[([0-9]+),([0-9]+)\],?\s*/
*hugs*