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.

Choose Items [Resolved]

Hello,
I'd like to request a little script that could be useful for lots of things like cooking, crafting mini-games. Basically, I want to be able to call script something like this:
Code:
items = 1, 5, 6, 7, 20
$scene = Scene_ItemList.new

I want a scrollable list to appear showing the icon (optional, but preferable), name, and number of each item in the inventory that's specified. This would be something like a Show Choices command, but you can scroll down the list to pick an item. In the example above, it would only show items with ID 1, 5, 6, 7, or 20. Again, like a Show Choices command, I want it so you can event something different depending on which item you pick.

Also, could it be non-SDK please? And I'm using ccoa's UMS, so try and make it compatible with that. Thanks a lot!

Hey poccil, do you mind making a slight modification to the script? It works great, but I'm working on another game that I want to use it for, and I'd like some changes to be made.

Could you add a function so it displays a message above the item window? I'd like the message to be customizable (different every time), so I could have text saying "Use Item" or "Select Ingredient", etc.

Also, I'm implementing a 10-item inventory limit. This will be already scripted, so you don't need to worry about it. However, I'd like to remove the number of each item. Here's an example:

I want to change something like this:
Code:
Potion x2 (icon)
Key x3 (icon)
to
Code:
Potion (icon)
Potion (icon)
Key (icon)
Key (icon)
Key (icon)

So it lists all the items in the inventory alone rather than combining them. For selecting multiple items, you can just make them have one variable value. So if you select any one of the 3 keys, the variable affected in the script will be the same.

I'd really appreciate it if anyone could make these changes. Thanks!
 

poccil

Sponsor

Use the script code below.  Put it in a new script section:

Edit: Changes made at end of June.

Code:
class Scene_Map
  def updatemini
    loop do
      $game_map.update
      $game_player.update
      $game_system.update
      $game_screen.update
      unless $game_temp.player_transferring
        break
      end
      transfer_player
      if $game_temp.transition_processing
        break
      end
    end
    @spriteset.update
    @message_window.update
  end
end

def pbUpdateSceneMap
 if $scene && $scene.is_a?(Scene_Map)
   $scene.updatemini
 end
end

def pbDisplayText(text)
   waiting = true
   $game_temp.message_proc = Proc.new {
     waiting = false
   }
   $game_temp.message_text = text
   while waiting
     Graphics.update
     Input.update
     pbUpdateSceneMap
   end
   Input.update
end

class ItemListTitle < Window_Base
  def initialize(title)
    super(0, 0, 320, 64)
    self.contents = Bitmap.new(width - 32, height - 32)
    set_text(title,1)
    self.opacity=128
  end
  def set_text(text, align = 0)
    # If at least one part of text and alignment differ from last time
    if text != @text or align != @align
      # Redraw text
      self.contents.clear
      self.contents.font.color = normal_color
      self.contents.draw_text(4, 0, self.width - 40, 32, text, align)
      @text = text
      @align = align
      @actor = nil
    end
    self.visible = true
  end
end


class ItemList < Window_Selectable
  attr_accessor :items
  def initialize(items)
    super(0,0,320,64)
    @items=items
    self.opacity=128
    @item_max=@items.length
    self.contents = Bitmap.new(width - 32, @item_max<=0 ? 1 : (@item_max * 32))
    @showCount=true
    @centered=false
    refresh
    self.index = 0
  end
  def showCount=(value)
    @showCount=value
    refresh
  end
  def centered=(value)
    @centered=value
    refresh
  end
  attr_reader :showCount
  def update
    index=self.index
    super
    refresh if index!=self.index
  end
  def refresh
    self.contents.clear
    for i in 0...@items.length
      item=$data_items[@items[i]]
      next if !item
      bitmap = RPG::Cache.icon(item.icon_name)
      width=24+4+self.contents.text_size(item.name).width
      itemcount=$game_party.item_number(@items[i])
      self.contents.font.color = itemcount==0 ? disabled_color : normal_color
      if @centered
        placement=(@showCount) ? self.contents.width : self.contents.width-32
        self.contents.blt((placement/2)-(width/2), i*32 + 4, 
           bitmap, Rect.new(0, 0, 24, 24),
          itemcount==0 ? 128 : 255)
        self.contents.draw_text((placement/2)-(width/2)+28, i*32, placement, 32, item.name)                
      else
        self.contents.blt(0, i*32 + 4, 
           bitmap, Rect.new(0, 0, 24, 24),
          itemcount==0 ? 128 : 255)
        self.contents.draw_text(28, i*32, self.contents.width-32, 32, item.name)        
      end
      width=self.contents.text_size(itemcount.to_s).width
      if @showCount
        self.contents.draw_text(self.contents.width-width-2,
         i*32, width, 32, itemcount.to_s)
      end
    end
  end
