Menu with variable # of elements: struggling with col_max and item_max

Hi everybody!
 
So my column-based custom menu worked fine, but then I thought "Well, it's perfect for when I have 5 party members (elements), but when I have less, there are some ugly empty spaces. Worse, you can actually select them. Maybe I could make the number of columns dependent on the number of party members I have?"
 
So I thought I'd just have to have my party members ids in an array, and then supply array.size to col_max and item_max and voila! But no. The game crashes, saying "no defined 'size' method for 'nil' class". So somehow my variable isn't defined properly. So I added a few print calls to see how this went, see below:
 

Spoiler
[\spoiler]

And it returns something like this:
item_max

col_max

col_max

item_max [1,2,3,4,5]

col_max [1,2,3,4,5]

col_max [1,2,3,4,5]

item_max [1,2,3,4,5]

item_max [1,2,3,4,5]

item_max [1,2,3,4,5]

item_max [1,2,3,4,5]

etc.

(I may not have written the exact number of times it was printed correctly)

So apparently, col_max and item_max are called a few times before the window is even initialized? Or it kinda happens at the same time? I don't know. Anybody  has any ideas about this? Thanks a lot!
Code:
class Window_ShowParty < Window_Selectable  def initialize(shown_party, height_adjust)    super(40, 40 + fitting_height(height_adjust), window_width, fitting_height(visible_line_number))    @shown_party = shown_party    refresh  end    # You'll notice I added a few print calls for debugging purposes  def col_max    print "col_max "    print @shown_party    print "\n"    return 5  end    def item_max    print "item_max"    print @shown_party    print "\n"    return 5  end 
Yes, it should call those methods a few times for sure, to know where to put the things in the window, how many columns/items there is, etc, before drawing anything.


For that reason, any kind of instance variables should be initialized before the 'super' call, if those variable are used in methods which need to be called before or during the initialization process.


Try to put the '@shown_party = shown_party' line above the super call, see if it works or not.
For some reason, I felt that super should always be the first call. I guess it makes sense. I feel so stupid now 😛 But thanks a lot!

Original thread: https://forums.rpgmakerweb.com/threads/menu-with-variable-of-elements-struggling-with-col_max-and-item_max.41947/