Envision, Create, Share

Welcome to HBGames, a leading amateur game development forum and Discord server. All are welcome, and amongst our ranks you will find experts in their field from all aspects of video game design and development.

[Resolved] [VX] Changing Variables

Krazy3

Member

Being new to scripting, I'd like to ask a noobish question. First of all, I've searched, checked, and hunted down everything in order to find the answer to my question.

In the script below, I have created a new window (appears in the menu) that displays the variable $copperore. I was wondering how I would change copper ore from within an event, or achieve something similar. The idea is for the character to mine ore from a rock, and instead of it being an actual item, it would appear in the menu. The problem I have, is that I cannot find a way to change this variable outside of the editor. Help is VERY appreciated :)

Code:
#==============================================================================
# ** Window_CopperOre
#------------------------------------------------------------------------------
#  This window displays the amount of copper ore.
#==============================================================================

class Window_CopperOre < Window_Base
  #--------------------------------------------------------------------------
  # * Object Initialization
  #     x : window X coordinate
  #     y : window Y coordinate
  #--------------------------------------------------------------------------
  def initialize(x, y)
    super(x, y, 160, WLH + 32)
    refresh
  end
  #--------------------------------------------------------------------------
  # * Refresh
  #--------------------------------------------------------------------------
  def refresh
    self.contents.clear
    self.contents.draw_text(4, 0, 120, 25, $copperore)
    self.contents.draw_text(40, 0, 120, 25, "Copper Ore")
  end
end
 

khmp

Sponsor

What you are probably doing is this:
Code:
class Scene_Whatever
  alias_method :old_main, :main
  def main
    @w_copperore = Window_CopperOre.new
    old_main
    @w_copperore.dispose
  end
end

What you should be doing:
Code:
class Scene_Whatever
  alias_method :old_main, :main
  alias_method :old_update, :update
  def main
    @w_copperore = Window_CopperOre.new
    old_main
    @w_copperore.dispose
  end

  def update
    @w_copperore.update
    old_update
  end
end

And inside your Window_CopperOre have this:
Code:
#==============================================================================
# ** Window_CopperOre
#------------------------------------------------------------------------------
#  This window displays the amount of copper ore.
#==============================================================================

class Window_CopperOre < Window_Base
  #--------------------------------------------------------------------------
  # * Object Initialization
  #     x : window X coordinate
  #     y : window Y coordinate
  #--------------------------------------------------------------------------
  def initialize(x, y)
    super(x, y, 160, WLH + 32)
    @copper = nil
    refresh
  end
  #--------------------------------------------------------------------------
  # * Refresh
  #--------------------------------------------------------------------------
  def refresh
    @copper = $copperore
    self.contents.clear
    self.contents.draw_text(4, 0, 120, 25, $copperore)
    self.contents.draw_text(40, 0, 120, 25, "Copper Ore")
  end
  #--------------------------------------------------------------------------
  # * Frame Update
  #--------------------------------------------------------------------------
  def update
    refresh unless $copperore.nil? || @copper == $copperore
  end
end

Here is the problem you are running into. The window was only being drawn once and that is all. So changing the variable does not automatically tell whatever window the variable was used to redraw. In the above window update code it checks to see if the variable is not nil or wasn't the last item to be drawn. This update is called once a frame but only redraws when necessary.

Good luck with it Krazy3! :thumb:
   
 

Krazy3

Member

I got it to work. The problem I had was the event. I wasn't scripting it right in there. This is also based in the menu, so it works just like the gold box (copied the code and edited it). Thanks for the help though. I'll save this and refer to it when I have this problem again :)
 

Thank you for viewing

HBGames is a leading amateur video game development forum and Discord server open to all ability levels. Feel free to have a nosey around!

Discord

Join our growing and active Discord server to discuss all aspects of game making in a relaxed environment. Join Us

Content

  • Our Games
  • Games in Development
  • Emoji by Twemoji.
    Top