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.

VX Event Items

OS

Sponsor

VX Event Items
By OS

Purpose

This script allows NPCs to hold items. It allows the user to choose which items can be given to NPCs, and allows the player to give items to NPCs, and to take them away. The user can also set a maximum number of items that can be held by the NPC.

Script

Code:
#===============================================================================
# Event Items
# Version: 1.0.0 VX
#
# This is loosely based on the script I wrote for XP. This script is much shorter
# and cleaner than the XP script. I copied Scene_Item and altered it very slightly
#(and deleted every Target reference), and I altered the initialize method of
# Game_Event, and added three methods.
#
# In order to use this script effectively, use
#
# $scene = Scene_TransferItem(event_id), where event_id is the id of the event
# that the items will be stored in.
#
# $game_map.events[event_id].max_items = n, where n is the maximum number of
# items the event will be allowed to hold.
#
# $game_map.events[event_id].give_item(item_id), where item_id is one of the
# Item IDs from your database. This will only work if the ID is also in the
# Module OS#GiftItems array at the top of the script.
#
# $game_map.events[event_id].lose_item(item_id), to directly remove an item that
# an NPC has from their inventory.
#
# It's lightweight and easy to use. Give credit, have fun, and enjoy!
#===============================================================================
module OS
  GiftItems = [1, 3, 4, 6]
end

class Game_Event < Game_Character
  attr_accessor :items
  def initialize(map_id, event)
    super()
    @map_id = map_id
    @event = event
    @id = @event.id
    @erased = false
    @starting = false
    @through = true
    @items = []
    @max_items = 10
    moveto(@event.x, @event.y)            # Move to initial position
    refresh
  end
  
  def give_item(item_id)
    if @items.size < @max_items
      @items.push(item_id)
      return true
    end
    return false
  end
  
  def take_item(item_id)
    if @item.include?(item_id)
      @item.delete(item_id)
      return item_id
    end
    return 0
  end
  
  def max_items(count)
    @max_items = count
  end
end

class Scene_ItemTransfer < Scene_Base
  #--------------------------------------------------------------------------
  # * Initialize variables
  #--------------------------------------------------------------------------
  def initialize(event_id)
    @event_id = event_id
  end
  #--------------------------------------------------------------------------
  # * Start processing
  #--------------------------------------------------------------------------
  def start
    super
    create_menu_background
    @viewport = Viewport.new(0, 0, 544, 416)
    @help_window = Window_Help.new
    @help_window.viewport = @viewport
    @item_window = Window_Item.new(0, 56, 544, 170)
    @item_window.viewport = @viewport
    @item_window.help_window = @help_window
    @item_window.active = true
  end
  #--------------------------------------------------------------------------
  # * Termination Processing
  #--------------------------------------------------------------------------
  def terminate
    super
    dispose_menu_background
    @viewport.dispose
    @help_window.dispose
    @item_window.dispose
  end
  #--------------------------------------------------------------------------
  # * Return to Original Screen
  #--------------------------------------------------------------------------
  def return_scene
    $scene = Scene_Map.new
  end
  #--------------------------------------------------------------------------
  # * Update Frame
  #--------------------------------------------------------------------------
  def update
    super
    update_menu_background
    @help_window.update
    @item_window.update
    if @item_window.active
      update_item_selection
    end
  end
  #--------------------------------------------------------------------------
  # * Update Item Selection
  #--------------------------------------------------------------------------
  def update_item_selection
    if Input.trigger?(Input::B)
      Sound.play_cancel
      return_scene
    elsif Input.trigger?(Input::C)
      @item = @item_window.item
      if @item != nil
        $game_party.last_item_id = @item.id
      end
      if OS::GiftItems.include?(@item.id)
        determine_item
      else
        Sound.play_buzzer
      end
    end
  end
  #--------------------------------------------------------------------------
  # * Confirm Item
  #--------------------------------------------------------------------------
  def determine_item
    if $game_map.events[@event_id].give_item(@item.id)
      $game_party.consume_item(@item)
      @item_window.draw_item(@item_window.index)
      Sound.play_decision
      return
    end
    Sound.play_buzzer
  end
end

Notes and Instructions

This is loosely based on the script I wrote for XP. This script is much shorter and cleaner than the XP script. I copied Scene_Item and altered it very slightly (and deleted every Target reference), and I altered the initialize method of Game_Event, and added three methods.

In order to use this script effectively, use

$scene = Scene_TransferItem(event_id), where event_id is the id of the event that the items will be stored in.

$game_map.events[event_id].max_items = n, where n is the maximum number of items the event will be allowed to hold.

$game_map.events[event_id].give_item(item_id), where item_id is one of the Item IDs from your database. This will only work if the ID is also in the Module OS#GiftItems array at the top of the script.

$game_map.events[event_id].lose_item(item_id), to directly remove an item that an NPC has from their inventory.

It's lightweight and easy to use. Give credit, have fun, and enjoy!

