Well, I have a test in a second, so this explanation will be cut off once ten minutes have passed on my end.
I assume by 2nd regular expression:
@text.gsub!(/\\SE\[(.+?)\]/i) { "\x13[#{$1}]" } # Sound effect
The \xdd (where d are characters) are hex. base 16 (0 1 2 3 4 5 6 7 8 9 a b c d e f 10 11 12 ...) are the characters accepted. I'll be honest here, I performed this conversion because the default scripts do. I am not sure what the benefit really is, aside from the fact that it converts what are multiple characters (\, s, e) into one character that still distinguishes between each of the codes and allows us to keep easy track of it. The rest of the line: gsub! is a method that replaces every instance of the code in round brackets with what is in curly brackets. So every time it matches the code it will replace it with the stuff in curly brackets. Now, for the { "\x13[#{$1}]" } - This is just a string. To place the value of a variable inside a string you can use the notation #{variable name}, and that is all that is.
Oh, and the reason we use \[ and \] instead of [ and ] is because in Regexp, [ and ] denote a set of letters where any of the things contained can be used. so [abcde] means it would accept a, b, c, d, or e as the next character in the string. Putting the backslash there denotes you want the actual character '['or ']'
As to how this converts into playing an SE when that character comes up:
The letter-by-letter works by taking the current character in a string and interpreting it. It does this through a case branch:
c = @text.slice!(/./m) # ???????
case c
when nil # ??????????
finish_message # ????
break
when "\x00" # ??
new_line
if @line_count >= MAX_LINE # ????????
unless @text.empty? # ??????????
self.pause = true # ????????
break
end
end
break
when "\x01" # \C[n] (?????)
@text.sub!(/\[([0-9]+)\]/, "")
contents.font.color = text_color($1.to_i)
next
etc....
Anyway, here is the benefit of using the hex (at least as far as I can see). If we had not converted it, then it would have taken "\", then "s", then "e" and you'd need to do some weird interpretive stuff before being able to determine what it means. You wouldn't be able to draw character by character really, without doing interpretive work first. However \xdd, is interpreted as a single character, so c = @text.slice! (/./m) gets the hex code and we can then interpret from that
So now we can make our case branch:
When we have our c = to one of these hex codes, we do something special, and if it doesn't match any of them then we just draw it. That is basically what the case branch does.
So the one that we are interested in is:
when "\x13" # \SE[file] Play Sound effect
# Play sound effect
@text.sub!(/\[(.*?)\]/, "")
begin
RPG::SE.new($1).play
rescue
end
Alright, have to go now. I'll finish this when I get back on.
EDIT::
Okay, so since we substituted \se for \x13, as we are going through the string we will come to a "\x13", and this is what we have as our case branch. You've seen the first command, kind of. sub! is the same as gsub! but it only replaces the first string that matches the regexp that it comes to, rather than every single one. The part after the comma, the "", just means that it isn't replaced with anything; essentially we just want to delete it.
You've seen this regexp before: it is the exact same as the one we initially found. We are again extracting the characters within the brackets and they are put in $1.
RPG::SE.new($1).play - means that an SE object is created from the name given in the square brackets and then played. Thus, the point of the new code is executed here
Normally, if the name written in the brackets was not a file in the SE folder, then the game would break. What the rescue does (I think) is that if, while the begin is being run, an exception is thrown (and trying to access a non-existent file will throw an exception), the rescue traps it. You could make the rescue print something else if you wanted it to, by writing something like rescue "File does not exist", but we don't really want to do that because nobody wants their game to be interrupted by an ugly print statement unless they have dedicated testers who will be sure to try and break the game in every way possible and thus find these errors when they occur*.
Anyway, this was very long. I'm probably not entirely correct on some things (or at all) but hopefully I am. But don't worry about understanding everything I've said yet anyway. For a lot of scripts (some quite complicated), this stuff is unnecessary. It may look complicated right now, but soon enough it will become much easier to work with even if you still aren't sure what it all means. As well, I'm just plain bad at explaining things. For a beginning scripter, I think it is probably best to start by working with windows and scenes.
* PeopleTest.exist? (Dedicated Testers) = false.