Pop-up item icon

Pop-up item icon
Version: 1.12

Introduction
With this script, a item icon will popup in the map everytime the player gains a new item/armor/weapon/gold. It is a great replacement for treasure chest scripts.

Screenshots
http://img206.imageshack.us/img206/6060/popupitemyi7.th.jpg[/IMG]

Script
script Spoiler
#==============================================================================
# ** Item Popup
#------------------------------------------------------------------------------
# Tibuda
# Version 1.12
# 02.23.07
#------------------------------------------------------------------------------
# * Version History :
# 01.11.07 - Version 1.00
# 01.13.07 - Version 1.01
#     - Bug fix: Bug while tryin' to unequip an item in the equipment scene
#     - Added: Item number font customization
# 01.19.07 - Version 1.10
#     - Added: Fading icons when losing items (optional)
#     - Added: Background image (optional)
#     - Added: Custom colors for each item
#     - Added: You can show a icon in events
#     - Added: Custom colors when calling the popup_icon
# 01.23.07 - Version 1.11
#     - Bug fix: Bug while changin'g class or changin' equip in the map
# 02.23.07 - Version 1.12 (one month)
#     - Bug fix: Showing icon with 0 items/gold
#------------------------------------------------------------------------------
# * Instructions
# - To show an icon call the following script:
#    $game_player.popup_icon(icon_name[, number, color])
#        for the player
#    self.event.popup_icon(icon_name[, number, color])
#        for the current event
#    $game_map.events[id].popup_icon(icon_name[, number, color])
#        for the the event id
# - Customizing the duration
#   ICON_TIME holds the time a icon is shown
#   ICON_DELAY is time a icon will wait to appear when there's a icon showing
# - Customizing the item number colors
#   The NUMBER_COLOR constant is the default color for all the item types
#   The GOLD_NUMBER_COLOR holds the color for the gold
#   The ITEM_NUMBER_COLOR hahs hold the color for specific items.
#   You can add a custom color for a item this way:
#    ITEM_NUMBER_COLOR = {item_id => item_color, item_id => item_color....}
#   If you want a default color for all items you can use the item_id 0
#   Use WEAPON_NUMBER_COLOR for weapons and ARMOR_NUMBER_COLOR for armors
#==============================================================================

#--------------------------------------------------------------------------
# module Popup_Item
#--------------------------------------------------------------------------
module Popup_Item
  # Fading icons
  FADE_LOSE    = false
  # The time we show the icon
  ICON_TIME    = 40
  # The time before show a new item
  ICON_DELAY   = 24
  # Icon to show when the player gets money
  GOLD_ICON    = "032-Item01"
  # A 32x32 background image
  BACKGROUND   = ""
  # Font for the item number
  NUMBER_FONT  = "Tahoma"
  NUMBER_SIZE  = 12
  NUMBER_COLOR = Color.new(255, 255, 255)
  # Custom font color for specific items
  GOLD_NUMBER_COLOR   = Color.new(255, 255, 0)
  ITEM_NUMBER_COLOR   = {}
  ARMOR_NUMBER_COLOR  = {}
  WEAPON_NUMBER_COLOR = {}
  # True if using Trickster's Method & Class Library
  TRICK_MACL = false
end

#--------------------------------------------------------------------------
# Game_Character
#--------------------------------------------------------------------------
class Game_Character
  attr_accessor :popup_icons
  alias popup_gm_party_init initialize
  def initialize
    popup_gm_party_init
    @popup_icons = []
  end
  def popup_icon(icon_name, number = 1, color = Popup_Item::NUMBER_COLOR)
    @popup_icons.push([icon_name, number, color])
  end
  def fade_icon(icon_name, number = 1, color = Popup_Item::NUMBER_COLOR)
    popup_icon(icon_name, -number, color) if Popup_Item::FADE_LOSE
  end
