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] Adding random item to inventory from list.

Oki have this fishlist = [142, 143, 144, 145, 146, 147, 148, 149, 150, 152] i want later one in the script to randomly place one of these items into the player inventory, but also be able to display what item it was in a window. The item should be @fish and i made this little class to do so.

class Fish
  attr_reader :base_id
 
  def initialize(base_id)
    @base_id = base_id
    base = $data_items[base_id]
  end
 
  def icon_name
    return $data_items[@base_id].icon_name
  end
 
  def name
    return $data_items[@base_id].name
  end
end

Thanks again
-Sobe
 
When you say display what it was in a window, do you mean a message window? If you want to add an item just use $game_party.gain_item(id, number). So if @fish was an item, just put gain_item(@fish.id, 1). Sorry, if this wasn't what you are asking, I'm not 100% sure what you want.
 
It would be pretty pointless to create a stand-alone class that gets properties that's already there for the data structure. I wrote a little something to do this. Just add the code below to where you want this random item gain to occur:
Code:
FISH = [142, 143, 144, 145, 146, 147, 148, 149, 150, 152]

def gain_random_fish
  index = rand(FISH.size)
  $game_party.gain_item(FISH[index], 1)
end
Just make sure that this method is added to whatever class you are going to use it in.

Hope this helps.
 
It's because you cannot use $FISH. It's FISH that was defined as a constant, not the global variable $FISH.
Remove the $ and you should be fine, unless it's inside a different class.
 

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