Submit Your Article


 
RPG Maker

Welcome Guest ( Log In | Register )


  Games Resources RPG Maker VX RPG Maker XP Scripts Tutorials Downloads

 
Reply to this topicStart new topic
> [Tutorial]Auto Colored Words, make your life easier
Slipknot
post Jun 27 2006, 09:59 AM
Post #1


Level 2
Group Icon

Group: Member
Posts: 25
Type: Scripter
RM Skill: Masterful




Auto Colored Words


You should already know about the text color in the message window, you just have to use \C[#] and your text will have a new color.

This can be useful to use with important words, like the currency, elements, names, locations, ... But if you want to draw in your game the word stone with blue in all the game, you'll have to use this every time the word appears in a message: some text \C[1]stone\c[0], more text.

But in this tutorial I'll try to teach you, how make this simplier by just adding a small script line in the Window_Message script.

To make this, first open your script editor and look for a script called Window_Message, then search the lines 84-86 or this text:
CODE
  text.gsub!(/\\[Nn]\[([0-9]+)\]/) do
 $game_actors[$1.to_i] != nil ? $game_actors[$1.to_i].name : ""
  end

And just below it add this line of code:
CODE
text.gsub!(/(stone)/) { "\\c[1]#{$1}\\c[0]" }


Then test it by writing a text containg the word stone, and it must appear of color blue.

Explanation:
  • text is a variable containg the current message.
  • gsub! is a string's method for replace substrings.
  • /(stone)/ is a RegExp (Regular Expression), a regexp must be inside '/'. The parenthesis are for indicate to Ruby that the string inside them will be used later.
  • "\\c[1]#{$1}\\c[0]" is a doubled quoted string, using expression sustitution.
    The first part of the string (\\c[1]) is \C[1] in the message edit window. The second part (#{$1}) is used to insert a variable in the string, in this case the variable is $1, this variable contains the last string matched.
    The third part is the same of the first, but this turns the text color into normal again.
Now every time you use stone in a message, it'll be drawed in a blue color, but if you use Stone or stones it won't work.

To fix this you must use the character class ([]) and some metacharacters (?.*+).
Replace your old code with this:
CODE
text.gsub!(/([Ss]tones?)/) { "\\c[1]#{$1}\\c[0]" }

Now take look to the [Ss], if ruby founds a "S" or "s" followed by tone or tones, will match and draw them of color blue.
The ? is used to match or not the previous character or character class in the pattern.

Now you can test it, type in a message this: Stones, stones, stone, Stone. and you'll get this:


This can also be useful to match different words with the same code:
CODE
text.gsub!(/([Ss]tr?o[kn]es?)/) { "\\c[3]#{$1}\\c[0]" }

The last code will match stone stones Stone Stones Stroke Stoke stokes... and draw them of color blue.

With a bit of scripting knowledge and taking a look to a custom message script, you'll be able to add yours new skills into it.

If you have a doubt or don't understand something, just ask it!


__________________________
Go to the top of the page
 
+Quote Post
   

Reply to this topicStart new topic
1 User(s) are reading this topic (1 Guests and 0 Anonymous Users)
0 Members:

 

Lo-Fi Version Time is now: 19th June 2013 - 04:18 PM
RPG RPG Revolution is an Privacy Policy and Legal
eXTReMe Tracker