The RPG Maker Resource Kit

RMRK RPG Maker Creation => VX => VX Scripts Database => Topic started by: modern algebra on March 05, 2008, 12:24:47 AM

Title: Ingame System SE Definition
Post by: modern algebra on March 05, 2008, 12:24:47 AM
Ingame System Audio Definition
Version: 2.0
Author: modern algebra
Date: June 5, 2010


Description


This script allows you to change what audio files are associated with the system at any time during the game. That's a bit abstract, so an easier way to explain it would be that if, for whatever reason, you want to be able to change the sound effect that is played when you press enter on something, then this script allows you to change that in-game. In other words, you don't have to have only one decision SE for the entire game. The same goes for all of the BGMs, MEs, and SEs that are set in the System tab of the Database, except for the title ones.

Features


Instructions

Place this script in its own slot in the Script Editor (F11), above Main but below Materials.

You can change any of the system audio files in-game using the following code in a call script:
Code: [Select]
change_audio (:type, "filename", volume, pitch)
         type - this is how you set which sound effect to change. The possible values are (they correspond to their name in System):
             :cursor, :decision, :cancel, :buzzer, :equip, :save, :load, :battle_start, :escape, :enemy_attack, :enemy_damage, :enemy_collapse, :actor_damage, :actor_collapse, :recovery, :miss, :evasion, :shop, :item_use, :skill_use, :boat, :ship, :airship, :battle, :battle_end, :gameover
         filename - the name of the audio file to change to it must be within quotation marks. If excluded, it erases the sound altogether.
         volume - the volume you want the audiofile to play at. If excluded, this defaults to 100
         pitch - the pitch you want the audiofile to play at. If excluded, this defaults to 100

EXAMPLES:
    change_audio (:cursor, "Cursor2", 60)
    change_audio (:gameover,"Victory1",120,80)

  You will want to avoid having it go over more than one line in the script call, so if it doesn't fit, set the name or other attributes to a new variable, like this:
    n = "Decision2"
    change_audio (:decision, n, 65, 100)

Script


Code: [Select]
#==============================================================================
#  Ingame System Audio Definition
#  Author: modern algebra (rmrk.net)
#  Version: 2.0
#  Date Released: June 5, 2010
#++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++
#  Description:
#
#    This script allows you to change what audio files are associated with the
#   system at any time during the game. That's a bit abstract, so an easier way
#   to explain it would be that if, for whatever reason, you want to be able to
#   change the sound effect that is played when you press enter on something,
#   then this script allows you to change that in-game. In other words, you
#   don't have to have only one decision SE for the entire game. The same goes
#   for all of the BGMs, MEs, and SEs that are set in the System tab of the
#   Database, except for the title ones.
#++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++
#  Instructions:
#
#    Place this script in its own slot in the Script Editor (F11), above Main
#   but below Materials.
#
#    You can change any of the system audio files in-game using the following
#   code in a call script:
#
#        change_audio (:type, "filename", volume, pitch)
#          type - this is how you set which sound effect to change. The
#            possible values are (they correspond to their name in System):
#              :cursor, :decision, :cancel, :buzzer, :equip, :save, :load,
#              :battle_start, :escape, :enemy_attack, :enemy_damage,
#              :enemy_collapse, :actor_damage, :actor_collapse, :recovery,
#              :miss, :evasion, :shop, :item_use, :skill_use, :boat, :ship,
#              :airship, :battle, :battle_end, :gameover
#          filename - the name of the audio file to change to it must be within
#            quotation marks. If excluded, it erases the sound altogether.
#          volume - the volume you want the audiofile to play at. If excluded,
#            this defaults to 100
#          pitch - the pitch you want the audiofile to play at. If excluded,
#            this defaults to 100
#
#  EXAMPLES:
#    change_audio (:cursor, "Cursor2", 60)
#    change_audio (:gameover,"Victory1",120,80)
#
#  You will want to avoid having it go over more than one line in the script
# call, so if it doesn't fit, set the name or other attributes to a new
# variable, like this:
#    n = "Decision2"
#    change_audio (:decision, n, 65, 100)
#==============================================================================
# ** Game_System
#++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++
#  Summary of Changes:
#    new attr_reader - ma_sounds; ma_music
#    aliased method - initialize
#    new method - set_audiofile
#==============================================================================

