Global Text Codes
Version: 1.0a
Author: modern algebra
Date: July 5, 2011
Version History
- <Version 1.0a> 2011.07.05 - Fixed an F12 StackError
- <Version 1.0> 2009.12.08 - Original Release
Description
This script allows you to use some of the special message codes in any window or scene. Want to bold the name of the lead actor or colour the name of special skills? You can do that with this script. For a full list of the codes you can use and what they do, look at the instructions. You can also create your own replacement codes with a minimum of scripting knowledge through a simple interface.
Also, note that this script will likely not apply to other scripts that already parse text and look for message codes. This is the case with Window_Message, for instance, so this script will not apply to Window_Message. It will also not apply to scripts like my Journal script. This is not to say it will cause errors, but some funky things may happen.
Features
- Can use message codes in any scenes you want
- A large variety of built-in message codes to suit your every desire
- The ability to create your own filters and message codes relatively easily
ScreenshotsNote: Times New Roman Font for the description, the icon in the description, wooden is italicized in the description, Bennet's name is underlined, the entire word, Robe, is a different colour and the capital letters of Feathered Hat are a different colour. All of that and more is possible with this script.
Instructions See the header for detailed instructions and a list of text codes you can use, but there is very little you have to do - just put the script in your project and use the text codes you want where you want them. It's that simple.
Script
#==============================================================================
# Global Text Codes
# Version: 1.0a
# Author: modern algebra (rmrk.net)
# Date: July 5, 2011
#++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++
# Description:
#
# This script allows you to use some of the special message codes in any
# window or scene. Want to bold the name of the lead actor or colour the name
# of special skills? You can do that with this script. For a full list of the
# codes you can use and what they do, look at the instructions. You can also
# create your own replacement codes with a minimum of scripting knowledge
# through a simple interface.
#++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++
# Instructions:
#
# All you need to do is put the script in the editor above Main and below
# your default scripts and it should work. You can now use the following
# codes in most windows (unless they parse texts out character by character
# before drawing, as Window_Message does. You would need a custom message
# system if you wanted to change that). But it would work in things like item
# windows, the menu generally, shops and so on - everywhere there is a
# bitmap pretty much. Again, a significant exception is Window_Message and
# other custom scripts that already have a similar feature, such as my Diary
# script. Anyway, the codes that can be used from the getgo are:
# \v[x] - Shows the value located in the variable x
# \n[x] - Shows the name of the Actor with ID x
# \c[x] - Changes the colour of the text to x. x can be 0 - 31
# \pid[x] - Shows Actor ID of Party Member in position X (0-3)
# \nc[x]- Shows the name of class with ID x
# \np[x]- Shows the name of the Party Member with index x
# \ne[x]- Shows the name of Event with ID x on the map
# \nm[x]- Shows the name of Monster with ID x
# \ni[x]- Shows the name of Item with ID x
# \nw[x]- Shows the name of Weapon with ID x
# \na[x]- Shows the name of Armour with ID x
# \pi[x]- Shows the price of Item with ID x
# \pw[x]- Shows the price of Weapon with ID x
# \pa[x]- Shows the price of Armour with ID x
# \iicon[x] - Shows the Icon of Item with ID x
# \wicon[x] - Shows the Icon of Weapon with ID x
# \aicon[x] - Shows the Icon of Armour with ID x
# \icon[x] - Shows the Icon with ID x
# \fn[font name] - Changes the font to font name chosen
# \fs[x] - Changes the font size to x
# \vocab[value] - prints vocab for that item type. Suitable values for this
# are: level, level_a, hp, hp_a, mp, mp_a, atk, def, spi,
# agi, weapon, armor1, armor2, armor3, armor4, weapon1,
# weapon2, attack, skill, guard, item, equip, status, save,
# game_end, fight, escape, new_game, shutdown, to_title,
# continue, cancel, gold
# \f[key] - Show Filter phrase attached to key
# \b - Toggles Bold
# \i - Toggles Italic
# \u - Toggles Underline
# \s - Toggles Shadow
# \ac[x]- Shows class of actor with ID x
# \a..[x] - Shows the .. of Actor X. .. can be any of the following:
# hp, maxhp, mp, maxmp, atk, def, spi, agi, exp_s, next_exp_s,
# next_rest_exp_s, level, weapon_id, armor1_id, armor2_id,
# armor3_id, armor4_id - and any other methods from Game_Actor.
# \Gold - shows current gold of party
#
# The filters can be set at line 82
# You can also create your own replacement codes, though it is a little
# complicated. To see how, go to like 91
#==============================================================================
#==============================================================================
# *** MA GLOBAL CODES
#++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++
# This module is used for configuring the settings for the script. It also
# interprets and saves the special user-defined replacement codes.
#==============================================================================
module MA_GlobalCodes
#~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
# * CONSTANTS
#~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
FILTERS = {
# FILTERS allows you to set a filter and replace any \F[x] code with it's
# corresponding Entry in the Hash. Leave the FILTERS = {} line alone and
# below it, you can set all of the codes you will want to be able to put
# as an argument in the \F code.
'PF3' => '\c[1]Paragraph Formatter\c[0], Version 2.0: Formatter_3',
0 => 'Numbered filters work too'
}
USER_CODES = {
#~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
# Here you can create your own special codes for your own nefarious
# purposes. The format is:
# "code" => ["replacement", "arguments"]
# This can probably be understood best by comparing it to codes you already
# know of (and are included. Take, for instance the variable code:
# \V[x] # shows the value of variable with ID x.
# If that were being added as a user code, then the code would be:
# "V" => [$game_variables[~1~], "%d"]
#
# So, the first part,"code", is what goes after the "\" for the code to be
# identified. The "replacement" is what that code is replaced with when it
# appears, and the "arguments" are what goes in the brackets that are then
# used in the replacement by identifying where they go with the ~x~ codes,
# where x is a digit referring to what comes first. The codes for the
# arguments are:
# %d - this means that the argument must be a number
# %w - this includes all word characters, including numbers. Thus, it
# would be ideal if you need filenames for arguments, for instance.
# %. - this accepts any type of character, including whitespace
#
# Thus to put this in more concrete terms, a code like the following:
# "LX" => ["3*~1~ + 5*~2~", "%d%d"]
# would correspond to the code:
# \LX[x, y], where x and y are numbers
# and would be replaced with the result of the evaluation of
# 3*x + 5*y
# It's obviously, a useless code, but hopefully that is a reasonable example.
# Also, if you don't need any arguments, you can write simply:
# "code" => "replacement"
#~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
# The two following codes are additional examples
# \VM[x, y] would be replaced by the product of variable x and variable y
"VM" => ["$game_variables[~1~]*$game_variables[~2~]", "%d%d"],
# \GOLD would be replaced by the amount of gold the party currently has
"GOLD" => "$game_party.gold"
}
#~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
# * User Code
# key : the key to check
#``````````````````````````````````````````````````````````````````````````
# This method interprets and saves appropriate coding, thus avoiding
# repetitious interpretation.
#~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
def self.user_code (key)
@interpreted_hash = {} if @interpreted_hash.nil?
if @interpreted_hash[key].nil?
value = USER_CODES[key]
return "", nil if value.nil?
if value.is_a? (Array)
# Get correct replacement value
replacement = value[0].dup
args = value[1].dup
# Put arguments into the replacement
i = 1
while i < args.size
ext = args[i, 1] == "d" ? "i" : "s"
replacement.sub! (/~(#{(i + 1) / 2})~/) { "$#{$1.to_i}.to_#{ext}" }
i += 2
end
args.gsub! (/%d/) { "(-?\\d+?),\\s*?" }
args.gsub! (/%w/) { "(\\w+?),\\s*?" }
args.gsub! (/%\./) { "(.+?),\\s*?" }
args.slice! (-5, 5)
@interpreted_hash[key] = [replacement, args]
else
@interpreted_hash[key] = [value, nil]
end
end
return *@interpreted_hash[key]
end
end
#==============================================================================
# ** Bitmap
#++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++
# Summary of Changes:
# aliased method - draw_text
# new methods - convert_special_characters; perform_substitution;
# execute_special_code
#==============================================================================
class Bitmap
#~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
# * Draw Text
#~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
alias malgr_glbl_msgcdes_drwtxt_8kf2 draw_text unless self.method_defined? (:malgr_glbl_msgcdes_drwtxt_8kf2)
def draw_text (*args)
# Retrieve Arguments
if args[0].is_a? (Rect)
tx, ty, tw, th = args[0].x, args[0].y, args[0].width, args[0].height
string = args[1].to_s.dup
align = args[2].nil? ? 0 : args[2]
else
tx, ty, tw, th = *args[0, 4]
string = args[4].to_s.dup
align = args[5].nil? ? 0 : args[5]
end
text = convert_special_characters (string)
text, codes, indices, icon_count = extract_special_codes (text)
if codes.empty?
# Underline
if @underline
uy = ty + self.font.size + (Window_Base::WLH - self.font.size) / 2
ul_lngth = [tw, self.text_size (text)].min
tx += align == 1 ? (tw - ul_lngth) / 2 : align == 2 ? (tw - ul_lngth) : 0
self.fill_rect (tx, uy, ul_lngth, 2, self.font.color)
end
# Run original method exactly if there are no special codes
malgr_glbl_msgcdes_drwtxt_8kf2 (tx, ty, tw, th, text, align)
else
i = 0
room_w = tw - (24*icon_count)
ts = self.text_size (text).width
if ts < room_w && align != 0
tx += align == 1 ? (room_w - ts) / 2 : room_w - ts
end
room_w = [tw.to_f / room_w.to_f, 1.0].min
while !codes.empty?
code = codes.shift
j = indices.shift
if i != j
txt = text[i, j-i]
rw = (self.text_size (txt).width*room_w).to_i
if @underline
uy = ty + self.font.size + (Window_Base::WLH - self.font.size) / 2
self.fill_rect (tx, uy, rw, 2, self.font.color)
end
# Run Original Method on portion
malgr_glbl_msgcdes_drwtxt_8kf2 (tx, ty, rw, th, txt)
tx += rw
end
tx = execute_special_code (tx, ty, code)
i = j
end
return if i == text.size - 1
txt = text[i, text.size - i]
rw = (self.text_size (txt).width*room_w).to_i
if @underline
uy = ty + self.font.size + (Window_Base::WLH - self.font.size) / 2
self.fill_rect (tx, uy, rw, 2, self.font.color)
end
# Run Original Method on final portion
malgr_glbl_msgcdes_drwtxt_8kf2 (tx, ty, rw, th, txt)
end
end
#~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
# * Execute Special Code
# x, y : coordinates for execution
# code : code to be executed
#~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
def execute_special_code (x, y, code)
case code[0,1]
when "\x01" # Colour
code[/<(#?[\dABCDEF]+)>/i]
if $1.include? ("#")
r, g, b = $1[1, 2].to_i (16), $1[3, 2].to_i (16), $1[5, 2].to_i (16)
self.font.color = Color.new (r, g, b)
else
wx = 64 + ($1.to_i % 8) * 8
wy = 96 + ($1.to_i / 8) * 8
windowskin = Cache.system ("Window")
self.font.color = windowskin.get_pixel(wx, wy)
end
when "\x02"
code[/<(\d+)>/]
bitmap = Cache.system("Iconset")
rect = Rect.new($1.to_i % 16 * 24, $1.to_i / 16 * 24, 24, 24)
self.blt(x, y, bitmap, rect, 255)
x += 24
when "\x03" then self.font.bold = !self.font.bold # Bold
when "\x04" then self.font.italic = !self.font.italic # Italic
when "\x05" then self.font.shadow = !self.font.shadow # Shadow
when "\x06" then @underline = !@underline # Underline
when "\x09" # Change Font Name
code[/<(.*?)>/]
self.font.name = $1.to_s if Font.exist? ($1.to_s)
when "\x10" # Change Font Size
code[/<(\d*)>/]
self.font.size = $1.to_i
end
return x
end
#~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
# * Convert Special Characters
# text : the text to convert
#~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
def convert_special_characters (text)
return "" if text == nil
text = perform_substitution (text)
# Get substitutions
text.gsub! (/\\C\[(\d+)\]/i) { "\x01<#{$1.to_i}>" } # Palette Color
text.gsub! (/\\C\[#([\dABCDEF]+)\]/i) { "\x01<##{$1.to_s}>" } # Hex Color
text.gsub! (/\\IIC?O?N?\[(\d+)\]/i) { $1.to_i.between? (1, $data_items.size - 1) ? "\x02<#{$data_items[$1.to_i].icon_index}>" : "" } # Item Icon
text.gsub! (/\\WIC?O?N?\[(\d+)\]/i) { $1.to_i.between? (1, $data_weapons.size - 1) ? "\x02<#{$data_weapons[$1.to_i].icon_index}>" : "" } # Weapon Icon
text.gsub! (/\\AIC?O?N?\[(\d+)\]/i) { $1.to_i.between? (1, $data_armors.size - 1) ? "\x02<#{$data_armors[$1.to_i].icon_index}>" : "" } # Armor Icon
text.gsub! (/\\IC?O?N?\[(\d+)\]/i) { "\x02<#{$1.to_s}>" } # Icon
text.gsub! (/\\B/i) { "\x03" } # Bold
text.gsub! (/\\I/i) { "\x04" } # Italic
text.gsub! (/\\S/i) { "\x05" } # Shadow
text.gsub! (/\\U/i) { "\x06" } # Underline
text.gsub! (/\\FN\[(.+?)\]/i) { "\x09<#{$1.to_s}>" } # Font Name
text.gsub! (/\\FS\[(\d+)\]/i) { "\x10<#{$1.to_s}>" } # Font Size
return text
end
#~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
# * Perform Substitution
#~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
def perform_substitution (text = @string)
text.gsub!(/\\V\[([0-9]+)\]/i) { $game_variables[$1.to_i] } # Variable
# FILTERS
text.gsub!(/\\F\[["'](.+?)["']\]/i) { MA_GlobalCodes::FILTERS[$1.to_s] }
text.gsub!(/\\F\[(.+?)\]/i) { MA_GlobalCodes::FILTERS[$1.to_i] }
# Party ID to Actor ID
text.gsub!(/\\PID\[(\d+)\]/i) { $game_party.members[$1.to_i].id }
# Old Codes
text.gsub!(/\\N\[([0-9]+)\]/i) { $1.to_i > 0 ? $game_actors[$1.to_i].name : ""} # Actor Name
# New Codes
begin
text.gsub! (/\\VOCAB\[(\w+)\]/i) { Vocab.send ($1.downcase) } # Vocab
rescue
end
text.gsub! (/\\AC\[(\d+)\]/i) { $game_actors[$1.to_i].class.name } # Actor Class
# Actor Stats
begin
text.gsub! (/\\A([^\[]+?)\[(\d+)\]/i) { $game_actors[$2.to_i].send ($1.to_s.downcase) }
rescue
end
text.gsub! (/\\NC\[(\d+)\]/i) { $1.to_i > 0 ? $data_classes[$1.to_i].name : "" } # Class Name
text.gsub! (/\\NE\[(\d+)\]/i) { $1.to_i > 0 ? $game_map.events[$1.to_i].name : "" } # Event Name
text.gsub! (/\\NM\[(\d+)\]/i) { $1.to_i > 0 ? $data_enemies[$1.to_i].name : "" } # Monster Name
text.gsub! (/\\NI\[(\d+)\]/i) { $1.to_i > 0 ? $data_items[$1.to_i].name : "" } # Item Name
text.gsub! (/\\NW\[(\d+)\]/i) { $1.to_i > 0 ? $data_weapons[$1.to_i].name : "" } # Weapon Name
text.gsub! (/\\NA\[(\d+)\]/i) { $1.to_i > 0 ? $data_armors[$1.to_i].name : "" } # Armor Name
text.gsub! (/\\PI\[(\d+)\]/i) { $1.to_i > 0 ? $data_items[$1.to_i].price.to_s : "" } # Item Price
text.gsub! (/\\PW\[(\d+)\]/i) { $1.to_i > 0 ? $data_weapons[$1.to_i].price.to_s : "" } # Weapon Price
text.gsub! (/\\PA\[(\d+)\]/i) { $1.to_i > 0 ? $data_armors[$1.to_i].price.to_s : "" } # Armor Price
# Parse custom replacement codes
MA_GlobalCodes::USER_CODES.keys.each { |key|
replacement, args = MA_GlobalCodes.user_code (key)
if args != nil
begin
text.gsub! (/\\#{key}\[#{args}\]/i) { eval (replacement) }
rescue
end
else
begin
text.gsub! (/\\#{key}/i) { eval (replacement) }
rescue
end
end
}
text.gsub! (/\\V\[([0-9]+)\]/i) { $game_variables[$1.to_i] } # Variable
text.gsub! (/\\\\/) { "\\" }
return text
end
#~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
# * No Argument Codes
#~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
def no_args_codes
return ["\x03", "\x04", "\x05", "\x06"]
end
#~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
# * Argument Codes
#~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
def args_codes
return ["\x01", "\x02", "\x09", "\x10"]
end
#~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
# * Extract Special Codes
#``````````````````````````````````````````````````````````````````````````
# This parses the text and removes the special codes, while retaining
# their position in order to be inserted when the time comes.
#~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
def extract_special_codes (text)
codes = []
code_indices = []
icon_count = 0
i = 0
while i < text.size
# Work from the end of the string
c = text[i, 1]
if no_args_codes.include? (c)
text.sub!(c, "")
codes.push (c)
code_indices.push (i)
elsif args_codes.include? (c)
a = text.sub! (/(#{c}<.*?>)/, "")
# If nothing is replaced
if a == nil
i += 1
else
codes.push ($1)
code_indices.push (i)
icon_count += 1 if c == "\x02"
end
else
i += 1
end
end
return text, codes, code_indices, icon_count
end
end
Credit
Thanks
- Anahuyu Izimori, for the request
Support
Please post in this topic at rmrk.net for support. Do not PM me. I am not active on other forums, and so there is no guarantee I will ever see your post if you do not post it here.
Known Compatibility Issues
Probably quite a few. I doubt there will be many game-breaking errors, but as I mentioned in the description, it will not apply to some scripts such as message scripts or any scripts (again, it may be possible that a message script and this could be in the same project without errors, but only to say that the message codes in this script will not work in a message)
This script by
modern algebra is licensed under a
Creative Commons Attribution-Non-Commercial-Share Alike 2.5 Canada License.