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.

[RMVX Script] -SOLVED- Adding on to a script- call sound effect from message box

Hi! I'm brand new to RGSS2- just started picking at it a few days ago and so far I can mod the default/other people's scripts pretty minimally. Right now I'm trying to add a feature to a pre-existing script that would allow you to make a call that plays a sound effect in the middle of text.

The original script is here.

Code:
#=======================================================================
#  Modern Algebra Message Script
#  Version 0.5
#  Author: modern algebra
#  February 22, 2008
#~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
#  Special Thanks to:
#      Arrow-1 for the request. I loved the animated facesets idea
#      Zeriab for his regular expressions tutorial
#~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
#  Current Features:
#     - Precise letter-by-letter timing control
#     - Animated Facesets
#     - Can play a sound effect with each letter drawn
#     - numerous additional codes - see below for all the codes
#~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
#  Current Codes:
#     - \< Slows the speed at which the text is drawn
#     - \> Increases the speed at which the text is drawn
#     - \b bold text
#     - \i italicized text
#     - \name[text] name box
#     - \ne[enemy_id] name of enemy
#     - \ni[item_id] Name of item
#     - \nw[weapon_id] Name of weapon
#     - \na[armor_id] Name of Armor
#     - \pi[item_id] price of item
#     - \pw[weapon_id] price of weapon
#     - \pa[armor_id] price of Armor
#     - \icon[icon_index] draw icon assigned to icon_index
#     - \iicon[item_id] draw icon of item
#     - \wicon[weapon_id] draw icon of weapon
#     - \aicon[armor_id] draw icon of armor
#~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
#  Instructions:
#    Fill in the constants below for default settings. You can change pretty much all of these 
#    constants in-game with the codes:
#
#        Message_Options.letter_by_letter_sound = true/false
#        Message_Options.message_se = 'filename'
#        Message_Options.default_timing = integer (lower = faster)
#        Message_Options.namebox_windowskin = 'Windowskin'
#        Message_Options.namebox_color = integer (0..32)
#        Message_Options.namebox_fontname = 'font name'
#        Message_Options.namebox_fontsize = 'Windowskin'
#        Message_Options.namebox_offset_x = integer
#        Message_Options.namebox_offset_y = integer
#        Message_Options.opacity = integer (0..255)
#
#  To use animated facesets, merely use a face graphic that ends with _a and you must have 
#  another faceset with the same name but ending with _b. 
#=======================================================================

