Play SE On Balloon Pop-Up
Version: 1.0
Author: DiamondandPlatinum3
Date: July 31, 2012
Planned Future Versions
Looks good! The only thing that I would change is that I would move the variable definitions outside of the start_balloon method and I would make them class variables or else move them outside the class altogether. That would (a) be easier on memory since the value of the variables is the same for every instance of Sprite_Character; and (b) it would also make it easier to alias the start_balloon method if you ever needed to in some other script. Those aren't major benefits though.
You might also want to make the names of the variables a little more unique.
In the original script I had it inside of a module, but on one of my other scripts Pacman suggested that I use the variables in that script as local variables instead of instance variables :o
[spoiler=original script]
#=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=
# Play SE On Balloon Pop-Up
# Version: 1.0
# Author: DiamondandPlatinum3
# Date: July 2, 2012
#~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
# Description:
#
# This script allows you to play an SE (Sound Effect) when a ballon icon
# pops-up above someones head.
#~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
#------------------------------------------------------------------------------
# Instructions:
#
# - All you have to do is go into the editable region and change the
# appropriate settings to suit your needs. This includes which SE plays
# depending on the balloon icon that is being shown.
#
# - If you do not wish for a SE to play on any specific balloon icon,
# simply set the value to nil.
#
#=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=
module DP3_BalloonSE_Options
#=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-
# -=
# Editable Region //// ==
# =-
#-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=
SE_Volume = 80 # Volume of the SE
SE_Pitch = 100 # Pitch of the SE
Exclamation_SE = "Raise1" # SE played with Exclamation Balloon
Question_SE = "Confuse" # SE played with Question Balloon
Music_Note_SE = "Sound1" # SE played with Music Note Balloon
Heart_SE = "Recovery" # SE played with Heart Balloon
Anger_SE = "Buzzer1" # SE played with Anger Balloon
Sweat_SE = "Down" # SE played with Sweat Balloon
Cobweb_SE = "Sand" # SE played with Cobweb Balloon
Silence_SE = "Stare" # SE played with Silence Balloon
Lightbulb_SE = "Bell" # SE played with Lightbulb Balloon
Zzz_SE = nil # SE played with Zzz Balloon
#=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=
end # of Editable Region
#=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=
#---------------------------------------------------------
# No touchie past here unless you know what you are
# doing. Failure to heed this warning could cause your
# computer to yell and scream at you.
#
# Edit at your own risk.
#--------------------------------------------------------
class Sprite_Character < Sprite_Base
alias dp3_balloon_sfx_play_9huf start_balloon
def start_balloon
#Find out which balloon is being used and play the SE appropriately.
if @balloon_id == 1
RPG::SE.new(DP3_BalloonSE_Options::Exclamation_SE, DP3_BalloonSE_Options::SE_Volume,DP3_BalloonSE_Options::SE_Pitch).play if DP3_BalloonSE_Options::Exclamation_SE != nil
elsif @balloon_id == 2
RPG::SE.new(DP3_BalloonSE_Options::Question_SE, DP3_BalloonSE_Options::SE_Volume,DP3_BalloonSE_Options::SE_Pitch).play if DP3_BalloonSE_Options::Question_SE != nil
elsif @balloon_id == 3
RPG::SE.new(DP3_BalloonSE_Options::Music_Note_SE, DP3_BalloonSE_Options::SE_Volume,DP3_BalloonSE_Options::SE_Pitch).play if DP3_BalloonSE_Options::Music_Note_SE != nil
elsif @balloon_id == 4
RPG::SE.new(DP3_BalloonSE_Options::Heart_SE, DP3_BalloonSE_Options::SE_Volume,DP3_BalloonSE_Options::SE_Pitch).play if DP3_BalloonSE_Options::Heart_SE != nil
elsif @balloon_id == 5
RPG::SE.new(DP3_BalloonSE_Options::Anger_SE, DP3_BalloonSE_Options::SE_Volume,DP3_BalloonSE_Options::SE_Pitch).play if DP3_BalloonSE_Options::Anger_SE != nil
elsif @balloon_id == 6
RPG::SE.new(DP3_BalloonSE_Options::Sweat_SE, DP3_BalloonSE_Options::SE_Volume,DP3_BalloonSE_Options::SE_Pitch).play if DP3_BalloonSE_Options::Sweat_SE != nil
elsif @balloon_id == 7
RPG::SE.new(DP3_BalloonSE_Options::Cobweb_SE, DP3_BalloonSE_Options::SE_Volume,DP3_BalloonSE_Options::SE_Pitch).play if DP3_BalloonSE_Options::Cobweb_SE != nil
elsif @balloon_id == 8
RPG::SE.new(DP3_BalloonSE_Options::Silence_SE, DP3_BalloonSE_Options::SE_Volume,DP3_BalloonSE_Options::SE_Pitch).play if DP3_BalloonSE_Options::Silence_SE != nil
elsif @balloon_id == 9
RPG::SE.new(DP3_BalloonSE_Options::Lightbulb_SE, DP3_BalloonSE_Options::SE_Volume,DP3_BalloonSE_Options::SE_Pitch).play if DP3_BalloonSE_Options::Lightbulb_SE != nil
elsif @balloon_id == 10
RPG::SE.new(DP3_BalloonSE_Options::Zzz_SE, DP3_BalloonSE_Options::SE_Volume,DP3_BalloonSE_Options::SE_Pitch).play if DP3_BalloonSE_Options::Zzz_SE != nil
end
dp3_balloon_sfx_play_9huf
end # of function
end # of class
[/spoiler]
Any suggestions on when I should use modules and when I should use locals?
(Original script was for VX, was just porting it over to VXA cause I thought it might be useful for guilds; Only I found out that it was completely compatible (save for a few sound files that VXA seems to have changed)).
First, your post demonstrated a little confusion with terminology regarding variables and constants, in that you refer to constants in a module as instance variables and instance variables as local. The following tutorial isn't great at explaining when to use each, but it should help with the terminology: http://www.techotopia.com/index.php/Ruby_Variable_Scope#What_is_Variable_Scope.3F
With regard to your question about modules, when people use them as a place to configure their scripts, it is generally to create a unique namespace. So you might want to do that if you have a lot of constants to set and they have similar names to what other people might use in their own scripts. It's mostly just a scripting preference whether you house your configuration inside a module. It can make things a lot neater.1
Anyway, I wasn't saying that you should put them in a module necessarily; I just don't think you should use instance variables to hold configuration unless you would ever want the SE played to change depending on which instance of the class is calling the method. In other words, if you want one sprite's Sweat Balloon to make one sound and you want another sprite's Sweat Balloon to make a different sound, then you would use instance variables. Otherwise you are wasting memory by having every instance of a class hold the same values.
With few exceptions, constants are what you want to use for configuration. I suspect that what Pacman meant when he said to localize them was not to make them instance variables, but simply to house the constants in the Sprite_Character class, like so:
#=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=
# Play SE On Balloon Pop-Up
# Version: 1.0
# Author: DiamondandPlatinum3
# Date: July 2, 2012
#~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
# Description:
#
# This script allows you to play an SE (Sound Effect) when a ballon icon
# pops-up above someones head.
#~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
#------------------------------------------------------------------------------
# Instructions:
#
# - All you have to do is go into the editable region and change the
# appropriate settings to suit your needs. This includes which SE plays
# depending on the balloon icon that is being shown.
#
# - If you do not wish for a SE to play on any specific balloon icon,
# simply set the value to nil.
#
#=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=
class Sprite_Character < Sprite_Base
#=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-
# -=
# Editable Region //// ==
# =-
#-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=
SE_Volume = 80 # Volume of the SE
SE_Pitch = 100 # Pitch of the SE
Exclamation_SE = "Raise1" # SE played with Exclamation Balloon
Question_SE = "Confuse" # SE played with Question Balloon
Music_Note_SE = "Sound1" # SE played with Music Note Balloon
Heart_SE = "Recovery" # SE played with Heart Balloon
Anger_SE = "Buzzer1" # SE played with Anger Balloon
Sweat_SE = "Down" # SE played with Sweat Balloon
Cobweb_SE = "Sand" # SE played with Cobweb Balloon
Silence_SE = "Stare" # SE played with Silence Balloon
Lightbulb_SE = "Bell" # SE played with Lightbulb Balloon
Zzz_SE = nil # SE played with Zzz Balloon
#=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=
#---------------------------------------------------------
# No touchie past here unless you know what you are
# doing. Failure to heed this warning could cause your
# computer to yell and scream at you.
#
# Edit at your own risk.
#--------------------------------------------------------
alias dp3_balloon_sfx_play_9huf start_balloon
def start_balloon
#Find out which balloon is being used and play the SE appropriately.
if @balloon_id == 1
RPG::SE.new(Exclamation_SE, SE_Volume,SE_Pitch).play if Exclamation_SE != nil
elsif @balloon_id == 2
RPG::SE.new(Question_SE, SE_Volume,SE_Pitch).play if Question_SE != nil
elsif @balloon_id == 3
RPG::SE.new(Music_Note_SE, SE_Volume,SE_Pitch).play if Music_Note_SE != nil
elsif @balloon_id == 4
RPG::SE.new(Heart_SE, SE_Volume,SE_Pitch).play if Heart_SE != nil
elsif @balloon_id == 5
RPG::SE.new(Anger_SE, SE_Volume,SE_Pitch).play if Anger_SE != nil
elsif @balloon_id == 6
RPG::SE.new(Sweat_SE, SE_Volume,SE_Pitch).play if Sweat_SE != nil
elsif @balloon_id == 7
RPG::SE.new(Cobweb_SE, SE_Volume,SE_Pitch).play if Cobweb_SE != nil
elsif @balloon_id == 8
RPG::SE.new(Silence_SE, SE_Volume,SE_Pitch).play if Silence_SE != nil
elsif @balloon_id == 9
RPG::SE.new(Lightbulb_SE, SE_Volume,SE_Pitch).play if Lightbulb_SE != nil
elsif @balloon_id == 10
RPG::SE.new(Zzz_SE, SE_Volume,SE_Pitch).play if Zzz_SE != nil
end
dp3_balloon_sfx_play_9huf
end # of function
end # of class
1 Modules have more important uses too, such as multiple inheritance, but that is not related to your question.
Ah ok, that makes sense :)
I think I'll stick with a module for this script so that people can easily read it and edit what they need to, I'll also fix that other script up (seems I misunderstood Pacman ;9).
Yeah I got the terminology wrong, sorry about that :-[ a lot of things coding related seems to have blown over my head lately.
As for your comment about multiple inheritance...
is this what you mean?
module DP3
#Base code here (if necessary)
module Sprite
#code here
end
module Battle
#code here
end
end
@something = DP3::Sprite::Something # obviously won't work as it is now
As always, thanks for your mentoring Modern. I truly appreciate it :)
No, what I mean by multiple inheritance is that you can mix modules in with classes. Like this:
module D
def talk5
p "blablablablabla"
end
def talk4
p "blablablabla"
end
end
module C
def talk3
p "blablabla"
end
end
class B
def talk2
p "blabla"
end
end
class A < B
include C
include D
def talk1
p "bla"
end
end
a = A.new
a.talk1 # "bla"
a.talk2 # "blabla"
a.talk3 # "blablabla"
a.talk4 # "blablablabla"
a.talk5 # "blablablablabla"
In other words, the methods in the module become available to a class into which it is mixed.
Awesome, I think I get it :D
Oh! this script will make my life easier with less tedious eventing. Thank you for posting this!!!
Most welcome you are :)
Very useful, thanks. :)