class Game_System
  #~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
  # * Public Instance Variables
  #~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
  attr_reader :ma_sounds
  attr_reader :ma_music
  #~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
  # * Object Initialization
  #~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
  alias malrab_igsd2_inze_4xc9 initialize
  def initialize (*args)
    malrab_igsd2_inze_4xc9 (*args) # Run Original Method
    initialize_system_audio
  end
  #~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
  # * Initialize System Audio
  #~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
  def initialize_system_audio
    @ma_sounds = Marshal.load (Marshal.dump($data_system.sounds))
    @ma_music = [$data_system.boat.bgm.dup, $data_system.ship.bgm.dup,
      $data_system.airship.bgm.dup, $data_system.battle_bgm.dup,
      $data_system.battle_end_me.dup, $data_system.gameover_me.dup]
  end
  #~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
  # * Set AudioFile
  #~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
  def set_audiofile (id, name = "", *args)
    if id >= @ma_sounds.size
      id -= @ma_sounds.size
      @ma_music[id] = id < 4 ? RPG::BGM.new (name, *args) : RPG::ME.new (name, *args)
      case id
      when 0 then $data_system.boat.bgm = $game_system.ma_music[0]
      when 1 then $data_system.ship.bgm = $game_system.ma_music[1]
      when 2 then $data_system.airship.bgm = $game_system.ma_music[2]
      when 3 then $data_system.battle_bgm = $game_system.ma_music[3]
      when 4 then $data_system.battle_end_me = $game_system.ma_music[4]
      when 5 then $data_system.gameover_me = $game_system.ma_music[5]
      end
    else
      @ma_sounds[id] = RPG::SE.new (name, *args)
      $data_system.sounds[id] = @ma_sounds[id]
    end
  end
end

#==============================================================================
# ** Game_Interpreter
#++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++
#  Summary of Changes:
#    new method - change_audio
#==============================================================================

class Game_Interpreter
  #~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
  # * Change Audio
  #     type : symbol or integer representing type
  #     args : the attributes of the new file
  #~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
  def change_audio (type = nil, *args)
    if type.is_a? (Symbol)
      id = [:cursor, :decision, :cancel, :buzzer, :equip, :save, :load,
        :battle_start, :escape, :enemy_attack, :enemy_damage, :enemy_collapse,
        :actor_damage, :actor_collapse, :recovery, :miss, :evasion, :shop,
        :item_use, :skill_use, :boat, :ship, :airship, :battle, :battle_end,
        :gameover].index (type)
    else
      id = type
    end
    return if !id.is_a? (Integer)
    $game_system.set_audiofile (id, *args)
  end
end

#==============================================================================
# ** Scene File
#++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++
#  Summary of Changes:
#    aliased method - read_save_data
#==============================================================================

class Scene_File
  #~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
  # * Read Save Data
  #~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
  alias modrabra_isad_rdsvda_5uj8 read_save_data
  def read_save_data (*args)
    modrabra_isad_rdsvda_5uj8 (*args) # Run Original Method
    if $game_system.ma_sounds.nil?
      $game_system.initialize_system_audio
    else
      $data_system.sounds = $game_system.ma_sounds
      $data_system.boat.bgm = $game_system.ma_music[0]
      $data_system.ship.bgm = $game_system.ma_music[1]
      $data_system.airship.bgm = $game_system.ma_music[2]
      $data_system.battle_bgm = $game_system.ma_music[3]
      $data_system.battle_end_me = $game_system.ma_music[4]
      $data_system.gameover_me = $game_system.ma_music[5]
    end
  end
end

Credit



Thanks


Support


Just post in this topic.

Known Compatibility Issues

I don't forsee any compatibility problems that can't be fixed by putting this script below any of your other custom scripts, but still above Main.


Creative Commons License
This script by modern algebra is licensed under a Creative Commons Attribution-Non-Commercial-Share Alike 2.5 Canada License.
Title: Re: Ingame System SE Definition
Post by: Zylos on March 05, 2008, 12:44:38 AM
Modern, I love you.
Title: Re: Ingame System SE Definition
Post by: modern algebra on March 05, 2008, 12:50:02 AM
I'm not into bestiality, sorry  :P
Title: Re: Ingame System SE Definition
Post by: Zylos on March 05, 2008, 12:58:46 AM
 :o Not that kind of love!

Anyway, the only complaint I have about the script is that you cannot change volume or pitch. (Or can you?)
Title: Re: Ingame System SE Definition
Post by: modern algebra on March 05, 2008, 01:21:27 AM
I was thinking of adding that actually, I'll fix it up in a second.

EDIT::

Bear in mind, you can easily change any single part of the SE with the codes:

$game_system.whatever_se.name = "filename"
$game_system.whatever_se.volume = value
$game_system.whatever_se.pitch = value