module Message_Options
  #~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
  # * Constants
  #~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
  DEFAULT_TIMING = 0 # The default amount of frames between each letter as it is drawn
  LETTER_BY_LETTER_SOUND = true # Whether there should be an SE played with each character drawn
  MESSAGE_SE = 'Open1' # The SE to be played if LETTER_BY_LETTER_SOUND == true
  ANIMATED_FACESETS = true  # true => active animated facesets; false => deactivated
  NAMEBOX_WINDOWSKIN = "Window" # The windowskin for the name box
  NAMEBOX_COLOR = 5 # The Color of the text in the namebox
  NAMEBOX_FONTNAME = 'Times New Roman' # The Font of the namebox
  NAMEBOX_FONTSIZE = 20 # The Size of the text in the namebox
  NAMEBOX_OFFSET_X = 16 # How much the Namebox is in the X direction away from default
  NAMEBOX_OFFSET_Y = 16 # How much the Namebox is in the Y direction away from default
  NAMEBOX_OPACITY = 100 # The opacity of the name box
  #~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
  # * Message SE (Lazy Initialization)
  #~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
  def self.message_se
    self.message_se = MESSAGE_SE if @message_se.nil?
    return @message_se
  end
  #~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
  # * Message SE =
  #~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
  def self.message_se= (file_name)
    @message_se = RPG::SE.new  (file_name)
  end
  #~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
  # * Message_SE_Active?
  #~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
  def self.message_se_active?
    @letter_by_letter_sound = LETTER_BY_LETTER_SOUND if @letter_by_letter_sound.nil?
    return @letter_by_letter_sound  
  end
  #~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
  # * Letter BY Letter Sound = 
  #~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
  def self.letter_by_letter_sound= (boolean)
    @letter_by_letter_sound = boolean
  end
  #~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
  # * NameBox Windowskin
  #~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
  def self.namebox_windowskin
    @namebox_windowskin = Cache.system (NAMEBOX_WINDOWSKIN) if @namebox_windowskin == nil
    return @namebox_windowskin
  end
  #~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
  # * NameBox Windowskin=
  #~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
  def self.namebox_windowskin= (string)
    @namebox_windowskin = Cache.system (string)
  end
  #~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
  # * NameBox Color
  #~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
  def self.namebox_color
    @namebox_color = NAMEBOX_COLOR if @namebox_color == nil
    return @namebox_color
  end
  #~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
  # * NameBox Color=
  #~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
  def self.namebox_color= (value)
    @namebox_color = value
  end
  #~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
  # * NameBox Fontname
  #~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
  def self.namebox_fontname
    @namebox_font = NAMEBOX_FONTNAME if @namebox_font == nil
    return @namebox_font
  end
  #~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
  # * NameBox Fontname=
  #~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
  def self.namebox_fontname= (string)
    @namebox_font = string
  end
  #~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
  # * NameBox Fontsize
  #~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
  def self.namebox_fontsize
    @namebox_fontsize = NAMEBOX_FONTSIZE if @namebox_fontsize == nil
    return @namebox_fontsize
  end
  #~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
  # * NameBox Fontsize=
  #~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
  def self.namebox_fontsize= (value)
    @namebox_fontsize = value
  end
  #~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
  # * NameBox Offset X
  #~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
  def self.namebox_offset_x
    @namebox_x = NAMEBOX_OFFSET_X if @namebox_x == nil
    return @namebox_x
  end
  #~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
  # * NameBox Offset X=
  #~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
  def self.namebox_offset_x= (value)
    @namebox_x = value
  end
  #~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
  # * NameBox Offset Y
  #~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
  def self.namebox_offset_y
    @namebox_y = NAMEBOX_OFFSET_Y if @namebox_y == nil
    return @namebox_y
  end
  #~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
  # * NameBox Offset Y=
  #~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
  def self.namebox_offset_y= (value)
    @namebox_y = value
  end
  #~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
  # * NameBox Opacity
  #~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
  def self.namebox_opacity
    @namebox_opacity = NAMEBOX_OPACITY if @namebox_opacity == nil
    return @namebox_opacity
  end
  #~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
  # * NameBox Opacity=
  #~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
  def self.namebox_opacity= (value)
    @namebox_opacity = value
  end
  #~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
  # * Default timing
  #~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
  def self.default_timing
    @default_timing = DEFAULT_TIMING if @default_timing == nil
    return @default_timing
  end
  #~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
  # * Default Timing =
  #~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
  def self.default_timing= (value)
    value = [value, 0].max
    @default_timing = value
  end
end

module Sound
  #~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
  # * Play Message SE
  #~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
  def self.play_message_se
    Message_Options.message_se.play
  end
end

#======================================================================
# ** Sprite Message Face
#~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
#  A sprite to reduce lag when using Animated  Facesets
#======================================================================

class Sprite_MessageFace < Sprite_Base
  #~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
  # * Object Initialization
  #~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
  def initialize (face_file, face_index = 0, size = 96, single = false) 
    super ()
    self.visible = false
    self.z = 250
    face = Cache.face (face_file)
    if single
      self.bitmap = face
    else
      self.bitmap = Bitmap.new (face.width / 4, face.height / 2)
      rect = Rect.new(0, 0, 0, 0)
      rect.x = face_index %  4* 96 + (96 - size) / 2
      rect.y = face_index / 4 * 96 + (96 - size) / 2
      rect.width = size
      rect.height = size
      self.bitmap.blt(x, y, face, rect)
      face.dispose
    end
  end
end

#========================================================================
# ** Window Name Box
#~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
#  This window displays the name of a speaker in Window_Message
#========================================================================

class Window_NameBox < Window_Base
  #~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
  # * Object Initialization
  #~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
  def initialize (x, y, string)
    temp_bitmap = Bitmap.new (1, 1)
    temp_bitmap.font.name = Message_Options.namebox_fontname
    temp_bitmap.font.size = Message_Options.namebox_fontsize
    rect = temp_bitmap.text_size (string)
    temp_bitmap.dispose
    super (x, y, rect.width + 32, rect.height + 32)
    self.z = 300
    create_contents
    # Use Namebox settings
    self.back_opacity = Message_Options.namebox_opacity
    self.windowskin = Message_Options.namebox_windowskin
    self.contents.font.name = Message_Options.namebox_fontname
    self.contents.font.size = Message_Options.namebox_fontsize
    self.contents.font.color = text_color(Message_Options.namebox_color)
    # Draw the text
    self.contents.draw_text (0, 0, rect.width, rect.height, string)
  end
