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.

About Instance Variables from Windows ...

How do I call an instance variable from a window? like @commands?

To elaborate, I added a style argument to Window_Command to add the ability for a command window to display options in alternating order and the display of it works fine.
NORMAL DISPLAY
-----------------
1
2
3
4

ALTERNATING DISPLAY
----------------------
1 2
3 4
Now I have to recode the cursor, which brings up the problem with columns. In the original there is only ever one column of choices, so of course the original makers had it = 1 w/o thought to add functionability for aditional columns. However, in my model there are two. I created a @style instance variable for the argument style in Window_Command, so how do I bring that variable into Window_Selectable to vary the # of columns?
 

khmp

Sponsor

Code:
class Window_Command < Window_Selectable
  attr_accessor :commands
end

However you are trying to gain access to this data from an ancestor of the window. So in the ancestor create a method that takes in the array of commands than from the child invoke the method.

Code:
class Window_Selectable < Window_Base
  def do_stuff(commands)
    # Do work involving the commands
  end
end

That will let you get to that instance variable while the Command_Window exists. But if you are trying to access it from a child's data from the ancestor perhaps the design should change to reflect a new window rather than a change of the existing.
 
just wait a moment... I think the solution is quite simple...

edit:
OK, this may be your answer...

Instructions?

Well, you may need to create a window like this...
# here you would need all the commands you need, in this case you would need 4 of them.
@command_window = Window_Command2.new(160, [s1, s2, s3, s4])
# the rest is identical to the common update process and disposal of commands windows.

Code:
#==============================================================================
# ** Window_Command2
#    modified by shadowball
#------------------------------------------------------------------------------
#  This window deals with general command choices.
#==============================================================================
class Window_Command2 < Window_Selectable
  #--------------------------------------------------------------------------
  # * Object Initialization
  #     width    : window width
  #     commands : command text string array
  #--------------------------------------------------------------------------
  def initialize(width, commands)
    # Compute window height from command quantity
    @column_max = 2
    super(0, 0, (@column_max * 224), (commands.size * 32 / 2 ) + 32)
    # How many columns do you need?
    @column_max = 2
    #@row_max = 2
    @item_max = commands.size
    @commands = commands
    self.contents = Bitmap.new(@column_max * 208, @item_max * 16)
    refresh
    self.index = 0
  end
  #--------------------------------------------------------------------------
  # * Refresh
  #--------------------------------------------------------------------------
  def refresh
    self.contents.clear
    for i in 0...@item_max
      draw_item(i, normal_color)
    end
  end
  #--------------------------------------------------------------------------
  # * Draw Item
  #     index : item number
  #     color : text color
  #--------------------------------------------------------------------------
  def draw_item(index, color)
    # You may need to set the x and y values like this...
    x = index % 2 * 224 
    y = index / 2 * 32 
    self.contents.font.color = color
    rect = Rect.new(x, y, (self.contents.width / 2), 32)#- 8, 32)
    self.contents.fill_rect(rect, Color.new(0, 0, 0, 0))
    self.contents.draw_text(rect, @commands[index])
  end
  #--------------------------------------------------------------------------
  # * Disable Item
  #     index : item number
  #--------------------------------------------------------------------------
  def disable_item(index)
    draw_item(index, disabled_color)
  end
end
 

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