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.

Item Sort Help

Star

Sponsor

Hi again!

So this time I want to sort items based on item_id but I haven't been able to figure out the right method call or where to insert it properly because I either end up with a lot of the same item or a blank sheet of nothing in my window_item

I'm just using the normal window_item class

Code:
class Window_Recovery_Magical < Window_Selectable

  def initialize

    super(0, 68, 640, 346)

    @column_max = 2

    refresh

    self.index = 0

    if $game_temp.in_battle

      self.y = 64

      self.height = 256

      self.back_opacity = 160

    end

  end

  def item

    return @data[self.index]

  end

  def refresh

    if self.contents != nil

      self.contents.dispose

      self.contents = nil

    end

    @data = []

    for i in 1...$data_items.size

      if $game_party.item_number(i) > 0

        @data.push($data_item[i])

      end

    end

    @item_max = @data.size

    if @item_max > 0

      self.contents = Bitmap.new(width - 32, row_max * 32)

      for i in 0...@item_max

        draw_item(i)

      end

    end

  end

 

  def draw_item(index)

    item = @data[index]

    case item

    when RPG::Item

      number = $game_party.item_number(i)

    end

      self.contents.font.color = disabled_color

    x = 4 + index % 2 * (288 + 32)

    y = index / 2 * 32

    rect = Rect.new(x, y, self.width / @column_max - 32, 32)

    self.contents.fill_rect(rect, Color.new(0, 0, 0, 0))

    bitmap = RPG::Cache.icon(item.icon_name)

    face = RPG::Cache.picture("MenuWordBar") rescue nada

    opacity = self.contents.font.color == normal_color ? 255 : 128

    self.contents.blt(x, y + 4, face, Rect.new(0, 0, 287, 32), opacity)

    self.contents.blt(x, y + 4, bitmap, Rect.new(0, 0, 24, 24), opacity)

    self.contents.draw_text(x + 28, y, 212, 32, item.name, 0)

    self.contents.draw_text(x + 240, y, 16, 32, ":", 1)

    self.contents.draw_text(x + 256, y, 24, 32, number.to_s, 2)

  end

  def update_help

    @help_window.set_text(self.item == nil ? "" : self.item.description)

  end

end

Where and how can I put which item id's I want to appear in my list. I've tried figuring this out for four hours straight with no justice so I decided to beg for halp again.

My guess is that the method I need should go in here

Code:
    for i in 1...$data_items.size

      if $game_party.item_number(i) > 0

        @data.push($data_item[i])

      end

    end
But heck if I know what to use :blank: I'm so lost

Thanks.
 
You have the correct method, but I'm guessing you're doing the wrong thing. As it is, it searches through $data items (in ID order), and then checks to see if the party has at least one of that item. If they do, it adds that item to an array, which it uses to make the window.

So, what you'll probably want to do is have an array somewhere, that holds the ID of the items you don't want to appear. For example, you could add it to the initialize method, like so:

[rgss]#--------------------------------------------------------------------------
  # * Object Initialization
  #--------------------------------------------------------------------------
  def initialize
    super(0, 64, 640, 416)
    @column_max = 2
    @ignored_items = [1, 4, 7]
    refresh
    self.index = 0
    # If in battle, move window to center of screen
    # and make it semi-transparent
    if $game_temp.in_battle
      self.y = 64
      self.height = 256
      self.back_opacity = 160
    end
  end
[/rgss]

Then, you would add a bit of code to the block you posts, so that it looks like the following:

[rgss]    # Add item
    for i in 1...$data_items.size
      next if @ignored_items.include?(i)
      if $game_party.item_number(i) > 0
        @data.push($data_items)
      end
    end
[/rgss]

That would make it so that Potions (1), Perfumes (4), and Elixers (7) do not appear in the item window. Just make sure that the array is created before the initialize method calls the refresh method, or you will get a nilClass error.
 

Star

Sponsor

It sounds about right, but when I tested it I instead got an error undefined method for [] at @data.push($data_items)

I did the initialization and everything, so what else should I do?

Thanks
 
StarGGundam2":3fw83g1o said:
It sounds about right, but when I tested it I instead got an error undefined method for [] at @data.push($data_items)

I did the initialization and everything, so what else should I do?

Thanks


If you got that error, then you've redefined $data_items somewhere, since it is no longer an array. Although, looking at your first post, it looks like you need to add an s to the end of $data_item, so that it is $data_items. You should always be careful with variable names, because a little error like that can have severe consequences.
 

Star

Sponsor

Glitchfinder":cj8kw55c said:
If you got that error, then you've redefined $data_items somewhere, since it is no longer an array. Although, looking at your first post, it looks like you need to add an s to the end of $data_item, so that it is $data_items. You should always be careful with variable names, because a little error like that can have severe consequences.
I knew I did something like that. Dangit, Why didn't I remember that I changed it when I was poking around. Sorry for the trouble, you've helped me out a great deal.

Yes it all makes sense now. An undefined method is a method that's never been made before, why didn't I think about that when I saw the error. I have much to learn.

Thanks for your help
 
StarGGundam2":3dc9g0jr said:
I knew I did something like that. Dangit, Why didn't I remember that I changed it when I was poking around. Sorry for the trouble, you've helped me out a great deal.

Yes it all makes sense now. An undefined method is a method that's never been made before, why didn't I think about that when I saw the error. I have much to learn.

Thanks for your help
No problem. That's what this board is here for, after all. Keep up with your scripting, and one day you'll probably be on my end of threads like this.
 

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