end

#======================================================================
# ** Window_Message
#~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
#  Summary of changes:
#     Basically, this script changes the way letter-by-letter works in order to allow for various
#     options: namely speed variability, sound effects, and animated facesets
#
#     aliased method - convert_special_characters
#     overwritten methods - update_message
#======================================================================

class Window_Message < Window_Selectable
  #~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
  # * Convert Special Characters
  #~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
  alias modalg_msg_script_special_char_conversion convert_special_characters
  def  convert_special_characters
    # New codes
    modalg_msg_script_special_char_conversion 
    @text.gsub!(/\\NE\[([0-9]+)\]/i) { $data_enemies[$1.to_i].name } # Name Enemy
    @text.gsub!(/\\NI\[([0-9]+)\]/i) { $data_items[$1.to_i].name }   # Name Item
    @text.gsub!(/\\NW\[([0-9]+)\]/i) { $data_weapons[$1.to_i].name } # Name Weapon
    @text.gsub!(/\\NA\[([0-9]+)\]/i) { $data_armors[$1.to_i].name } # Name Armor
    @text.gsub!(/\\PI\[([0-9]+)\]/i) { $data_items[$1.to_i].price.to_s } # Price Item
    @text.gsub!(/\\PW\[([0-9]+)\]/i) { $data_weapons[$1.to_i].price.to_s } # Price Weapon
    @text.gsub!(/\\PA\[([0-9]+)\]/i) { $data_armors[$1.to_i].price.to_s } # Price Armor
    @text.gsub! (/\\IICON\[([0-9]+)\]/i) { "\x10[#{$data_items[$1.to_i].icon_index}]" } # Icon Item
    @text.gsub! (/\\WICON\[([0-9]+)\]/i) { "\x10[#{$data_weapons[$1.to_i].icon_index}]" } # Icon Weapon
    @text.gsub! (/\\AICON\[([0-9]+)\]/i) { "\x10[#{$data_armors[$1.to_i].icon_index}]" } # Icon Armor
    # Retrieve Name
    name = @text[/\\NAME\[.*?\]/i]
    name.sub! (/\\NAME/i, '') unless name == nil
    @text.gsub! (/\\NAME\[.*?\]/i) { "\x09#{name}" } # Name Window
    @text.gsub! (/\\ICON\[([0-9]+)\]/i) { "\x10[#{$1}]" } # Icon
    @text.gsub!(/\\B/i) { "\x11" } # Bold
    @text.gsub!(/\\I/i) { "\x12" } # Italic
    # Run original script
  end
  #~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
  # * Object Initialization
  #~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
  alias modalg_msg_script_init initialize
  def initialize
    # Run original method
    modalg_msg_script_init
    @letter_timing = Message_Options.default_timing
    @face_sprites = []
  end
  #~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
  # * New Page
  #~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
  def new_page
    contents.clear
    if $game_message.face_name.empty?
      @contents_x = 0
    else
      @face_sprites.clear
      name = $game_message.face_name.dup
      index = $game_message.face_index
      # Determine if the face is a single graphic
      single = name[/^\$/] != nil
      face = Sprite_MessageFace.new (name, index, 96, single)
      face.x = self.x + 16
      face.y = self.y + 16
      face.visible = true
      @face_sprites.push (face)
      @current_face = 0
      unless name[/\_a$/i] == nil
        name.gsub! (/\_a$/i) {|s| s = '_b'}
        face = Sprite_MessageFace.new (name, index, 96, single)
        face.x = self.x + 16
        face.y = self.y + 16
        @face_sprites.push (face)
      end
      @contents_x = 112
    end
    @contents_y = 0
    @line_count = 0
    @show_fast = false
    @line_show_fast = false
    @pause_skip = false
    contents.font.color = text_color(0)
  end
  #~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
  # * Update Message
  #~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
   def update_message
     loop do
      @wait_count = Message_Options.default_timing
      c = @text.slice!(/./m)            # 次ã
 

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