FAQ

None yet.

Screens

http://i64.photobucket.com/albums/h163/ ... temsVX.png[/img]


~OS
 

OS

Sponsor

@tuti05 - I never received errors, and I cannot check (VX Trial is over), so all I can say right now is try taking the leading 0's out of your numbers (i.e. instead of 001 use 1).
 

tuti05

Member

i tried and it still messed up can anyone post a demo or if you can tell me the instructions in a different way?
 

OS

Sponsor

Oh, yeah. Try putting the last three lines first, and $scene = Scene_TransferItem(9) last. You have to build NPC Inventories before you tell it to open the inventories. That should fix it.
 

tuti05

Member

Actually that sounded like it would work 100% but now in the working order an error pops up saying "max_items" is an unidentified method. I switched the order up a bit just to make sure and it said the same for "transfer_item"
 
Could you please convert this to XP. Also some suggestions to make it better are: that you can have a certain 'if' call script to make it true when a specific NPC is holding a specific item , that way this could make for awsome quests. Also the call script should include the NPCs name and item held, so that it makes it easy to use. This could be a really useful script.
 

OS

Sponsor

Sorry to disappoint, tuti05. I still don't have VX, and I still stick to the fact that it worked 100% for me. I wish I could test it, but I can't just yet. When I hook up my semi-new computer I'll get the Trial on it, and then I can fix this little problem.

As for a conversion, mephisto contacted me about adding some features to the XP version I already released, so it would be more like this version. Here is what I have so far:

Code:
#===============================================================================
# NPC Inventories
# By OS
#===============================================================================
# Use:
#
# All Events can be given items, but in order to do so, in the event you must
# Call Script: $scene = Scene_GiveItem.new(event_id), where event_id is the
# numerical id of the event.
#
# NEW
# In Module OS, in the Giveable array, you can put in the Item IDs of items
# that can be given to NPCs.
#
# NEW
# To check if a particular item has been given to an NPC, use
# Conditional Branch: Script: $game_map.events[id].items_include?(item_id),
# where event_id is the event's id, and item_id is the id of the particular
# item you are checking for.
#
# EDIT
# You can see how many items an NPC has by using Call Script:
# i = $game_map.events[event_id].item_count
#
# EDIT 2
# You can now set a maximum amount of items for an event by using:
# $game_map.events[event_id].max_item_count = amount
#
# To add an item directly to an NPC, use:
# $game_map.events[event_id].items << item_id
#
# To delete an item from an NPC's inventory, use:
# $game_map.events[event_id].remove_item(item_id)
#===============================================================================
module OS
  Giveable = [1,2,3]
end

class Game_Event
  attr_accessor :items, :max_item_count
  alias os_init initialize
  def initialize(map_id, event)
    super()
    @items = []
    @max_item_count = 10
    os_init(map_id, event)
  end
  
  def items_gained?
    return @items
  end
  
  def item_count
    return @items.size
  end
  
  def items_include?(item_id)
    if @items.include?(item_id)
      return true
    end
    return false
  end
  
  def remove_item(item_id)
    if items_include?(item_id)
      @items.delete(item_id)
    end
  end
end

#==============================================================================
# ** Scene_Item
#------------------------------------------------------------------------------
#  This class performs item screen processing.
#==============================================================================

class Scene_GiveItem
  def initialize(npc_id)
    @npc_id = npc_id
  end
  #--------------------------------------------------------------------------
  # * Main Processing
  #--------------------------------------------------------------------------
  def main
    # Make help window, item window
    @help_window = Window_Help.new
    @item_window = Window_Item.new
    # Associate help window
    @item_window.help_window = @help_window
    # 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
    @help_window.dispose
    @item_window.dispose
  end
  #--------------------------------------------------------------------------
  # * Frame Update
  #--------------------------------------------------------------------------
  def update
    # Update windows
    @help_window.update
    @item_window.update
    # If item window is active: call update_item
    if @item_window.active
      update_item
      return
    end
  end
  #--------------------------------------------------------------------------
  # * Frame Update (when item window is active)
  #--------------------------------------------------------------------------
  def update_item
    # If B button was pressed
    if Input.trigger?(Input::B)
      # Play cancel SE
      $game_system.se_play($data_system.cancel_se)
      # Switch to menu 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
      # If not a use item
      unless @item.is_a?(RPG::Item)
        # Play buzzer SE
        $game_system.se_play($data_system.buzzer_se)
        return
      end
      for i in 0..OS::Giveable.size - 1
        if @item.id == OS::Giveable[i] and
          $game_map.events[@npc_id].items.size < $game_map.events[@npc_id].max_item_count
          $game_party.lose_item(@item.id,1)
          $game_map.events[@npc_id].items.push(@item.id)
        end
      end
      $scene = Scene_GiveItem.new(@npc_id)
      return
    end
  end
end

Read the instructions in the header to learn how to use it. Peace!

~Owen Sael
 

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