Hey guys! I'm in need of extension for conditional branch that would check if certain BGM file is played in the background. Help would be appreciated! :)
Hey! You can actually do this pretty easily with nested conditional branches. In the conditional branch menu, go into the 4th tab and select Script and insert
!$game_system.playing_bgm.nil?
This confirms that there is a BGM track playing at the moment. Then, inside of the branch, create a second conditional branch, again on the 4th tab with Script selected, and insert
$game_system.playing_bgm.name == "TRACKNAME"
Make sure you replace TRACKNAME with the name of the BGM file.
Ultimately, your event should look like this:
(https://i.imgur.com/jKweOJ3.png)
Thank you lilbrudder917! that's exactly what I needed. :)
Just saying, you could combine both of those into one statement instead of two
$game_system.playing_bgm.name == 'Theme' rescue false
It is less efficient using the one line with a rescue as opposed to using two lines.
It goes against Ruby coding practice to use rescue for anything other than when it is needed, for catching exceptions, which should be "exceptional". A BGM playing or not I don't think is really that exceptional of an event that couldn't be handled other ways.
The reasoning is that "rescue" in Ruby is a somewhat expensive operation, as it it builds an entire stacktrace by iterating through the call stack that it would not normally be doing.
Back when I coded for RGSS, I was guilty of the community's strange convention that "shorter, compact code is always better", but in reality, it is not the case at all. I have spent a lot of time digging around the C side of Ruby the past few months, and realized that there are many, many things that are the standard convention in the RM community, but are actually bad practices, and are not even conventional in Ruby.
Probably should have split this topic off before blabbering on...
EDIT: I do realize that in this particular situation, it hardly matters, as I don't imagine it is invoked often enough to matter, and it is an unneeded micro-optimization, but I speak more on the global scale of simply better coding practices.
Quote from: ForeverZer0 on March 22, 2018, 01:59:33 pm
there are many, many things that are the standard convention in the RM community, but are actually bad practices, and are not even conventional in Ruby.
Not just the Ruby community. ._.
Thanks guys. One more question - shouldn't
$game_system.playing_bgm.name != 'THEME'
return true if BGM name isn't 'THEME'? sometimes after changing map BGM doesn't change and this statement keeps returning false.
@F0: If you're so opposed to it, then just combine the two as
$game_system.playing_bgm && $game_system.playing_bgm.name == "TRACKNAME"
There's nothing stating you can't use and/or in Conditional Branch scripts.
@Kise: The logic is right, but if you're using what I suggested earlier, then you want to change the false to a true
$game_system.playing_bgm.name != 'Theme' rescue true
Quote from: KK20 on March 22, 2018, 02:19:52 pm
@F0: If you're so opposed to it, then just combine the two as
$game_system.playing_bgm && $game_system.playing_bgm.name == "TRACKNAME"
There's nothing stating you can't use and/or in Conditional Branch scripts.
There's not, but it will return a NoMethodError if no BGM is playing since playing_bgm returns nil.
Which is why the first check is there. You do know that nil is evaluated to false, right?
Ah, my bad. When I was trying it myself I did have it on one line with $game_system.playing_bgm.nil? but I think I definitely had to have made the mistake of not putting a ! in front of it.
Edit: Strangely though it still doesn't seem to work, neither true nor false options are evaluating.
Edit 2: !$game_system.playing_bgm.nil? && $game_system.playing_bgm.name == "whatever" works, for whatever reason $game_system.playing_bgm && $game_system.playing_bgm.name == "whatever" does not. Guess it's a RGSS quirk or something.
Quote from: KK20 on March 22, 2018, 02:19:52 pm
@F0: If you're so opposed to it, then just combine the two as
$game_system.playing_bgm && $game_system.playing_bgm.name == "TRACKNAME"
There's nothing stating you can't use and/or in Conditional Branch scripts.
I am not opposed to anything, lol, and I am as guilty as anyone in the community of trying to make everything a "one-liner" back when I wrote RGSS scripts. Over the years I have obviously learned more as everyone has, and cringe at some of the stuff I wrote that I was clever at the time, and I am sure years from now I will feel the same of my current code as I learn more.
I just found the convention of trying to compact every little bit of code into the shortest possible number of characters/lines a bit odd,
even when at the cost of performance, which is typically the reasoning behind trying to make compact code. Some irony there.
This is not an issue unique to RGSS, but looking back, it does seem to hold some greater measure in the community that it is somehow "better" if two different snippets do the same thing, but one is shorter than the other.
Obviously this is a case-by-case basis, and there is no "rule" that doesn't have plenty of exceptions, but just stating an observation. The above example is a somewhat poor example, and would merely be a micro-optimization that holds no sway over the whole at all.
I am against
enforcing any type of standard (I still remember SDK...), but I am for "good coding practice", even when it may be inconvenient for the situation. I wish I would have known things I know now about Ruby when I wrote scripts, but alas, that time has passed :P
I am far, far from being masterful, but I have recently picked Ruby back up as a hobby, and realizing the error of my ways back then. I would suggest to anyone who is a performance fanatic to really delve deep into the Ruby C API, and really see whats going on behind the scenes with each line in your Ruby script.
@lilbrudder917
Oh I see why now. Even though nil is treated as false in an if-statement, RMXP is not evaluating the string in an if-statement.
if $game_system.playing_bgm && $game_system.playing_bgm.name == "TRACKNAME" #=> false
$game_system.playing_bgm && $game_system.playing_bgm.name == "TRACKNAME" #=> nil
The problem occurs in the Else handling, when interpreter is looking specifically for false
#--------------------------------------------------------------------------
# * Else
#--------------------------------------------------------------------------
def command_411
# If determinant results are false
if @branch[@list[@index].indent] == false
So yeah I guess you need to do the check for if not nil.
@F0
Yeah I understand and completely agree with you. I mainly just follow https://github.com/bbatsov/ruby-style-guide when I do actual development work. For scripts and one-liners, I flex my fingers for out-of-the-box thinking.
I came across one more problem, custom script related. It's supposed to Fade in BGM, but it's causing some problems if fade out command is called before fade in is complete.
Here's a demo - https://nofile.io/f/KSjgM11Gfmb/Project.zip
#==============================================================================
# Self Variables Script v. 1.02
# by Caldaron (16.09.2006)
#==============================================================================
module FadeIn
#--------------------------------------------------------------------------
MAPS = [] # Map IDs in which the BGM fades in
FADE = 6 # Time in seconds of fade in
#--------------------------------------------------------------------------
end
#==============================================================================
module RPG
#--------------------------------------------------------------------------
class AudioFile
#--------------------------------------------------------------------------
def initialize(name = "", volume = 100, pitch = 100, fade = 0)
@name = name
@volume = volume
@pitch = pitch
@fade = fade
end
#--------------------------------------------------------------------------
attr_accessor :name
attr_accessor :volume
attr_accessor :pitch
attr_accessor :fade
end
#--------------------------------------------------------------------------
end
#==============================================================================
class Game_System
#--------------------------------------------------------------------------
alias fade_init initialize
#--------------------------------------------------------------------------
def initialize
fade_init
@time = 0
@wait = 10
@fade = false
@bgm_name = nil
@bgm_volume = 0
@bgm_pitch = 0
@bgm_wait = 0
end
#--------------------------------------------------------------------------
def bgm_play(bgm)
@fade = false
if bgm != nil and bgm.name != ""
if bgm.fade != nil
bgm_fade_in(bgm.name, bgm.volume, bgm.pitch, bgm.fade)
@playing_bgm = RPG::AudioFile.new(bgm.name, bgm.volume, bgm.pitch, FadeIn::FADE)
else
Audio.bgm_play("Audio/BGM/" + bgm.name, bgm.volume, bgm.pitch)
@playing_bgm = bgm
end
else
Audio.bgm_stop
end
Graphics.frame_reset
end
#--------------------------------------------------------------------------
def bgm_fade_in(name, volume, pitch, time)
@playing_bgm = RPG::AudioFile.new(name, volume, pitch, time)
@bgm_name = name
@bgm_volume = volume
@bgm_pitch = pitch
@bgm_wait = time
@fade = true
end
#--------------------------------------------------------------------------
alias fade_update update
#--------------------------------------------------------------------------
def update
if @fade
@wait -= 1
if @wait == 0
@time += @bgm_volume/(@bgm_wait * 4.00)
if @time >= @bgm_volume
Audio.bgm_play("Audio/BGM/" + @bgm_name, @bgm_volume, @bgm_pitch)
@fade = false
@bgm_name = nil
@bgm_volume = 0
@bgm_pitch = 0
@time = 10
else
Audio.bgm_play("Audio/BGM/" + @bgm_name, @time, @bgm_pitch)
end
@wait = 10
end
end
fade_update
end
#--------------------------------------------------------------------------
end
#==============================================================================
class Game_Map
#--------------------------------------------------------------------------
def autoplay
if @map.autoplay_bgm
if FadeIn::MAPS.include?(@map_id)
$game_system.bgm_fade_in(@map.bgm.name, @map.bgm.volume, @map.bgm.pitch, FadeIn::FADE)
else
$game_system.bgm_play(@map.bgm)
end
end
if @map.autoplay_bgs
$game_system.bgs_play(@map.bgs)
end
end
#--------------------------------------------------------------------------
end
#==============================================================================
class Scene_Battle
#--------------------------------------------------------------------------
alias fade_phase5 update_phase5
#--------------------------------------------------------------------------
def update_phase5
$game_system.bgm_stop
fade_phase5
end
#--------------------------------------------------------------------------
alias fade_end battle_end
#--------------------------------------------------------------------------
def battle_end(result)
fade_end(result)
$game_system.bgm_play($game_temp.map_bgm)
end
#--------------------------------------------------------------------------
end
Just add @fade = false to Game_System#bgm_fade. Or replace the script with this one:
#==============================================================================
# Self Variables Script v. 1.02
# by Caldaron (16.09.2006)
#==============================================================================
module FadeIn
#--------------------------------------------------------------------------
MAPS = [] # Map IDs in which the BGM fades in
FADE = 6 # Time in seconds of fade in
#--------------------------------------------------------------------------
end
#==============================================================================
module RPG
#--------------------------------------------------------------------------
class AudioFile
#--------------------------------------------------------------------------
def initialize(name = "", volume = 100, pitch = 100, fade = 0)
@name = name
@volume = volume
@pitch = pitch
@fade = fade
end
#--------------------------------------------------------------------------
attr_accessor :name
attr_accessor :volume
attr_accessor :pitch
attr_accessor :fade
end
#--------------------------------------------------------------------------
end
#==============================================================================
class Game_System
#--------------------------------------------------------------------------
alias fade_init initialize
#--------------------------------------------------------------------------
def initialize
fade_init
@time = 0
@wait = 10
@fade = false
@bgm_name = nil
@bgm_volume = 0
@bgm_pitch = 0
@bgm_wait = 0
end
#--------------------------------------------------------------------------
def bgm_play(bgm)
@fade = false
if bgm != nil and bgm.name != ""
if bgm.fade != nil
bgm_fade_in(bgm.name, bgm.volume, bgm.pitch, bgm.fade)
@playing_bgm = RPG::AudioFile.new(bgm.name, bgm.volume, bgm.pitch, FadeIn::FADE)
else
Audio.bgm_play("Audio/BGM/" + bgm.name, bgm.volume, bgm.pitch)
@playing_bgm = bgm
end
else
Audio.bgm_stop
end
Graphics.frame_reset
end
#--------------------------------------------------------------------------
def bgm_fade_in(name, volume, pitch, time)
@playing_bgm = RPG::AudioFile.new(name, volume, pitch, time)
@bgm_name = name
@bgm_volume = volume
@bgm_pitch = pitch
@bgm_wait = time
@fade = true
end
#--------------------------------------------------------------------------
# * Fade Out Background Music
# time : fade-out time (in seconds)
#--------------------------------------------------------------------------
alias bgm_fade_new_fade_out bgm_fade
def bgm_fade(time)
bgm_fade_new_fade_out(time)
@fade = false
end
#--------------------------------------------------------------------------
alias fade_update update
#--------------------------------------------------------------------------
def update
if @fade
@wait -= 1
if @wait == 0
@time += @bgm_volume/(@bgm_wait * 4.00)
if @time >= @bgm_volume
Audio.bgm_play("Audio/BGM/" + @bgm_name, @bgm_volume, @bgm_pitch)
@fade = false
@bgm_name = nil
@bgm_volume = 0
@bgm_pitch = 0
@time = 10
else
Audio.bgm_play("Audio/BGM/" + @bgm_name, @time, @bgm_pitch)
end
@wait = 10
end
end
fade_update
end
#--------------------------------------------------------------------------
end
#==============================================================================
class Game_Map
#--------------------------------------------------------------------------
def autoplay
if @map.autoplay_bgm
if FadeIn::MAPS.include?(@map_id)
$game_system.bgm_fade_in(@map.bgm.name, @map.bgm.volume, @map.bgm.pitch, FadeIn::FADE)
else
$game_system.bgm_play(@map.bgm)
end
end
if @map.autoplay_bgs
$game_system.bgs_play(@map.bgs)
end
end
#--------------------------------------------------------------------------
end
#==============================================================================
class Scene_Battle
#--------------------------------------------------------------------------
alias fade_phase5 update_phase5
#--------------------------------------------------------------------------
def update_phase5
$game_system.bgm_stop
fade_phase5
end
#--------------------------------------------------------------------------
alias fade_end battle_end
#--------------------------------------------------------------------------
def battle_end(result)
fade_end(result)
$game_system.bgm_play($game_temp.map_bgm)
end
#--------------------------------------------------------------------------
end
Yeah, that fixes most of the problem. :) It seems like for few loops in the demo with fixed script music gets louder instead of keeping the same volume raise each loop.
Sometimes even BGM kicks in at full volume for some reason.