end
#--------------------------------------------------------------------------
# Game_Party
#--------------------------------------------------------------------------
class Game_Party
  attr_accessor :ignore_icon
  alias popup_gm_party_init initialize
  def initialize
    popup_gm_party_init
    @ignore_icon = false
  end
  def popup_icon(icon_name, number = 1, color = Popup_Item::NUMBER_COLOR)
    $game_player.popup_icon(icon_name, number, color)
  end
  def fade_icon(icon_name, number = 1, color = Popup_Item::NUMBER_COLOR)
    $game_player.fade_icon(icon_name, number, color)
  end
  alias popup_gm_party_gain_gold gain_gold
  def gain_gold(n)
    popup_gm_party_gain_gold(n)
    return if @ignore_icon || n < 1
    if Popup_Item::GOLD_ICON != "" and $scene.is_a?(Scene_Map)
      if Popup_Item::GOLD_NUMBER_COLOR.is_a?(Color)
        color = Popup_Item::GOLD_NUMBER_COLOR
      else
        color = Popup_Item::NUMBER_COLOR
      end
      popup_icon(Popup_Item::GOLD_ICON, n, color)
    end
  end
  alias popup_gm_party_gain_item gain_item
  def gain_item(id, n)
    popup_gm_party_gain_item(id, n)
    return if id < 1 or id >= $data_items.size or @ignore_icon || n < 1
    if $scene.is_a?(Scene_Map)
      if Popup_Item::ITEM_NUMBER_COLOR[id].is_a?(Color)
        color = Popup_Item::ITEM_NUMBER_COLOR[id]
      elsif Popup_Item::ITEM_NUMBER_COLOR[0].is_a?(Color)
        color = Popup_Item::ITEM_NUMBER_COLOR[0]
      else
        color = Popup_Item::NUMBER_COLOR
      end
      popup_icon($data_items[id].icon_name, n, color)
    end
  end
  alias popup_gm_party_gain_weapon gain_weapon
  def gain_weapon(id, n)
    popup_gm_party_gain_weapon(id, n)
    return if id < 1 or id >= $data_weapons.size or @ignore_icon || n < 1
    if $scene.is_a?(Scene_Map)
      if Popup_Item::WEAPON_NUMBER_COLOR[id].is_a?(Color)
        color = Popup_Item::WEAPON_NUMBER_COLOR[id]
      elsif Popup_Item::WEAPON_NUMBER_COLOR[0].is_a?(Color)
        color = Popup_Item::WEAPON_NUMBER_COLOR[0]
      else
        color = Popup_Item::NUMBER_COLOR
      end
      popup_icon($data_weapons[id].icon_name, n, color)
    end
  end
  alias popup_gm_party_gain_armor gain_armor
  def gain_armor(id, n)
    popup_gm_party_gain_armor(id, n)
    return if id < 1 or id >= $data_armors.size or @ignore_icon || n < 1
    if $scene.is_a?(Scene_Map)
      if Popup_Item::ARMOR_NUMBER_COLOR[id].is_a?(Color)
        color = Popup_Item::ARMOR_NUMBER_COLOR[id]
      elsif Popup_Item::ARMOR_NUMBER_COLOR[0].is_a?(Color)
        color = Popup_Item::ARMOR_NUMBER_COLOR[0]
      else
        color = Popup_Item::NUMBER_COLOR
      end
      popup_icon($data_armors[id].icon_name, n, color)
    end
  end
end
#--------------------------------------------------------------------------
# Game_Actor
#--------------------------------------------------------------------------
class Game_Actor
  alias popup_gm_actor_equip equip
  def equip(equip_type, id)
    $game_party.ignore_icon = true
    popup_gm_actor_equip(equip_type, id)
    $game_party.ignore_icon = false
  end
end
#--------------------------------------------------------------------------
# module RPG
#--------------------------------------------------------------------------
module RPG
  #--------------------------------------------------------------------------
  # RPG::Sprite
  #--------------------------------------------------------------------------
  class Sprite
    def popup_icon(name, number = 1, color = Popup_Item::NUMBER_COLOR)
      return if name == "" or name == nil
      return if number.is_a?(Numeric) and number < 0 and !Popup_Item::FADE_LOSE
      @icons = [] if [email protected]_a?(Array)
      icon = {}
      icon["time"]   = 0 - @icons.size * Popup_Item::ICON_DELAY
      if number.is_a?(Numeric) and number < 0
        icon["fade"] = true
        number       = - number
      else
        icon["fade"] = false
      end
      icon["sprite"] = RPG::Sprite.new(self.viewport)
      if Popup_Item::BACKGROUND != ""
        bitmap = RPG::Cache.picture(Popup_Item::BACKGROUND)
        icon["sprite"].bitmap = Bitmap.new(bitmap.width, bitmap.height)
        icon["sprite"].bitmap.blt(0, 0, bitmap, Rect.new(0,0,bitmap.width,bitmap.width))
      else
        icon["sprite"].bitmap = Bitmap.new(32,32)
       
      end
      bitmap = RPG::Cache.icon(name)
      icon["sprite"].bitmap.blt(4, 4, bitmap, Rect.new(0,0,24,24))
      if Popup_Item::NUMBER_SIZE > 0 and number != 1 and number != ""
        icon["sprite"].bitmap.font.outline = true if Popup_Item::TRICK_MACL
        icon["sprite"].bitmap.font.name  = Popup_Item::NUMBER_FONT
        icon["sprite"].bitmap.font.size  = Popup_Item::NUMBER_SIZE
        icon["sprite"].bitmap.font.color = color
        icon["sprite"].bitmap.draw_text(5, 28 - Popup_Item::NUMBER_SIZE - 1, 27, Popup_Item::NUMBER_SIZE, number.to_s)
      end
      icon["sprite"].ox = 0
      icon["sprite"].oy = 20
      icon["sprite"].x  = self.x
      icon["sprite"].y  = self.y - self.oy / 2 + 1 - icon["time"]
      icon["sprite"].z  = 3000
      icon["sprite"].visible = false
      @icons.push(icon)
    end
    def update_icons
      return unless @icons.is_a?(Array)
      return if Graphics.frame_count % 2 != 0
      i = 0
      while i < @icons.size
        if @icons[i]["time"] > Popup_Item::ICON_TIME
          @icons[i]["sprite"].bitmap.dispose
          @icons[i]["sprite"].dispose
          @icons.delete_at(i)
        else
          @icons[i]["time"] += 1
          if @icons[i]["fade"]
            op = [255 * (1 - @icons[i]["time"] / Popup_Item::ICON_TIME),0].max
            @icons[i]["sprite"].opacity = op
            @icons[i]["sprite"].update
          else
            @icons[i]["sprite"].y -= 1
          end
          @icons[i]["sprite"].visible = @icons[i]["time"] >= 0
          i += 1
        end
      end
    end
    alias popup_sprite_update update
    def update
      popup_sprite_update
      update_icons
    end
  end
