My custom window not refreshing?

So in case you didn't know, I can't script to save my life. I tried making a hud like window that shows text in the map scene from some variables. It works together with some events to show the player's "clan", as well as the clan's reputation. The script is below:

Spoiler
class Window_Clan < Window_Base  def initialize

    super(0, 368, Graphics.width, fitting_height(1))

    self.windowskin = Cache.system("Black Window")

    create_contents

    refresh

  end

  def refresh

    contents.clear

    #Draw "Clan" text

    draw_text_ex(0, 2, "Clan:")

    #Draw clan name

    draw_text_ex(30, 2, $game_variables[1])

    #Draw clan reputation

    draw_text_ex(96, 2, $game_variables[2])

    draw_icon($game_variables[4], 72, 2, enabled = true)

  end

end

class Scene_Map < Scene_Base

  def create_all_windows

    create_message_window

    create_scroll_text_window

    create_location_window

    create_clan_window

  end

  def create_clan_window

    @clan_window = Window_Clan.new

    @clan_window.z = 1

  end

end
Everything about the script seems to work fine (which is a first for me tbh), except the contents of the window will not update unless I enter another scene and then return to the map scene! Since it's supposed to be a hud, the contents should update along with the variables. But since my scripting knowledge pretty much ends here, I can't figure it out. Also, if anyone has any more advice as to how to make the code a little bit cleaner, I would be happy to hear that as well. 
I've moved this thread to RGSSx Script Support. Please be sure to post your threads in the correct forum next time. Thank you.


You also have to call the update or refresh method. Look in the Scene_Map.update method and see that it calls update on all of its windows. In fact, if you are attaching your new window to the map, find another window that's already attached to the map (like the message window) and look for any time it's used in Scene_Map. You will potentially have to cater for similar handling of your new window for each of those scenarios.
thank you for moving my topic into the correct forum  🙂 I followed your advice to the best of my abilities, and was able to have the window update properly. However, it currently produces a considerable amount of lag. Though I can understand why it makes lag, I don't really know how to solve it. Here's the code below:

Spoiler
class Window_Clan < Window_Base
def initialize
super(0, 368, Graphics.width, fitting_height(1))
self.windowskin = Cache.system("Black Window")
create_contents
refresh
end
def refresh
contents.clear
#Draw "Clan" text
draw_text_ex(0, 2, "Clan:")
#Draw clan name
draw_text_ex(30, 2, $game_variables[1])
#Draw clan reputation
draw_text_ex(96, 2, $game_variables[2])
draw_icon($game_variables[4], 72, 2, enabled = true)
end
def update
super
end
end
class Scene_Map < Scene_Base
def update
super
$game_map.update(true)
$game_player.update
$game_timer.update
update_clan_window
@spriteset.update
update_scene if scene_change_ok?
end
def create_all_windows
create_message_window
create_scroll_text_window
create_location_window
create_clan_window
end
def create_clan_window
@clan_window = Window_Clan.new
@clan_window.z = 1
end
def update_clan_window
@clan_window.refresh
end
end
You are redrawing the whole window every time. Does that need to be done?


If you can, turn a switch on whenever the window needs to be refreshed. Then change

@clan_window.refreshto
Code:
@clan_window.refresh if $game_switches[12]
so it's only redrawing the window when the contents change.
When you want to post a script, can you put code tags around it? It makes it retain the formatting and makes it much easier to read.
This should work out better :

class Window_Clan < Window_Base def initialize # Store clan name and clan rep in instance variables @clan_name = $game_variables[1] @clan_rep = $game_variables[2] super(0, 368, Graphics.width, fitting_height(1)) self.windowskin = Cache.system("Black Window") create_contents refresh end def refresh contents.clear #Draw "Clan" text draw_text_ex(0, 2, "Clan:") #Draw clan name draw_text_ex(30, 2, $game_variables[1]) #Draw clan reputation draw_text_ex(96, 2, $game_variables[2]) draw_icon($game_variables[4], 72, 2, enabled = true) # Update clan name and clan rep @clan_name = $game_variables[1] @clan_rep = $game_variables[2] end def update super # Refresh only if old clan name does not equal clan name in variable 1 or # if clan rep does not equal old clan rep # Constantly re-drawing text and graphics eat up system resources. refresh if (@clan_name != $game_variables[1] || @clan_rep != $game_variables[2]) endend # Window_Clanclass Scene_Map < Scene_Base # Over-write of 'update' not needed # See 'Scene_Base' line 93 'update_all_windows' # Any window will be updated automatically within the scene#~ def update#~ super#~ $game_map.update(true)#~ $game_player.update#~ $game_timer.update#~ update_clan_window#~ @spriteset.update#~ update_scene if scene_change_ok?#~ end # Alias existing method names to increase compatibility with other scripts # Check to see if alias already set unless method_defined?🙂clan_vBrehVy9_create_all_windows) # Random letter/number combination decreases the chance of another alias # conflicting with the name. # new alias name original name alias_method🙂clan_vBrehVy9_create_all_windows, :create_all_windows) end # Ensure any arguments or code blocks are passed through the aliased command # all arguments, code block def create_all_windows(*args, &block) # Aliased command calls original command clan_vBrehVy9_create_all_windows(*args, &block) create_clan_window end def create_clan_window @clan_window = Window_Clan.new @clan_window.z = 1 end # Not needed#~ def update_clan_window#~ @clan_window.refresh#~ endend # Scene_MapEDIT: More cautious method alias. Few code alterations.EDIT II: Oop, forgot to update '@clan_name' and '@clan_rep' during 'refresh' method.
THANK YOU! It works perfectly in my project. I'll be sure to credit you guys for your help 🙂
After glancing over the code again, I forgot to set the instance variables in the 'Window_Clan' class to the newer values. Otherwise it will just keep refreshing.

Original thread: https://forums.rpgmakerweb.com/threads/my-custom-window-not-refreshing.40946/