(SOLVED)making script call for numeric collectables

This was hard to name but I am trying to make a system for my map script. It shows when you have found all the collectables on the current map. In this case, it's one of the same collectable that is the same item given. It needed to be linked to the map itself and another id for which collectable. so I tried making a system that the dev could just add to the game system itself is their way for me to get this script call to work $game_system.collectable_add(map_id, "collectable_name", amount)


Code:
class Game_System
  attr_accessor     :collectable
  attr_accessor     :collectablename
  alias jake_MAP_gamesystem   initialize
  def intitilize
    $collectable = []
    $collectable[] = Array.new()
    @collectablename = ""
    jake_MAP_gamesystem()
  end
 
  def collectable_get(id,collect)
    return if @collectable == nil
    p($collectable[id][collect])
  end
 
  def collectable_add(id, collect, val)
    p($collectable[id][collect])
    return if $collectable[id][collect] == nil
    $collectable[id] = Array.new() if $collectable[id][collect] == nil
    $collectable[id][collect] += val
    p($collectable[id][collect])
  end
 I also am trying to get my map script to read this in an if command has well. 
  def collectable_set(id, collect ,val)
    $collectable = []
    $collectable[id] = Array.new()
    $collectable[id][collect] = val
  end
 
  def collectable
    $collectable
  end
end
The first issue I see here is that you've misspelled initialize, meaning the "initilize" method won't be treated as a constructor, won't be run when the object is created, and subsequently your $collectable array and @collectablename instance variable will never exist to be referenced later.
You're also mixing up your variables: $collectible (a global variable) is not the same as @collectible (an instance variable). You should use @collectible.

I think you're trying to use an array as a hash, which won't work. Arrays should be used for things that need to be kept in order. In this case, I think you should just use a hash.

You can do something like so:
Ruby:
def collectible_add(id, collect, val)
  # Create the hash if the ID does not already exist as a key
  # We use Hash.new(0) to give everything a default value of 0
  @collectible[id] = Hash.new(0) unless @collectible.key?(id)
  p(@collectible[id][collect])
  @collectible[id][collect] += val
  p(@collectible[id][collect])
end

And then in the initialize method, you would set the @collectibleinstance variable as a hash like so: @collectible = {}

Here's some more information on hashes.
you've misspelled initialize, meaning the "initilize" method
I'm so embarrassed. I am so dumb I have been working on this script for so long I never noticed the one word I have spelled many times at this point.

You're also mixing up your variables: $collectible (a global variable) is not the same as @collectible (an instance variable). You should use @collectible.
so a bit of that was I forgot to clean up the many ways I tried getting this to work.
I think you're trying to use an array as a hash
Hashes I forgot about those I haven't programmed with them much yet. My funny way of learning new things when needed. do I also need to do anything fancy for def collectable_get(id, collect) and def collectable_set(id, collect ,val). I am guessing the p commands are so I can make sure it works.
Yep, the p commands are just so you can see the values printed.

You've got an attr_accessor for the @collectible instance variable, which means you don't need to make your own getter/setter methods (the attr_accessor does that for you, so you can just use $game_system.collectible[id][collect]). However, I'd suggest making your own instead of doing the attr_accessor, because at the moment you'll get an error if you try getting a value that doesn't exist. So you could do something like:
Ruby:
def collectible(id, collect)
  # Or whatever value you want to return if the id and collect keys don't exist
  return 0 unless @collectible.key?(id) && @collectible[id].key?(collect)
  return @collectible[id][collect]
end

Then you can get it by using $game_system.collectible(id, collect). collectible_add(id, collect, value) is already doing the setting for you, but you could do:
Ruby:
def set_collectible(id, collect, value)
  return unless @collectible.key?(id) && @collectible[id].key?(collect)
  @collectible[id][collect] = value
end
Replace the following line:

$collectable[id] = Array.new() if $collectable[id][collect] == nil

...with this one:

@collectable[id] ||= []

The ||= operator means ignore the value to the right handside if it's not equal to nil (nothing). It'd only fully execute it if that array has never been created.

Well, I've noticed they've already told you about not creating a new global variable because it's darn unnecessary anyway. Follow their advice.
Replace the following line:

$collectable[id] = Array.new() if $collectable[id][collect] == nil

...with this one:

@collectable[id] ||= []

The ||= operator means ignore the value to the right handside if it's not equal to nil (nothing). It'd only fully execute it if that array has never been created.

Well, I've noticed they've already told you about not creating a new global variable because it's darn unnecessary anyway. Follow their advice.
totally forgot I still had not told this to be closed after the last post made. Your comment was not fully in vain I now know what || does other than or in an if command

This thread is being closed, due to being solved. If for some reason you would like this thread re-opened, please report this post and leave a message why. Thank you.

Original thread: https://forums.rpgmakerweb.com/threads/solved-making-script-call-for-numeric-collectables.136832/