Hello, some weeks ago I have written a small HUD Script.
I have refactored the code a bit today and have some questions.
Here is the Code:
I have this method:
which iterate through all battle_members and draws them in the hud.
There is a 2D-array "@current_hp_mp_name" which saves the hp, mp and name of the current actor in the loop.
This array is later used on the update method to check if a refresh is needed:
This works ok, but im wondering if there are other and better solutions available to handle the updating?
I would also appreciate if I get some general review on the code.
greets
I have refactored the code a bit today and have some questions.
Here is the Code:
#==============================================================================## Simple HUD# Author: eugene222# Version: 1.0a - 09.03.2014#==============================================================================## Changelog:# Version 1.0 - 08.03.2014# Version 1.0a - 09.03.2014 - Fixed some bugs# Version 1.0b - 31.03.2014 - Fixed a bug and refactored the code#==============================================================================#module E222 module HUD_CONFIG #--------------------------------------------------------------------------# # Show HUD at bottom: #--------------------------------------------------------------------------# HUD_BOT = true #--------------------------------------------------------------------------# # Opacity of the HUD-Window (0-255) #--------------------------------------------------------------------------# WINDOW_OPACITY = 255 #--------------------------------------------------------------------------# # HUD Show Switch #--------------------------------------------------------------------------# HUD_SWITCH_ID = 1 #--------------------------------------------------------------------------# # Show actor sprite?, Position🙂right or :left) #--------------------------------------------------------------------------# DRAW_ACTOR = [true, :right] #--------------------------------------------------------------------------# # Some Options: #--------------------------------------------------------------------------# DRAW_NAME = true DRAW_FACE = true DRAW_HP = true DRAW_MP = true #--------------------------------------------------------------------------# # Center the Hud if less than 4 people in party #--------------------------------------------------------------------------# HUD_CENTER = false #--------------------------------------------------------------------------# # This Hud works only properly if Max_Battle_Member is less than 5 # i will fix this later #--------------------------------------------------------------------------# endend#------------------------------------------------------------------------------#class Window_SimpleHUD < Window_Base include E222::HUD_CONFIG def initialize() super(window_x, window_y, window_width ,window_height) self.opacity = WINDOW_OPACITY self.visible = $game_switches[HUD_SWITCH_ID] refresh end #--------------------------------------------------------------------------# def window_y HUD_BOT ? Graphics.height - window_height : 0 end #--------------------------------------------------------------------------# def window_x HUD_CENTER ? (Graphics.width - window_width) / 2 : 0 end #--------------------------------------------------------------------------# def window_width ((Graphics.width-24) / 4) * $game_party.battle_members.size + 24 end #--------------------------------------------------------------------------# def window_height fitting_height(4) end #--------------------------------------------------------------------------# def actor_pos DRAW_ACTOR[1] == :right ? 112 : 16 end #--------------------------------------------------------------------------# # change the y-value of the HP-Gauge if DRAW_MP is turned off #--------------------------------------------------------------------------# def hp_y DRAW_MP ? fitting_height(1.4) : fitting_height(2.1) end #--------------------------------------------------------------------------# def draw_actors $game_party.battle_members.each_with_index do |actor, index| start_x = (Graphics.width / 4 - 6) * index + 3 draw_actor_graphics(actor, index, start_x) draw_actor_name(actor, start_x, 0) if DRAW_NAME draw_actor_gauges(actor, index, start_x) # Save the current hp, mp and name of each actor into an array: @current_hp_mp_name[index] = actor.hp, actor.mp, actor.name end end #--------------------------------------------------------------------------# def draw_actor_graphics(actor, index, x) draw_actor_face(actor, x, 0) if DRAW_FACE draw_actor_graphic(actor, x + actor_pos, hp_y) if DRAW_ACTOR[0] end #--------------------------------------------------------------------------# def draw_actor_gauges(actor, index, x) draw_actor_hp(actor, x, hp_y) if DRAW_HP draw_actor_mp(actor, x, fitting_height(2.1)) if DRAW_MP end #--------------------------------------------------------------------------# def refresh clean_current contents.clear draw_actors end #--------------------------------------------------------------------------# def clean_current # clean the array @current_hp_mp_name = nil @current_hp_mp_name = [] end #--------------------------------------------------------------------------# def resize_window self.x = window_x self.width = window_width self.contents = Bitmap.new(width - 24, height - 24) end #--------------------------------------------------------------------------# def update super set_visible return unless self.visible # if an actor was removed or added the hud needs an resize, too: if @current_hp_mp_name.size != $game_party.battle_members.size; resize_window refresh return end # Iterate through each actor and refresh if something changed: $game_party.battle_members.each_with_index do |actor, index| if need_update?(actor, index) refresh break end end end #--------------------------------------------------------------------------# def set_visible if $game_switches[HUD_SWITCH_ID] && !$game_message.visible self.visible = true else self.visible = false end end #--------------------------------------------------------------------------# # Check if name, hp or mp of actor has changed #--------------------------------------------------------------------------# def need_update?(actor, index) actor.hp != @current_hp_mp_name[index][0] || actor.mp != @current_hp_mp_name[index][1] || actor.name != @current_hp_mp_name[index][2] end #--------------------------------------------------------------------------#end#------------------------------------------------------------------------------# class Scene_Map alias :scene_map_create_all_windows_e222 :create_all_windows def create_all_windows scene_map_create_all_windows_e222 @hud_window = Window_SimpleHUD.new endend#------------------------------------------------------------------------------## SCRIPT END#------------------------------------------------------------------------------#
def draw_actors $game_party.battle_members.each_with_index do |actor, index| start_x = (Graphics.width / 4 - 6) * index + 3 draw_actor_graphics(actor, index, start_x) draw_actor_name(actor, start_x, 0) if DRAW_NAME draw_actor_gauges(actor, index, start_x) # Save the current hp, mp and name of each actor into an array: @current_hp_mp_name[index] = actor.hp, actor.mp, actor.name end end
There is a 2D-array "@current_hp_mp_name" which saves the hp, mp and name of the current actor in the loop.
This array is later used on the update method to check if a refresh is needed:
def update super set_visible return unless self.visible # if an actor was removed or added the hud needs a resize, too: if @current_hp_mp_name.size != $game_party.battle_members.size; resize_window refresh return end # Iterate through each actor and refresh if something changed: $game_party.battle_members.each_with_index do |actor, index| if need_update?(actor, index) refresh break end end end
Code:
def need_update?(actor, index) actor.hp != @current_hp_mp_name[index][0] || actor.mp != @current_hp_mp_name[index][1] || actor.name != @current_hp_mp_name[index][2] end
I would also appreciate if I get some general review on the code.
greets