end

def chooseItem(title,items,variableNumber)
 realitems=[]
 showEach=true
 for item in items
   if $game_party.item_number(item)>0
     if showEach
       $game_party.item_number(item).times {
         realitems.push(item)
       }
     else
       realitems.push(item)
     end
   end
 end
 if realitems.length==0
   $game_variables[variableNumber]=-1
   return
 end
 titlewindow=nil
 itemlist=ItemList.new(realitems)
 itemlist.x=0
 itemlist.y=0
 itemlist.width=320
 itemlist.height=[realitems.length*32+33,480].min
 itemlist.z=500
 itemlist.showCount=!showEach
 itemlist.centered=true
 index=-1
 if title && title!=""
   titlewindow=ItemListTitle.new(title)
   itemlist.y+=64
   itemlist.height=[realitems.length*32+33,416].min
 end
 loop do
   Graphics.update
   Input.update
   itemlist.update
   titlewindow.update if titlewindow
   pbUpdateSceneMap
   if Input.trigger?(Input::C)
      item=realitems[itemlist.index]
      if $game_party.item_number(item)==0
        $game_system.se_play($data_system.buzzer_se)
      else
        $game_system.se_play($data_system.decision_se)
        index=item
        break
      end
   end
   if Input.trigger?(Input::B)
      $game_system.se_play($data_system.cancel_se)
      index=0
      break
   end
 end
 titlewindow.dispose if titlewindow
 itemlist.dispose
 $game_variables[variableNumber]=index
 Input.update
end

The script code above defines a function called chooseItem.  It can be used in a Script event command like this:

Code:
chooseItem("My Title",[1,2,3,4,5], 10)
The first parameter is the title (can be an empty string).  The second parameter is an array of items to show on the list (here, [1,2,3,4,5]).  The third parameter identifies a variable to store the result (here, 10).  The result will be one of the items given in the array, or 0 if the item choice was canceled.

Set "showEach" in the script above to false to allow the item count to show on the item list.  By default, this will not be the case, as you requested.
 
Wow, quick reply. Thanks poccil, I'll try it tomorrow and see how it goes.

EDIT: I got it to work by manually editing the lines so part of it would fit on the first line  :-\ Here's my call script:

Code:
chooseItem([1,2,3,4,5,6,7,8,9,10,11,
12,28,34,35,36,37,38,39,40,41,47,49,
50], 19)

However, there are still some slight problems.
#1, the window is a bit too big to fit on the screen at once. Where should I edit it to change the height of the window?
#2, the window shows all items specified, regardless of how many of that item you have. (ex: even if you have 0 of item 1, it still shows item 1 with quantity 0.) Could you perhaps edit it so it only shows items that you have at least 1 of?

Thanks :)
 

poccil

Sponsor

The first point is due to a bug in the original script.  I've fixed it now.

I have incorporated your second point in the script above; the function changeItem now sets the variable to -1 if the player can't choose any items.
 
Poccil, I'm going to use this in a new game, but I'd like some modifications. Take a look at my first post, I'd really appreciate it if you could edit it like I asked.

Thanks :smile:

old bump :S
 

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