But I will make it possible that you can set everything in one line, for the sake of ease.

EDIT 2:: It's done now. Read the instructions to see how.
Title: Re: Ingame System SE Definition
Post by: hagotis on July 30, 2008, 09:57:03 PM
Ya I tried this. It didn't work. Even with a brand new project with no added scripts.
Title: Re: Ingame System SE Definition
Post by: modern algebra on July 30, 2008, 10:03:05 PM
I'm going to have to disagree with you there - I just tried it and it works fine. What does your code to change the sound look like (the call script part)?
Title: Re: Ingame System SE Definition
Post by: hagotis on July 30, 2008, 10:07:52 PM
script: $game_system.cursor_se = "beep5",80,50

that is what I have in my call script. probably missing something though.
Title: Re: Ingame System SE Definition
Post by: modern algebra on July 30, 2008, 10:15:06 PM
Does an error message come up or does it simply not change the SE, or does no SE play at all?
Title: Re: Ingame System SE Definition
Post by: hagotis on July 30, 2008, 10:20:28 PM
line 198 in the script
undefined methods "sound"

I have place the code for the script above main...can't see why that is the problem unless it needs to be somewhere in game_system.


Title: Re: Ingame System SE Definition
Post by: modern algebra on July 30, 2008, 10:25:37 PM
Well, above Main and below Materials, but that wouldn't be the error caused if you had it in the wrong position in the editor.

Anyway, so does this error happen as soon as you go into the game - I imagine it would. And is it undefined method 'sound' or 'sounds'?
Title: Re: Ingame System SE Definition
Post by: hagotis on July 30, 2008, 10:28:44 PM
Yes as soon as I start the game it appears. Even if I have don't have the call script event.

Okay what I did was have an event running with the call script with my desired sound change(which was the cursor).
That is it.

By the way, thanks for the quite replies. I really appreciate it. The same sound over and over again bores my game that I am creating.
Title: Re: Ingame System SE Definition
Post by: modern algebra on July 30, 2008, 10:31:25 PM
Can you upload that test project you made for me? Just zip it or rar it and attach it to your post through the additional options.

And it's no problem - I'm happy to help.
Title: Re: Ingame System SE Definition
Post by: hagotis on July 30, 2008, 10:36:48 PM
Here it is.

Oh, and I meant quick replies, not "quite".
Title: Re: Ingame System SE Definition
Post by: modern algebra on July 30, 2008, 10:39:52 PM
I figured as much.

And the #1 problem that is occuring is that you are using RMVX scripts in an RMXP project :P
Title: Re: Ingame System SE Definition
Post by: hagotis on July 30, 2008, 10:42:09 PM
Ahhhhhhhhhhhhhhhhhhhhhh, your kidding me!

Damn it ahahahaha. No wonder it wasn't working.

There is a script to do the same thing for XP I hope.


I feel like a damn idiot.
Title: Re: Ingame System SE Definition
Post by: modern algebra on July 30, 2008, 10:43:28 PM
Ah, don't feel badly :P

I don't know if there is a script to do the same thing for XP - it might be a little inconvenient.
Title: Re: Ingame System SE Definition
Post by: hagotis on July 30, 2008, 10:46:08 PM
Okay, it is no big deal anyway. Like I said, I just didn't like the same sound over and over. I guess I can just march on with the project anyways.
Thanks again for your help.
Title: Re: Ingame System SE Definition
Post by: modern algebra on June 05, 2010, 11:07:55 PM
I rewrote this script, so it is updated to Version 2.0. It can now modify all System Audio (except for Title BGM) and has a much nicer way to change them.
Title: Re: Ingame System SE Definition
Post by: cozziekuns on June 05, 2010, 11:22:24 PM
This is nice. I like how you didn't forget the pitch function, which almost everyone I know ignores. Also, would the save_data method work the same as the Marshal.dump method?
Title: Re: Ingame System SE Definition
Post by: modern algebra on June 06, 2010, 12:34:04 AM
Not really. save_data would do roughly the same thing in that it would use Marshal.dump but it would save it to a file. I didn't want to save it to a file ~ the only thing I wanted to do was clone all the elements of $data_system.sounds in addition to the array. In actuality, I didn't need to; I wrote it that way before I had decided not to leave any backup work for the original $data_system objects to do. But a simple $game_system.ma_sounds = $data_system.sounds would have been sufficient and probably a better design choice.
Title: Re: Ingame System SE Definition
Post by: cozziekuns on June 06, 2010, 01:50:14 AM
After reading over the help file again, it makes sense, I guess.

Thanks for the tip.