Envision, Create, Share

Welcome to HBGames, a leading amateur game development forum and Discord server. All are welcome, and amongst our ranks you will find experts in their field from all aspects of video game design and development.

Need help with "gsub!"

Hello. I want to make a message system with the following function. When there is \k[string] in the text, it replaces \k[string] for string (In bold and/or italic). I know that I have to use text.gsub! but when I do it, exactly like in the original message system, nothing happens.

Help me please! :please:
Thanks in advance!

-Dargor
 

Zeriab

Sponsor

The first step is to write a regular expression. I have made a tutorial about them here: http://www.rmxp.org/forums/showthread.php?t=21009
You can use the one I give here: (\k[string] and \K[string] accepted where string consists of any
Code:
/\\[Kk]\[(.*?)\]/

The parenthesis around the (.*?) are there for back reference purposes.
.*? and not .* because we would not want a greedy approach.
To keep in the spirit of a message system I'll as an example modify the default message system. (Having any custom scripts may mess this up)
I suggest you make a new project, a test project if you want to test this.,

Go to around line 90 with the
Code:
text.gsub!(/\\[Gg]/) { "\002" }
Below this we make insert this line:
Code:
text.gsub!(/\\[Kk]\[(.*?)\]/) {"\003" + $1 + "\003"}
This replaces the \k[String] with with \003String\003
The $1 is a back reference to the first parenthesis. You only have one () so it 's easy in this case.

The reason I put two \003 was to have one to open and one to close.
If the start and ending should act differently then just use this block instead: {"\003" + $1 + "\003"}

This alone is not enough. Now you have just change the string.
Luckily a great help have been given for the next step. Let's look in this while loop:
Code:
while ((c = text.slice!(/./m)) != nil)
Note:
Code:
        # If \G
        if c == "\002"
           # stuff...
        end

We just have to define actions in an if-branch for our \003.
We'll use this:
Code:
        # If bold and italic
        if c == "\003"
          font = self.contents.font
          font.bold = !font.bold
          font.italic = !font.italic
        end

This simple says that if it meets a \003 it will change the font to be the opposite of what it was before in case being bold and italic.
That is... if the font is not bold, it will make the font bold, otherwise it will make the font non-bold
If the font is not italic, it will make the font italic, otherwise it will make the font non-italic.

I hope I have helped you enough ^_^
- Zeriab
 

Thank you for viewing

HBGames is a leading amateur video game development forum and Discord server open to all ability levels. Feel free to have a nosey around!

Discord

Join our growing and active Discord server to discuss all aspects of game making in a relaxed environment. Join Us

Content

  • Our Games
  • Games in Development
  • Emoji by Twemoji.
    Top