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.

Scripts need minor adjustments

I have these two scripts that Brewmeister brewed for me that have slight issues. First, let me explain what the scripts are supposed to do. One script is a window class that goes with a Scene. The Scene opens a window that contains items that you specify when calling the script through an event command. The player can choose which items he wants by selecting them. The only problem is that the items just stay there after you select them, instead of leaving the scene. They still enter your inventory, but since they don't go away, the player can keep clicking on them infinitely.

I need for the script to get rid of the items that you select and when you exit the scene, the items are still gone once the scene is re-called. Like, if I want the scene to be called with the initial items of A and B, the player will take item A then leave. He then opens it again and only item B is there.

Here's the window script:
Code:
#==============================================================================

# ** Window_Chest

#------------------------------------------------------------------------------

#  This window displays items in a chest

#==============================================================================

 

class Window_Chest < Window_Selectable

  #--------------------------------------------------------------------------

  # * Object Initialization

  #--------------------------------------------------------------------------

  def initialize(items, weapons, armor)

    super(270, 54, 240, 380)

    @column_max = 1

    @items = items

    @weapons = weapons

    @armor = armor

    refresh

    self.index = 0

    self.back_opacity = 160

    # 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

  #--------------------------------------------------------------------------

  # * Get Item

  #--------------------------------------------------------------------------

  def item

    return @data[self.index]

  end

  #--------------------------------------------------------------------------

  # * Refresh

  #--------------------------------------------------------------------------

  def refresh

    if self.contents != nil

      self.contents.dispose

      self.contents = nil

    end

    @data = []

    # Add item

    for i in [email=0...@items.size]0...@items.size[/email]

        @data.push($data_items[@items[i]])

    end

    # Add weapons and Armor

    for i in [email=0...@weapons.size]0...@weapons.size[/email]

        @data.push($data_weapons[@weapons[i]])

    end

    for i in [email=0...@armor.size]0...@armor.size[/email]

        @data.push($data_armors[@armor[i]])

    end

    # If item count is not 0, make a bit map and draw all items

    @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

  #--------------------------------------------------------------------------

  # * Draw Item

  #     index : item number

  #--------------------------------------------------------------------------

  def draw_item(index)

    item = @data[index]

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

    y = index / 1 * 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)

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

    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)

  end

  #--------------------------------------------------------------------------

  # * Help Text Update

  #--------------------------------------------------------------------------

  def update_help

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

  end

end

 

and here's the scene script:

Code:
#==============================================================================

# ** Scene_Chest

#------------------------------------------------------------------------------

#  This class performs chest screen processing.

#  Call with:

#  $scene = Scene_Chest.new([items],[weapons],[armor])

#  EX:

#  $scene = Scene_Chest.new([1,2,3],[1,2,3],[1,2,3])

#  Use an empty array [] if no object of that type

#  EX:

#  $scene = Scene_Chest.new([1,2,3],[],[1,2,3])

#==============================================================================

 

class Scene_Chest

  #--------------------------------------------------------------------------

  # * Initialize

  #--------------------------------------------------------------------------

  def initialize(items, weapons, armor)

    @items = items

    @weapons = weapons

    @armor = armor

  end

  #--------------------------------------------------------------------------

  # * Main Processing

  #--------------------------------------------------------------------------

  def main

    # Make menu background

    @spriteset = Spriteset_Map.new

    @item_window = Window_Chest.new(@items, @weapons, @armor)

    # Execute transition

    Graphics.transition

    # Main loop

    loop do

      # Update game screen

      Graphics.update

      # Update input information

      Input.update

      # Frame update

      update

      # Abort loop if screen is changed

      if $scene != self

        break

      end

    end

    # Prepare for transition

    Graphics.freeze

    # Dispose of windows

    @item_window.dispose

  end

  #--------------------------------------------------------------------------

  # * Frame Update

  #--------------------------------------------------------------------------

  def update

    # Update windows

    @item_window.update

    update_item

  end

  #--------------------------------------------------------------------------

  # * Frame Update (when item window is active)

  #--------------------------------------------------------------------------

  def update_item

    # If B button was pressed

    if Input.trigger?(Input::B)

      # Play buzzer SE

      $game_system.se_play($data_system.buzzer_se)

      # Switch to map screen

      $scene = Scene_Map.new

      return

    end

    # If C button was pressed

    if Input.trigger?(Input::C)

      # Get currently selected data on the item window

      @item = @item_window.item

      # Play decision SE

      $game_system.se_play($data_system.decision_se)

      # Add the item to the inventory

      case @item

      when RPG::Item

        $game_party.gain_item(@item.id, 1)

        # Draw item window item

        @item_window.draw_item(@item_window.index)

      when RPG::Weapon

        $game_party.gain_weapon(@item.id, 1)

        # Draw item window item

        @item_window.draw_item(@item_window.index)

      when RPG::Armor

        $game_party.gain_armor(@item.id, 1)

        # Draw item window item

        @item_window.draw_item(@item_window.index)

      end

    end

  end

end

 

I will be extremely grateful to whoever can help me with this. Thanks.
 
i found why i was having issues saving and loading the chest data, i was using
[rgss]ChestData::Chests = Marshal.load(file)
[/rgss]
instead of:
[rgss]ChestData::Chests.merge!(Marshal.load(file))
[/rgss]
i feel really retarded, how did i miss that?