end
#--------------------------------------------------------------------------
# Sprite_Character
#--------------------------------------------------------------------------
class Sprite_Character
  alias popup_spr_char_update update
  def update
    popup_spr_char_update
    if @character.is_a?(Game_Character)
      while @character.popup_icons.size > 0
        popup_icon(@character.popup_icons[0][0], @character.popup_icons[0][1],
                   @character.popup_icons[0][2])
        @character.popup_icons.delete_at(0)
      end
    end
    update_icons
  end
end
Expand to see the code.

Instructions
- Manually showing the icon
Call the following script for your need replacing the paramater. The number and color are optional.
$game_player.popup_icon(icon_name, number, color)
# for the player
self.event.popup_icon(icon_name, number, color)
# for the current event
$game_map.events[id].popup_icon(icon_name, number, color)
# for the the event id
Expand to see the code.

- Customizing the duration
ICON_TIME holds the time a icon is shown
ICON_DELAY is time a icon will wait to appear when there's a icon showing

- Customizing the item number colors
The NUMBER_COLOR constant is the default color for all the item types
The GOLD_NUMBER_COLOR holds the color for the gold
The ITEM_NUMBER_COLOR hahs hold the color for specific items.
You can add a custom color for a item this way:
ITEM_NUMBER_COLOR = {item_id => item_color, item_id, item_color....}
Expand to see the code.

If you want a default color for all items you can use the item_id 0
Use WEAPON_NUMBER_COLOR for weapons and ARMOR_NUMBER_COLOR for armors.
These values are a Color object. You can call them this way:
Color.new(red, green, blue)
Expand to see the code.

or if you are using Trickster's MACL
Color.red
Color.green
Color.blue
Color.yellow
Color.purple
Color.orange
Color.white
Color.black
Color.gray
Expand to see the code.

Compatibility
I guess it will work with everything, but not sure. XD

Credits and Thanks
It's just stupid, I don't need credit for this. :p
Hey, i cant use the script..

i receive an error:

