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.

This little bit will not add an item to the party's inventory, Please help.

This bit of code has been driving me nuts for over 8hrs now. It's suppose to randomly choose one of those item ids from the list depending on the terrain tag the tile is and then shower that you caught "that items name" and add the item to the inventory, but it gives and error when it goes to show that text and then if you ignore that line its seems to work fine but nothing gets added to the inventory, can someone please help? Thanks you.
-Sobe


Code:
  def hooked
    if $hooked == false
        $hooked = true
        case $waterlevel
        when 6
        fishinwater = [142]
        when 7
        fishinwater = [142, 143, 144, 145, 146, 147, 148, 149, 150, 152]
      end
    end
    $fish = rand(fishinwater)
    if $hooked == true #If a fish was hooked, then catch it and wait a little.
       $hooked = false
       @help_window.set_text("You caught a " + $data_items[$fish] + "!", 1)
       if Input.trigger?(Input::C)
        $game_party.gain_item($fish, 1)
        end
       $game_system.me_play($data_system.battle_end_me)
       $game_variables[1] += 1
       $scene = Scene_Map.new
       end
     end
   end
 
The problem is the  = rand(fishinwater) part. fishinwater is an array, rand expects an integer. The following code should work, unless there s an error elsewhere. If you need the id# for later just replace index with $fish.
Code:
 def hooked
    if !$hooked
        $hooked = true
        case $waterlevel
        when 6
        fishinwater = [142]
        when 7
        fishinwater = [142, 143, 144, 145, 146, 147, 148, 149, 150, 152]
      end
    end
    if fishinwater.size != 0 
        index = rand(fishinwater.size)
    else
        index = 0
    end
    if $hooked #If a fish was hooked, then catch it and wait a little.
       $hooked = false
       @help_window.set_text("You caught a " + $data_items[index] + "!", 1)
       if Input.trigger?(Input::C)
        $game_party.gain_item(index, 1)
        end
       $game_system.me_play($data_system.battle_end_me)
       $game_variables[1] += 1
       $scene = Scene_Map.new
       end
     end
   end
 
This is the hole code now with your fix and .size taken out, but i get a nil into string error on line 60, and if i just ignore that line it still doesnt add an item to my inventory. PS and with the .size i get a nil:nillclass error. Thanks again

Code:
class Scene_Fishing
  
   def main
    @spriteset = Spriteset_Map.new 
    @help_window = Window_Help.new
    @help_window.set_text("Fishing...", 1)
    $hooked = false #You don't have a fish on your hook yet.
    Graphics.transition
    loop do
      Graphics.update
      Input.update
      update
      if $scene != self
        break
      end
    end
    Graphics.freeze
    @spriteset.dispose
    @help_window.dispose
    end
  def update
    @spriteset.update
    @help_window.update
    update_fish
  end
  def update_fish
    # If B button was pressed
    @help_window.set_text("Oh a bite!", 1)
    if Input.trigger?(Input::C)
      # Play cancel SE
      $game_system.se_play($data_system.cancel_se)
      hooked
      return
    end
    if Input.trigger?(Input::B)
      # Play cancel SE
      $game_system.se_play($data_system.cancel_se)
      # Switch to map screen
      $scene = Scene_Map.new
      return
    end
  end
 def hooked
    if !$hooked
        $hooked = true
        case $waterlevel
        when 6
        fishinwater = [142]
        when 7
        fishinwater = [142, 143, 144, 145, 146, 147, 148, 149, 150, 152]
      end
    end
    if fishinwater != 0 
        index = rand(fishinwater)
    else
        index = 0
    end
    if $hooked #If a fish was hooked, then catch it and wait a little.
       $hooked = false
       @help_window.set_text("You caught a " + $data_items[index] + "!", 1)
       if Input.trigger?(Input::C)
        $game_party.gain_item(index, 1)
        end
       $game_system.me_play($data_system.battle_end_me)
       $game_variables[1] += 1
       $scene = Scene_Map.new
       end
     end
   end
 
The error message would seem to indicate that fishinwater = nil. But, that can only happen if $hooked doesn't have a value, or if $waterlevel isn't 6 or 7; as fishinwater wouldn't then get assigned. How do you assign $waterlevel? At any rate, I rewrote your code, you'll notice that I removed $hooked since main sets it to false and at the end of the hooked method it is set back to false. In other words, it is false before and after the method so both conditional get run, it is uneccessary. If you let me know exactly what you want, in english, I might be able to help, anyways here goes:
Code:
class Scene_Fishing
  
   def main
    @spriteset = Spriteset_Map.new 
    @help_window = Window_Help.new
    @help_window.set_text("Fishing...", 1)
    Graphics.transition
    loop do
      Graphics.update
      Input.update
      update
      if $scene != self
        break
      end
    end
    Graphics.freeze
    @spriteset.dispose
    @help_window.dispose
  end
  
  def update
    @spriteset.update
    @help_window.update
    update_fish
  end
  
  def update_fish
    # If B button was pressed
    @help_window.set_text("Oh a bite!", 1)
    if Input.trigger?(Input::C)
      # Play cancel SE
      $game_system.se_play($data_system.cancel_se)
      hooked
      return
    end
    if Input.trigger?(Input::B)
      # Play cancel SE
      $game_system.se_play($data_system.cancel_se)
      # Switch to map screen
      $scene = Scene_Map.new
      return
    end
  end
  
  def hooked
    if $waterlevel >= 6
      index = 142 + rand(9 * ($waterlevel - 6))
    end
    @help_window.set_text("You caught a " + $data_items[index] + "!", 1)
    if Input.trigger?(Input::C)
      $game_party.gain_item(index, 1)
    end
      $game_system.me_play($data_system.battle_end_me)
      $game_variables[1] += 1
      $scene = Scene_Map.new
    end
  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