How do I check by name if an item exists in the database and then return the ID of the item?
This is in RGSS3.
This is in RGSS3.
id = 1 for i in $data_items check = $data_items[id]p check.name if check.name == i.name modid = id break end id += 1 end
def find_first(name) $data_items.compact.select { |i| i.name[name] }.first.idrescue nilend
Sure. Basically, I just used the "index" method on String the same way that you'd use it on an Array -- the side effect of this is that it essentially searches the string for the given contents and returns the substring if those contents are found (or nil if the given contents don't exist). The most interesting thing about this, to me, is that it works just as well with substrings as it does with regular expressions.
Pretty much. It's also in case an exception occurs at any step along the way -- but it will only rescue exceptions which are descendants of StandardError, so if something really interesting happens, we'll still have something raised.
def find_first(name) $data_items.compact.select { |i| i.name[name] }.first.id rescue nilend# This works, too, but it's terribly verbose.def find_first(name) begin $data_items.compact.select { |i| i.name[name] }.first.id rescue nil endend
def find_first(name) (result = $data_items.compact.find { |i| i.name[name] }) && result.idend
def find_first(name) result = $data_items.compact.find { |i| i.name[name] } result.id if resultend
Original thread: https://forums.rpgmakerweb.com/threads/check-by-name-if-an-item-exists.28595/