??????''?58???? No method error?????
undefined method `icon_name' for :NilClass.


thanks
You need to go to:

# Icon to show when the player gets money
GOLD_ICON = "032-Item01"

and change "032-Item01" to whichever Icon you want for gold.
At least I'm fairly certain that's what you need.

-----------------------------------------------

And now I have an issue. It seems to me it's supposed to display the number of attained items if they are above One, but it's not displaying.
Well, that not result. i copied the script equally... and i change the name... and nothing...
what is the problem?

some script?, i use the sdk, and the character icon graphic, by sephirot.. but i dont think this is the problem...

whats up?
[QUOTE=mephisto;132251]Well, that not result. i copied the script equally... and i change the name... and nothing...
what is the problem?

some script?, i use the sdk, and the character icon graphic, by sephirot.. but i dont think this is the problem...

whats up?[/QUOTE]


Go to line 22 and change that to false, and see what happens.
@mephisto
I really don't now whats goin on, i have this script working with seph's test bed (and character icon graphic) fine. try to change the order in script editor. Edit: Twar3Draconis is right, if you are not using Trickster's MACL that must be false. I will edit that in my first post.

@Joy
The NUMBER_SIZE constant must be greater than zero to show the item number you gain.
It's at default, does not work.
@Joy
try adding this code after line 75
        icon["sprite"].bitmap.font.name = "Tahoma"
        icon["sprite"].bitmap.font.color = Color.new(0,0,0)
Expand to see the code.

just a question, are you using the method library from trickster? if you are not, the text is supposed to be white.
Fantastic! Now it works perfectly [needed to change (0,0,0) to (255,255,255)]. Looks beautiful. I'll be adding it to A Simple Wish, so welcome to the scripting credits page!

Thanks so very much for this nifty feature. :)
Joy found a Bug in the equipment scene while changing the equips. I've fixed that and changed the post. I've added too a way to change the font and color for the item number you gain.
Thanks a lot, now the script is right!..
Can you modify the Script to show the numbers in different colors. And to show a Background image ( like a mini-message-box


ThatÂÂ ´ll be great
This reminds me of Star Ocean for the SNES.
Cool
@mephisto
Good it's working now. You just turned the MACL constant false or you did something else?
@Sir Lord Biowulve
You mean different colors for each item? That should be easy. About the background, I don't think it will look good, but we must see.
@Chaos_Prime
Good you think it's cool.
Quote:
You mean different colors for each item? That should be easy

I mean, that the user can call the PopUp by using this:
Quote:
$game_party.popup_icon(Icon_name, number, number_color)


number_color = a colorname like "red" or "yellow"

to show the Text ( the number ) in a different color

Quote:
About the background, I don't think it will look good, but we must see.


Perhaps a Picture thatÂÂ ´s stored in the Picture folder ( 32x32 ) and that could be changed by the user.

I stored an example of a Background image in the attachment.
@Sir Lord Biowulve
I'll be working on it
thx
Update!!! Check first post.
# 01.19.07 - Version 1.10
# - Added: Fading icons when losing items (optional)
# - Added: Background image (optional)
# - Added: Custom colors for each item
# - Added: You can show a icon in events
# - Added: Custom colors when calling the popup_icon
now itÂÂ ´s perfect...thx
@Sir Lord Biowulve
nope
ok..itÂÂ ´s perfect in my point of view ^^
i get an error when changing the class of the character:

undefined method ÂÂ ´icon_name for nilclass
this is yet so simple and so usefull :)

nicely done :)
@Kbrito: i'll check it
@Freakboy: :thumb:

Edit: Update!
# 01.23.07 - Version 1.11
# - Bug fix: Bug while changin'g class or changin' equip in the map
This is great, I've replaced all of my item giving with it (got lots of icons, may as well use them...) I modded the values for the first type of popping, so that the item always moves directly up and back down again, and never to the sides, as if rising from the chest, because thats more the effect I preferred, seemed silly to me to have it flying off somewhere...

It works fine, but it lags horribly when the player moves after grabbing the item...just for a moment, but its enough to be seriously irritating. Also, if you grab two items quickly, one after another (before the notice window for the 1st one dissappears) that window then doesn't dispose correctly until you force the game to dispose its windows (through opening the menu, or suchlike).

Anyways to fix this? Cause its great otherwise...
@eharper256
I will look for this lag when getting two items.
But, what windows are you talking about? There's no windows in my script. Can you explain how are you trying to "force the game to dispose its windows"??
Wait a minute. I'm a moron, Ignore everything I said... My script isn't this one, its the star ocean chests...':| God I'm an idiot. Actually, I'll probably use this one now, it looks more efficient. (Smacks self with large trout)...O_o
I really want to use this script but I cant figure out how to use it, I get a syntax error everytime. If some one can please explain what I exactly have to do please let me know. for example I dont know what to put into the call script

$game_player.popup_icon(icon_name, number, color)
# for the player
self.event.popup_icon(icon_name, number, color)
# for the current event
$game_map.events[id].popup_icon(icon_name, number, color)
# for the the event id

Please help me
@CloudsEternalLove
What do you want to do with the script??
It will automatically show the icons when you get items. If this is what you want, you don't have to do anything else than adding my script above main.
If you want something else, you can use these commands to show icons in other places. If you wanna show the icon "001-Weapon01" in an event of id 33 you call this script:
$game_map.events[33].popup_icon("001-Weapon01")
Expand to see the code.
I found a bug hehe.. If you use this with an ABS system (I'm using MrMo's) and you kill a creature it shows the gold icon (bag for me, default) even if you get "0" gold. It's an easy fix I'm sure but I don't know how to, well, just thought I'd bring this to your attention, great script by the way very useful.

Original thread: https://www.hbgames.org/forums/viewtopic.php?t=12455