[VX ACE] BGM PLAY SCRIPTING HELP?

Posts

Pages: 1
So I'm trying to get started in Ruby scripting and I thought to start with something simple. I wanted to make a script were if a button was pressed a specific BGM would play. I am able to run the game with the script except the BGM isn't playing after I press the button.

class RPG::AudioFile
def initialize(name = '', volume = 100, pitch = 100)
@name = name
@volume = volume
@pitch = pitch
end
attr_accessor :name
attr_accessor :volume
attr_accessor :pitch
end
class RPG::BGM < RPG::AudioFile
@@last = RPG::BGM.new
def play(pos = 0)
if @name.empty?
Audio.bgm_stop
@@last = RPG::BGM.new
else
Audio.bgm_play('Audio/BGM/' + @name, @volume, @pitch, pos)
@@last = self.clone
end
end
def replay
play(@pos)
end
def self.stop
Audio.bgm_stop
@@last = RPG::BGM.new
end
def self.fade(time)
Audio.bgm_fade(time)
@@last = RPG::BGM.new
end
def self.last
@@last.pos = Audio.bgm_pos
@@last
end
attr_accessor :pos
end
module Radio
Radio_1 = :Shift
end
module CarRadio
if Input.trigger?(Radio::Radio_1)
Audio.bgm_play('Town1'[100[100]])
else
end
end

Thoughts?
class RPG::AudioFile 
  def initialize(name = '', volume = 100, pitch = 100) 
    @name = name 
    @volume = volume 
    @pitch = pitch 
  end 
  attr_accessor :name 
  attr_accessor :volume 
  attr_accessor :pitch 
end 

class RPG::BGM < RPG::AudioFile 
  @@last = RPG::BGM.new 
  def play(pos = 0) 
    if @name.empty? 
      Audio.bgm_stop 
      @@last = RPG::BGM.new 
    else 
      Audio.bgm_play('Audio/BGM/' + @name, @volume, @pitch, pos) 
      @@last = self.clone 
    end 
  end 
  def replay 
    play(@pos) 
  end 
  def self.stop 
    Audio.bgm_stop 
    @@last = RPG::BGM.new 
  end 
  def self.fade(time) 
    Audio.bgm_fade(time) 
    @@last = RPG::BGM.new 
  end 
  def self.last 
    @@last.pos = Audio.bgm_pos 
    @@last 
  end 
  attr_accessor :pos 
end 

module Radio 
  Radio_1 = :Shift 
end 

module CarRadio 
  if Input.trigger?(Radio::Radio_1) 
    Audio.bgm_play('Town1'[100[100]]) 
  else 
  end 
end


I've formatted the code for you so it becomes more readable.

I'm not really sure what you are trying to do here, honestly. Why are you redefining the BGM and audiofile classes?
If you're new to Ruby, it's also probably not the best idea to start with modules. I haven't done anything with modules yet as well, so can't help you with that. (I could get your example with the radio module)

The reason your code doesn't work is because it is never run. A method doesn't run by itself, but must be called by some other method, also you don't even have a method in your CarRadio module, so you can't call the input check.

You also seem to have misunderstood the documentation. In programming documentation, the brackets usually mean that whatever is within the brackets is optional. Therefore Audio.bgm_play(filename[, volume[, pitch]]) is read as follows: you always need the filename operator, while the other three are optional. If you want pos, though, you need to have both pitch and volume, if you need pitch, you have to have volume. That's why the brackets are nested like that. Between the parameters you have to put the commas.

When you want to add a new command for the player (I assume it's just meant to be on the normal map), you should first look through the code, where player-input is processed. It's organized a bit weirdly, but this happens in the Game_Player class, in the update method.

Therefore, the easiest way to accomplish what you want to do is to override the Game_Player class and extend the functionality of the update class with your input check.

Insert an entry between Materials and (Insert Here) in the Script window and call it Game_Player, then copy this code into it:

#==============================================================================
# ** Game_Player
#------------------------------------------------------------------------------
#  This class handles the player. It includes event starting determinants and
# map scrolling functions. The instance of this class is referenced by
# $game_player.
#==============================================================================

class Game_Player < Game_Character
  #--------------------------------------------------------------------------
  # * Frame Update
  #--------------------------------------------------------------------------
  alias update_default update
  def update
    update_default
    update_bgm
  end
  #--------------------------------------------------------------------------
  # * BGM Update (change music when pressing shift)
  #--------------------------------------------------------------------------
  def update_bgm
    if Input.trigger?(:A) 
      Audio.bgm_play("Audio/BGM/Town1",100,100) 
    end 
  end
end


I'd suggest using :A instead of :SHIFT, since this one works both when pressing shift on a keyboard and the dash button on a controller, unless you want it to be keyboard only for some reason. (I at least prefer to play RM games with a controller)

Also, you have to put the full path to the music file into the bgm_play function call, otherwise it won't the file.

To make custom scripts you really have to learn your way around the RGSS code and how everything is organized. The easiest way is to figure out some functionality that's already in there and is close enough to what you want to do that you can learn from the implementation. Like for this example, looking up where the button checks for player input are defined and how to play music.

A good way to learn how everything is organized is to try to implement a custom menu of some sort, or change around one of the standard menu. Gives you some insight into how scenes and windows interact.
Well, that's how I started the ruby scripting. (then again, I've got prior programming knowledge)

Hope that long rambly post helps you a bit.
Thank you so much for this! I just have one question. Is there a way to edit this script so that when the song stops playing another song plays directly after rather than looping the same song again? Thank you!
You'd have to figure out when the song ends and then pay a new bgm. If you want to create a full on radio feature like in GTA, it's going to be a bit of work.
I'm not too familiar with the sound stuff in the RGSS, from what I see you can get the playback position of an ogg or wav file and figure it out with that, but for MP3s... the only way I can think of right now is doing it "per hand", having a timer that switches the song after it's time is up.
You'd have to put in the times for each song instead of detecting them, though, which is... sub-optimal.
Pages: 1