Conditional Branch addon

Started by Kise, March 22, 2018, 07:52:22 am

Previous topic - Next topic

Kise

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! :)

lilbrudder917

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:
Spoiler: ShowHide

Kise

Thank you lilbrudder917! that's exactly what I needed. :)

KK20

Just saying, you could combine both of those into one statement instead of two

$game_system.playing_bgm.name == 'Theme' rescue false

Other Projects
RPG Maker XP Ace  Upgrade RMXP to RMVXA performance!
XPA Tilemap  Tilemap rewrite with many features, including custom resolution!

Nintendo Switch Friend Code: 8310-1917-5318
Discord: KK20 Tyler#8901

Join the CP Discord Server!

ForeverZer0

March 22, 2018, 01:59:33 pm #4 Last Edit: March 22, 2018, 02:02:35 pm by ForeverZer0
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.
I am done scripting for RMXP. I will likely not offer support for even my own scripts anymore, but feel free to ask on the forum, there are plenty of other talented scripters that can help you.

Blizzard

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. ._.
Check out Daygames and our games:

King of Booze 2      King of Booze: Never Ever
Drinking Game for Android      Never have I ever for Android
Drinking Game for iOS      Never have I ever for iOS


Quote from: winkioI do not speak to bricks, either as individuals or in wall form.

Quote from: Barney StinsonWhen I get sad, I stop being sad and be awesome instead. True story.

Kise

March 22, 2018, 02:13:09 pm #6 Last Edit: March 22, 2018, 02:14:14 pm by Kise
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.

KK20

@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


Other Projects
RPG Maker XP Ace  Upgrade RMXP to RMVXA performance!
XPA Tilemap  Tilemap rewrite with many features, including custom resolution!

Nintendo Switch Friend Code: 8310-1917-5318
Discord: KK20 Tyler#8901

Join the CP Discord Server!

lilbrudder917

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.

KK20

Which is why the first check is there. You do know that nil is evaluated to false, right?

Other Projects
RPG Maker XP Ace  Upgrade RMXP to RMVXA performance!
XPA Tilemap  Tilemap rewrite with many features, including custom resolution!

Nintendo Switch Friend Code: 8310-1917-5318
Discord: KK20 Tyler#8901

Join the CP Discord Server!

lilbrudder917

March 22, 2018, 02:30:26 pm #10 Last Edit: March 22, 2018, 02:34:46 pm by lilbrudder917
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.

ForeverZer0

March 22, 2018, 02:43:01 pm #11 Last Edit: March 22, 2018, 02:44:13 pm by ForeverZer0
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.
I am done scripting for RMXP. I will likely not offer support for even my own scripts anymore, but feel free to ask on the forum, there are plenty of other talented scripters that can help you.

KK20

@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.

Other Projects
RPG Maker XP Ace  Upgrade RMXP to RMVXA performance!
XPA Tilemap  Tilemap rewrite with many features, including custom resolution!

Nintendo Switch Friend Code: 8310-1917-5318
Discord: KK20 Tyler#8901

Join the CP Discord Server!

Kise

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

lilbrudder917

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

Kise

March 22, 2018, 03:24:03 pm #15 Last Edit: March 22, 2018, 03:48:39 pm by Kise
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.