Okay, so I have a script which shows health bars for enemies in battle. I'm trying to make it so the user can select an option where they can turn the script on or off, but I have absolutely NO idea how to do it... Probably a stupid question, but we all gotta start somewhere, right? 😛
Conditional Branch Scripting
RGSSx Script SupportI've moved this thread to RGSSx Script Support. Please be sure to post your threads in the correct forum next time. Thank you.
Determine HOW the user will choose to turn it on or off - via the menu? By a Show Choices command at the beginning of the game? By using an item?
However it's done, you need to allocate a switch to indicate whether it should be on or off. And will the script be enabled or disabled by default? If enabled by default, then the switch being OFF means to run the script, and the switch being turned ON means to disable the script.
Then in your script, just add an if $game_switches[id] condition around the script that displays the health bars, where id is the number of the switch you used for this purpose (don't include leading zeros).
If you need more help than that, you're going to have to answer those questions and provide the script you're using (or a link to the script).
Determine HOW the user will choose to turn it on or off - via the menu? By a Show Choices command at the beginning of the game? By using an item?
However it's done, you need to allocate a switch to indicate whether it should be on or off. And will the script be enabled or disabled by default? If enabled by default, then the switch being OFF means to run the script, and the switch being turned ON means to disable the script.
Then in your script, just add an if $game_switches[id] condition around the script that displays the health bars, where id is the number of the switch you used for this purpose (don't include leading zeros).
If you need more help than that, you're going to have to answer those questions and provide the script you're using (or a link to the script).
My apologies, I thought that that was in the right place, didn't know if it was like a conditional branch or an if statement in the script. Sorry again.
That solves my question 🙂 So like, it would be like this?
if $game_switches[###]
rest of code
end
That solves my question 🙂 So like, it would be like this?
if $game_switches[###]
rest of code
end
It depends on what rest of code is. If you want to post the script, or a link to it, I can tell you the best place to put it.
#--# Basic Enemy HP Bars v 2.5
#
# Adds customizable hp bars to enemies in battle. See configuration
# below for more detail. Also allows for the option of using a nice
# graphical hp bar from a image file.
#
# Usage: Plug and play, customize as needed.
#
#------#
#-- Script by: V.M of D.T
#
#- Questions or comments can be:
# posted on the thread for the script
# given by email: [email hidden]
# provided on facebook: http://www.facebook....niousTailsGames
# posed on site: daimonioustails.wordpress.com
#
#- Free to use in any project with credit given, donations always welcome!
#Customization starts here:
module DTP_HP
#Whether to place the hp bar above or below the enemy
ABOVE_MONSTER = true
#Whether to use a custome image or not:
#Image would be placed in Graphics/System and named Custom_HP.png
CUSTOM_BAR = false
#The width of the hp bar
BAR_WIDTH = 66
#The height of the hp bar
BAR_HEIGHT = 5
#The width of the border around the hp bar
BORDER_WIDTH = 1
#The height of the border around the hp bar
BORDER_HEIGHT = 1
#Offset the hp bar along the x-axis(left,right)
BAR_OFFSET_X = 0
#Offset the hp bar along the y-axis(up,down)
BAR_OFFSET_Y = 0
#Color for the back of the hp bar
COLOR_BAR_BACK = Color.new(0,0,0,200)
#First color for the hp bar gradient
COLOR_BAR_1 = Color.new(255,0,0)
#Second color for the hp bar gradient
COLOR_BAR_2 = Color.new(200,100,100)
#Outside border color
COLOR_BORDER_1 = Color.new(0,0,0,185)
#Inside border color
COLOR_BORDER_2 = Color.new(255,255,255,185)
#Whether to display text or not
USE_TEXT = true
#Text to be displayed, chp = current hp, mhp = max hp, php = percentage hp
#Examples: "php%" or "chp/mhp" or "chp - php%"
TEXT_DISPLAY = "chp"
#Offset for the text along the x-axis(left,right)
TEXT_OFFSET_X = 5
#Offset for the text along the y-axis(up,down)
TEXT_OFFSET_Y = -24
#Size of the displayed text
TEXT_SIZE = Font.default_size
#Font of the displayed text
TEXT_FONT = Font.default_name
#Show bars only when specific actor in party. Array format. Example: [8,7]
#Set to [] to not use actor only
SPECIFIC_ACTOR = []
#Show enemy hp bar only if certain state is applied (like a scan state)
#Set to 0 to not use state only
SCAN_STATE = 0
#Enemies will show hp bar as long as they have been affected but scan state
#at least once before
SCAN_ONCE = false
#Text to display if it's a boss monster, accepts same arguments
BOSS_TEXT = "???"
#The width of the boss hp bar
BOSS_BAR_WIDTH = 66
#The height of the boss hp bar
BOSS_BAR_HEIGHT = 5
#The width of the border around the boss hp bar
BOSS_BORDER_WIDTH = 1
#The height of the border around the boss hp bar
BOSS_BORDER_HEIGHT = 1
#ID's of boss monsters in array format.
BOSS_MONSTERS = []
end
#Customization ends here
class Sprite_Battler
alias hpbar_update update
alias hpbar_dispose dispose
def update
hpbar_update
return unless @battler.is_a?(Game_Enemy)
if @battler
update_hp_bar
end
end
def update_hp_bar
boss = DTP_HP::BOSS_MONSTERS.include?(@battler.enemy_id)
if @hp_bar.nil?
@hp_bar = Sprite_Base.new(self.viewport)
if !boss
width = DTP_HP::BAR_WIDTH + DTP_HP::BORDER_WIDTH * 4
height = DTP_HP::BAR_HEIGHT + DTP_HP::BORDER_HEIGHT * 4
else
width = DTP_HP::BOSS_BAR_WIDTH + DTP_HP::BOSS_BORDER_WIDTH * 4
height = DTP_HP::BOSS_BAR_HEIGHT + DTP_HP::BOSS_BORDER_HEIGHT * 4
end
@hp_bar.bitmap = Bitmap.new(width,height)
@hp_bar.x = self.x - @hp_bar.width / 2 + DTP_HP::BAR_OFFSET_X
@hp_bar.y = self.y + DTP_HP::BAR_OFFSET_Y - self.bitmap.height - @hp_bar.height
@hp_bar.y = self.y + DTP_HP::BAR_OFFSET_Y unless DTP_HP::ABOVE_MONSTER
@hp_bar.x = 0 if @hp_bar.x < 0
@hp_bar.y = 0 if @hp_bar.y < 0
@hp_bar.z = 98
end
if @text_display.nil?
@text_display = Sprite_Base.new(self.viewport)
@text_display.bitmap = Bitmap.new(100,DTP_HP::TEXT_SIZE)
@text_display.bitmap.font.size = DTP_HP::TEXT_SIZE
@text_display.bitmap.font.name = DTP_HP::TEXT_FONT
@text_display.x = @hp_bar.x + DTP_HP::TEXT_OFFSET_X
@text_display.y = @hp_bar.y + DTP_HP::TEXT_OFFSET_Y
@text_display.z = 99
end
determine_visible
return unless @hp_bar.visible
@hp_bar.opacity = self.opacity if @hp_bar.opacity != self.opacity
@hp_bar.bitmap.clear
if !boss
width = DTP_HP::BAR_WIDTH
height = DTP_HP::BAR_HEIGHT
bwidth = DTP_HP::BORDER_WIDTH
bheight = DTP_HP::BORDER_HEIGHT
else
width = DTP_HP::BOSS_BAR_WIDTH
height = DTP_HP::BOSS_BAR_HEIGHT
bwidth = DTP_HP::BOSS_BORDER_WIDTH
bheight = DTP_HP::BOSS_BORDER_HEIGHT
end
btotal = (bwidth + bheight) * 2
rwidth = @hp_bar.bitmap.width
rheight = @hp_bar.bitmap.height
if !DTP_HP::CUSTOM_BAR
@hp_bar.bitmap.fill_rect(0,0,rwidth,rheight,DTP_HP::COLOR_BAR_BACK)
@hp_bar.bitmap.fill_rect(bwidth,bheight,rwidth-bwidth*2,rheight-bheight*2,DTP_HP::COLOR_BORDER_2)
@hp_bar.bitmap.fill_rect(bwidth*2,bheight*2,width,height,DTP_HP::COLOR_BORDER_1)
end
hp_width = @battler.hp_rate * width
@hp_bar.bitmap.gradient_fill_rect(bwidth*2,bheight*2,hp_width,height,DTP_HP::COLOR_BAR_1,DTP_HP::COLOR_BAR_2)
if DTP_HP::CUSTOM_BAR
border_bitmap = Bitmap.new("Graphics/System/Custom_HP.png")
rect = Rect.new(0,0,border_bitmap.width,border_bitmap.height)
@hp_bar.bitmap.blt(0,0,border_bitmap,rect)
end
return unless DTP_HP::USE_TEXT
@text_display.opacity = @hp_bar.opacity if @text_display.opacity != @hp_bar.opacity
@text_display.bitmap.clear
text = DTP_HP::TEXT_DISPLAY.clone
text = DTP_HP::BOSS_TEXT.clone if DTP_HP::BOSS_MONSTERS.include?(@battler.enemy_id)
text.gsub!(/chp/) {@battler.hp}
text.gsub!(/mhp/) {@battler.mhp}
text.gsub!(/php/) {(@battler.hp_rate * 100).to_i}
@text_display.bitmap.draw_text(0,0,100,@text_display.height,text)
end
def determine_visible
if [email protected]?
@hp_bar.visible = false
@text_display.visible = false
return if [email protected]?
end
@hp_bar.visible = true
if DTP_HP::SCAN_STATE != 0
@hp_bar.visible = false
@hp_bar.visible = true if @battler.state?(DTP_HP::SCAN_STATE)
@hp_bar.visible = true if $game_party.monster_scans[@battler.enemy_id] == true
$game_party.monster_scans[@battler.enemy_id] = true if @hp_bar.visible
end
if !DTP_HP::SPECIFIC_ACTOR.empty?
@hp_bar.visible = false unless DTP_HP::SCAN_STATE != 0
DTP_HP::SPECIFIC_ACTOR.each do |i|
next unless $game_party.battle_members.include?($game_actors)
@hp_bar.visible = true
end
end
@text_display.visible = false if !@hp_bar.visible
@text_display.visible = true if @hp_bar.visible
end
def dispose
@hp_bar.dispose if @hpbar
@text_display.dispose if @text_display
hpbar_dispose
end
end
class Game_Party
alias hp_bar_init initialize
attr_accessor :monster_scans
def initialize
hp_bar_init
@monster_scans = []
end
end
#
# Adds customizable hp bars to enemies in battle. See configuration
# below for more detail. Also allows for the option of using a nice
# graphical hp bar from a image file.
#
# Usage: Plug and play, customize as needed.
#
#------#
#-- Script by: V.M of D.T
#
#- Questions or comments can be:
# posted on the thread for the script
# given by email: [email hidden]
# provided on facebook: http://www.facebook....niousTailsGames
# posed on site: daimonioustails.wordpress.com
#
#- Free to use in any project with credit given, donations always welcome!
#Customization starts here:
module DTP_HP
#Whether to place the hp bar above or below the enemy
ABOVE_MONSTER = true
#Whether to use a custome image or not:
#Image would be placed in Graphics/System and named Custom_HP.png
CUSTOM_BAR = false
#The width of the hp bar
BAR_WIDTH = 66
#The height of the hp bar
BAR_HEIGHT = 5
#The width of the border around the hp bar
BORDER_WIDTH = 1
#The height of the border around the hp bar
BORDER_HEIGHT = 1
#Offset the hp bar along the x-axis(left,right)
BAR_OFFSET_X = 0
#Offset the hp bar along the y-axis(up,down)
BAR_OFFSET_Y = 0
#Color for the back of the hp bar
COLOR_BAR_BACK = Color.new(0,0,0,200)
#First color for the hp bar gradient
COLOR_BAR_1 = Color.new(255,0,0)
#Second color for the hp bar gradient
COLOR_BAR_2 = Color.new(200,100,100)
#Outside border color
COLOR_BORDER_1 = Color.new(0,0,0,185)
#Inside border color
COLOR_BORDER_2 = Color.new(255,255,255,185)
#Whether to display text or not
USE_TEXT = true
#Text to be displayed, chp = current hp, mhp = max hp, php = percentage hp
#Examples: "php%" or "chp/mhp" or "chp - php%"
TEXT_DISPLAY = "chp"
#Offset for the text along the x-axis(left,right)
TEXT_OFFSET_X = 5
#Offset for the text along the y-axis(up,down)
TEXT_OFFSET_Y = -24
#Size of the displayed text
TEXT_SIZE = Font.default_size
#Font of the displayed text
TEXT_FONT = Font.default_name
#Show bars only when specific actor in party. Array format. Example: [8,7]
#Set to [] to not use actor only
SPECIFIC_ACTOR = []
#Show enemy hp bar only if certain state is applied (like a scan state)
#Set to 0 to not use state only
SCAN_STATE = 0
#Enemies will show hp bar as long as they have been affected but scan state
#at least once before
SCAN_ONCE = false
#Text to display if it's a boss monster, accepts same arguments
BOSS_TEXT = "???"
#The width of the boss hp bar
BOSS_BAR_WIDTH = 66
#The height of the boss hp bar
BOSS_BAR_HEIGHT = 5
#The width of the border around the boss hp bar
BOSS_BORDER_WIDTH = 1
#The height of the border around the boss hp bar
BOSS_BORDER_HEIGHT = 1
#ID's of boss monsters in array format.
BOSS_MONSTERS = []
end
#Customization ends here
class Sprite_Battler
alias hpbar_update update
alias hpbar_dispose dispose
def update
hpbar_update
return unless @battler.is_a?(Game_Enemy)
if @battler
update_hp_bar
end
end
def update_hp_bar
boss = DTP_HP::BOSS_MONSTERS.include?(@battler.enemy_id)
if @hp_bar.nil?
@hp_bar = Sprite_Base.new(self.viewport)
if !boss
width = DTP_HP::BAR_WIDTH + DTP_HP::BORDER_WIDTH * 4
height = DTP_HP::BAR_HEIGHT + DTP_HP::BORDER_HEIGHT * 4
else
width = DTP_HP::BOSS_BAR_WIDTH + DTP_HP::BOSS_BORDER_WIDTH * 4
height = DTP_HP::BOSS_BAR_HEIGHT + DTP_HP::BOSS_BORDER_HEIGHT * 4
end
@hp_bar.bitmap = Bitmap.new(width,height)
@hp_bar.x = self.x - @hp_bar.width / 2 + DTP_HP::BAR_OFFSET_X
@hp_bar.y = self.y + DTP_HP::BAR_OFFSET_Y - self.bitmap.height - @hp_bar.height
@hp_bar.y = self.y + DTP_HP::BAR_OFFSET_Y unless DTP_HP::ABOVE_MONSTER
@hp_bar.x = 0 if @hp_bar.x < 0
@hp_bar.y = 0 if @hp_bar.y < 0
@hp_bar.z = 98
end
if @text_display.nil?
@text_display = Sprite_Base.new(self.viewport)
@text_display.bitmap = Bitmap.new(100,DTP_HP::TEXT_SIZE)
@text_display.bitmap.font.size = DTP_HP::TEXT_SIZE
@text_display.bitmap.font.name = DTP_HP::TEXT_FONT
@text_display.x = @hp_bar.x + DTP_HP::TEXT_OFFSET_X
@text_display.y = @hp_bar.y + DTP_HP::TEXT_OFFSET_Y
@text_display.z = 99
end
determine_visible
return unless @hp_bar.visible
@hp_bar.opacity = self.opacity if @hp_bar.opacity != self.opacity
@hp_bar.bitmap.clear
if !boss
width = DTP_HP::BAR_WIDTH
height = DTP_HP::BAR_HEIGHT
bwidth = DTP_HP::BORDER_WIDTH
bheight = DTP_HP::BORDER_HEIGHT
else
width = DTP_HP::BOSS_BAR_WIDTH
height = DTP_HP::BOSS_BAR_HEIGHT
bwidth = DTP_HP::BOSS_BORDER_WIDTH
bheight = DTP_HP::BOSS_BORDER_HEIGHT
end
btotal = (bwidth + bheight) * 2
rwidth = @hp_bar.bitmap.width
rheight = @hp_bar.bitmap.height
if !DTP_HP::CUSTOM_BAR
@hp_bar.bitmap.fill_rect(0,0,rwidth,rheight,DTP_HP::COLOR_BAR_BACK)
@hp_bar.bitmap.fill_rect(bwidth,bheight,rwidth-bwidth*2,rheight-bheight*2,DTP_HP::COLOR_BORDER_2)
@hp_bar.bitmap.fill_rect(bwidth*2,bheight*2,width,height,DTP_HP::COLOR_BORDER_1)
end
hp_width = @battler.hp_rate * width
@hp_bar.bitmap.gradient_fill_rect(bwidth*2,bheight*2,hp_width,height,DTP_HP::COLOR_BAR_1,DTP_HP::COLOR_BAR_2)
if DTP_HP::CUSTOM_BAR
border_bitmap = Bitmap.new("Graphics/System/Custom_HP.png")
rect = Rect.new(0,0,border_bitmap.width,border_bitmap.height)
@hp_bar.bitmap.blt(0,0,border_bitmap,rect)
end
return unless DTP_HP::USE_TEXT
@text_display.opacity = @hp_bar.opacity if @text_display.opacity != @hp_bar.opacity
@text_display.bitmap.clear
text = DTP_HP::TEXT_DISPLAY.clone
text = DTP_HP::BOSS_TEXT.clone if DTP_HP::BOSS_MONSTERS.include?(@battler.enemy_id)
text.gsub!(/chp/) {@battler.hp}
text.gsub!(/mhp/) {@battler.mhp}
text.gsub!(/php/) {(@battler.hp_rate * 100).to_i}
@text_display.bitmap.draw_text(0,0,100,@text_display.height,text)
end
def determine_visible
if [email protected]?
@hp_bar.visible = false
@text_display.visible = false
return if [email protected]?
end
@hp_bar.visible = true
if DTP_HP::SCAN_STATE != 0
@hp_bar.visible = false
@hp_bar.visible = true if @battler.state?(DTP_HP::SCAN_STATE)
@hp_bar.visible = true if $game_party.monster_scans[@battler.enemy_id] == true
$game_party.monster_scans[@battler.enemy_id] = true if @hp_bar.visible
end
if !DTP_HP::SPECIFIC_ACTOR.empty?
@hp_bar.visible = false unless DTP_HP::SCAN_STATE != 0
DTP_HP::SPECIFIC_ACTOR.each do |i|
next unless $game_party.battle_members.include?($game_actors)
@hp_bar.visible = true
end
end
@text_display.visible = false if !@hp_bar.visible
@text_display.visible = true if @hp_bar.visible
end
def dispose
@hp_bar.dispose if @hpbar
@text_display.dispose if @text_display
hpbar_dispose
end
end
class Game_Party
alias hp_bar_init initialize
attr_accessor :monster_scans
def initialize
hp_bar_init
@monster_scans = []
end
end
Okay, this one is REALLY simple! It just requires one line added to the Sprite_battler.update method, immediately after it's called the original update.
Let's assume you're using switch 12 for the HP bars.
If switch 12 == ON means the bars should show, change the Sprite_Battler.update method to this:
If switch 12 == ON means the bars should NOT show, change it to this instead:
Let's assume you're using switch 12 for the HP bars.
If switch 12 == ON means the bars should show, change the Sprite_Battler.update method to this:
Code:
def update hpbar_update return unless $game_switches[12] return unless @battler.is_a?(Game_Enemy) if @battler update_hp_bar end end
Code:
def update hpbar_update return if $game_switches[12] return unless @battler.is_a?(Game_Enemy) if @battler update_hp_bar end end
Awesome! It works! Thank you very much, the topic can be closed now 🙂
(Sorry about my mistakes for posting in the forum. Still new and learning)
(Sorry about my mistakes for posting in the forum. Still new and learning)
This thread is being closed, due to being solved. If for some reason you would like this thread re-opened, please report this post and leave a message why. Thank you.
Original thread: https://forums.rpgmakerweb.com/threads/conditional-branch-scripting.18144/