I've created this script that is supposed to give all items and equipment a variable called "space". I've created a script so I can use notetags for this:
module DRAGONISW
ITEM_SPACE = /<(?:SPACE|space):[ ](\d+)>/i
end # DRAGONISW
#==============================================================================
# ? DataManager
#==============================================================================
module DataManager
#--------------------------------------------------------------------------
# alias method: load_database
#--------------------------------------------------------------------------
class <<self; alias load_database_isw load_database; end
def self.load_database
load_database_isw
load_notetags_isw
end
#--------------------------------------------------------------------------
# new method: load_notetags_aim
#--------------------------------------------------------------------------
def self.load_notetags_isw
groups = [$data_items, $data_weapons, $data_armors]
for group in groups
for obj in group
next if obj.nil?
obj.load_notetags_isw
end
end
end
end # DataManager
#==============================================================================
# ? RPG::BaseItem
#==============================================================================
class RPG::BaseItem
#--------------------------------------------------------------------------
# public instance variables
#--------------------------------------------------------------------------
attr_accessor :space
#--------------------------------------------------------------------------
# common cache: load_notetags_aim
#--------------------------------------------------------------------------
def load_notetags_isw
@space = 0
#---
self.note.split(/[\r\n]+/).each { |line|
case line
#---
when DRAGONISW::ITEM_SPACE
@space = $1.to_i
end
} # self.note.split
#---
end
end # RPG::BaseItem
#==============================================================================
# Game_Interpreter
#==============================================================================
class Game_Interpreter
def inv_item_space
amount = 0
for i in 0..999
item = $data_items[i]
amount += $game_party.item_number(item) * item.space
end
return amount
end
end # Game_Interpreter
I try testing it out by calling the script code:
$game_variables[2] = inv_item_spaceBut I keep getting this error:
QuoteScript 'Game_Interpreter' line 1409: NoMethodError occurred.
undefined method `space' for nil:NilClass
I tried testing it on a brand new game and I still got this error. I've looked at many script that use notetags and it seems like I'm doing it correctly, so I don't understand what I'm doing wrong.
The first element of $data_items, $data_weapons and $data_armors is a nil value. This is because there isn't an item with ID of 0. It starts at 1. This is the same for any of the other information in the database too.
You will want to move to the next ID if the item found is nil:
Something like:
def inv_item_space
amount = 0
for i in 0..999
item = $data_items[i]
next if item.nil?
amount += $game_party.item_number(item) * item.space
end
return amount
end
should help avoid that.
Quote from: LoganF on February 03, 2013, 11:08:14 PM
The first element of $data_items, $data_weapons and $data_armors is a nil value. This is because there isn't an item with ID of 0. It starts at 1. This is the same for any of the other information in the database too.
You will want to move to the next ID if the item found is nil:
Something like:
def inv_item_space
amount = 0
for i in 0..999
item = $data_items[i]
next if item.nil?
amount += $game_party.item_number(item) * item.space
end
return amount
end
should help avoid that.
Thanks that worked perfectly. It's funny because I tried making it "for i in 1..999", but that didn't work either.
I gave that a go too, but that came up with the same error: a "no method for nil:NilClass". This just ensures that you won't be calling the method on a nil object.
Well, you could limit it to the number of items in the array, so instead of 0..999, it could be:
for i in 1...$data_items.size
But anyway, the nil check does the trick too and there might be some custom scripts out there which would make it so that some elements were nil even limiting it to the size of $data_items.