anyways here is new demo where all chest data is saved into save file: here
 
One more thing: when a chest is empty and you go to open it again, it lags for a split second. It's nothing huge, but it'll feel smoother if it would disallow the action button completely when the chest is empty. Is that an eventing thing or a scripting thing?
 
really? i cant get my game to do that...ill look at it for you real quick


edit: do you have lots of stuff that edited scene map or are you on a large map? the only lag i can find is when it is loading the map(because you aren't really on the map when you open the chest, its just a copy of the map.)

edit2: ya it is defiantly caused when i reload the map, im sorry but i dont think there is anyway around that. here is the way i plan on fixing it. i recommend you cut and paste your old scene_chest below the main just in case you dont like my new one.

[rgss]#==============================================================================
# ** Scene_Chest by Brewmeister, modified for Plague180's ChestData
#------------------------------------------------------------------------------
#  This class performs chest screen processing.
#  Create Chest:
#  ChestData::Chests['Chest Name'] = Chest.new([1],[1],[1])
#  Chest.new([items],[weapons],[armor])
#  Empty slots must still have [] EX: Chest.new([1],[],[2])
#  For rore then one item use: Chest.new([1,2,3],[],[])
#  Call with:
#  $scene = Scene_Chest.new('Chest Name')
#==============================================================================
 
class Scene_Chest
  #--------------------------------------------------------------------------
  # * Initialize
  #--------------------------------------------------------------------------
  def initialize(chest)
    # Grab chest data
    @chest = ChestData::Chests[chest]
  end
  #--------------------------------------------------------------------------
  # * Main Processing
  #--------------------------------------------------------------------------
  def main
    # Make menu background
    @spriteset = Spriteset_Map.new
    #Make new chest window
    @item_window = Window_Chest.new(@chest.data)
    # Execute transition
    Graphics.transition
    # Main loop
    loop do
      @item_window.opacity = 255
      # Update game screen
      Graphics.update
      # Update input information
      Input.update
      # Frame update
      update
      # Abort loop if screen is changed
      if $scene != self
        break
      end
    end
    if @chest.data.size == 0
      @item_window.opacity = 0
      @item_window.windowskin = nil
      $scene = Scene_Map.new
    end
    # Prepare for transition
    Graphics.freeze
    # Dispose of windows
    @item_window.dispose
  end
  #--------------------------------------------------------------------------
  # * Frame Update
  #--------------------------------------------------------------------------
  def update
    # Update windows
    @item_window.update
    update_item
  end
  #--------------------------------------------------------------------------
  # * Frame Update (when item window is active)
  #--------------------------------------------------------------------------
  def update_item
    # If B button was pressed
    if Input.trigger?(Input::B)
      # Play buzzer SE
      $game_system.se_play($data_system.buzzer_se)
      # Switch to map screen
      $scene = Scene_Map.new
      return
    end
    # If C button was pressed
    if Input.trigger?(Input::C)
      # Get currently selected data on the item window
      @item = @item_window.item
      # Play decision SE
      $game_system.se_play($data_system.decision_se)
      # Remove item from list
      @item_window.get_item
      case @item
      when RPG::Item
        # Add the item to the inventory
        $game_party.gain_item(@item.id, 1)
      when RPG::Weapon
        # Add the weapon to the inventory
        $game_party.gain_weapon(@item.id, 1)
      when RPG::Armor
        # Add the armor to the inventory
        $game_party.gain_armor(@item.id, 1)
      end
    end
  end
end
 
[/rgss]


edit again: i found better solution that fixes the lagg issue, making you a new demo will post soon
 
Alright thanks I'll give it a shot and then edit this post.

EDIT: Works perfectly! Can you give me a short tutorial in post form of how to make a chest with your script? Just to make sure I don't screw anything up :biggrin:
 
sure copy paste the one of the ones i made in the demo

1. change the name of the chest eg: ChestData::Chests['FieldChestOne']=Chest.new([],[],[])
2. add the item ids you want.(potion in example)ChestData::Chests['FieldChestOne']=Chest.new([1],[],[])
3. then go to pages 2 and 3 add name of chest $scene = Scene_Chest.new('FieldChestOne')
4. open the conditional branch on page 3 and change the name of the chest there as well. eg : Scene_Chest.check('FieldChestOne')

thats all you gotta do take like 20 seconds :D but if you are wondering why i do it that way, page one creates the chest and saves it to my data base(if you wanted you could add every chest in script instead of in the event, but i think keeping it in even is simpler). page two is the normal chest sitting waiting for some one to open it in all its glory. and the last page is to check before attempting to open it in case the chest doesnt have anything left, that being said if there is still something in the chest then go ahead and open it.

other fun facts:

if you want two potions(more then one of same item):
ChestData::Chests['FieldChestOne']=Chest.new([1,1],[],[])

if you want a potion, steel sword and 6 bronze armor(lots of items)
ChestData::Chests['FieldChestOne']=Chest.new([1],[3],[13,13,13,13,13,13])

edit:eek:ne last fun fact if you want the empty chest to open a small empty window for any reason all you have to do is remove the conditional branch(but keep the $scene = Scene_Chest.new("FeildChestOne'))

p.s. i love this script if you get any more ideas let me know :D
 

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