accesing a weapon in RPG::Weapons

Hey every one here is what I'm working with. My question is, how do I access a different weapon from the RPG::Weapons class.

Code:
#==============================================================================# ** RPG::Weapon#------------------------------------------------------------------------------#  This class handles the weapon's w_type, price and features.#==============================================================================class RPG::Weapon < RPG::EquipItem  #--------------------------------------------------------------------------  # * Public Instance Variables  #--------------------------------------------------------------------------  attr_accessor :prefix, :suffix  #--------------------------------------------------------------------------  # * Aliasing Method: Initializing Processing  #--------------------------------------------------------------------------  alias :rpgi5254252452 :initialize  #--------------------------------------------------------------------------  # * Initializing Processing  #--------------------------------------------------------------------------  def initialize    rpgi5254252452()    @prefix = @suffix = $data_weapons[5]  endend
I think that each member of $data_weapons returns an instance of that class... so each weapon is it's own instance of RPG::Weapon...

anyway, to access any weapon, just use the $data_weapons array

though if that's the case, what ur doing might not work for some... since it might be called before the other weapon is created...

like if for example they initialize with respect to ID, then weapon 5 will initialize before weapon 6, so weapon 5 probably won't be able to invoke the properties of weapon 6 during initialization...

so you might want to move that @prefix = @suffix = $data_weapons[5] somewhere else... somewhere that will surely only run once all weapons have been initialized already

like do it outside...

Code:
#Make sure you do this after $data_weapons has been madeweapon = RPG::Weapon.new#or you might need to Marshal load a marshal dump of that, #I think the other affix script does thatweapon.suffix = weapon.prefix = $data_weapons[5]
You're better off just using an ID and not an instance of the class, and when you need to use it, just use $data_weapons[id].
does that work when creating new weapons?
You'd only need to use it (refer to $data_weapons[id]) during the game though, not when creating them.
Is this what you ment Shaz?

Just for testing I have it setup to automatically assign it the name of weapon 5.

Now as for the script here is what I currently have. I tried the marshal dump method and. it is returning a nil integer error line 31 and I'm guessing 38 once this one is fixed. 

I can set up all the unless nil's later. I just want to get it working first. 

Spoiler
Code:
#==============================================================================# ** RPG::Weapon#------------------------------------------------------------------------------#  This class handles the weapon's w_type, price and features.#==============================================================================class RPG::Weapon < RPG::EquipItem  #--------------------------------------------------------------------------  # * Public Instance Variables  #--------------------------------------------------------------------------  attr_accessor :prefix, :suffix  #--------------------------------------------------------------------------  # * Aliasing Method: Initializing Processing  #--------------------------------------------------------------------------  alias :rpgi5254252452 :initialize  #--------------------------------------------------------------------------  # * Initializing Processing  #--------------------------------------------------------------------------  def initialize    rpgi5254252452()    @prefix = @suffix = 5  end   #--------------------------------------------------------------------------  # * Prefix  #--------------------------------------------------------------------------  def prefix    return Marshal.load(Marshal.dump($data_weapons[@prefix]))  end   #--------------------------------------------------------------------------  # * Suffix  #--------------------------------------------------------------------------  def suffix    return Marshal.load(Marshal.dump($data_weapons[@suffix]))  end   #--------------------------------------------------------------------------  # * Name  #--------------------------------------------------------------------------  def name    return "%s + %s + %s" %[prefix.name, @name, suffix.name]  end   #--------------------------------------------------------------------------  # * Price  #--------------------------------------------------------------------------  def price    return prefix.price + @price + suffix.price  end   #--------------------------------------------------------------------------  # * Parameters  #--------------------------------------------------------------------------  def params    params = @params.dup    8.times {  |i| params[i] += prefix.params[i] } unless prefix == nil    8.times {  |i| params[i] += suffix.params[i] } unless suffix == nil    return params  end   #--------------------------------------------------------------------------  # * Features  #--------------------------------------------------------------------------  def features    @features.concat(prefix.features)    @features.concat(suffix.features)    return @features  end   end
I'm not sure why you're doing the Marshal.load(Marshal.dump bit. Just return $game_weapons[id]. But yes, that's what I was thinking of.
It's still the same error.

Spoiler
Code:
#==============================================================================# ** RPG::Weapon#------------------------------------------------------------------------------#  This class handles the weapon's w_type, price and features.#==============================================================================class RPG::Weapon < RPG::EquipItem  #--------------------------------------------------------------------------  # * Public Instance Variables  #--------------------------------------------------------------------------  attr_accessor :prefix, :suffix  #--------------------------------------------------------------------------  # * Aliasing Method: Initializing Processing  #--------------------------------------------------------------------------  alias :rpgi5254252452 :initialize  #--------------------------------------------------------------------------  # * Initializing Processing  #--------------------------------------------------------------------------  def initialize    rpgi5254252452()    @prefix = @suffix = 5  end   #--------------------------------------------------------------------------  # * Prefix  #--------------------------------------------------------------------------  def prefix    return $data_weapons[@prefix]  end   #--------------------------------------------------------------------------  # * Suffix  #--------------------------------------------------------------------------  def suffix    return $data_weapons[@suffix]  end   #--------------------------------------------------------------------------  # * Name  #--------------------------------------------------------------------------  def name    return "%s + %s + %s" %[prefix.name, @name, suffix.name]  end   #--------------------------------------------------------------------------  # * Price  #--------------------------------------------------------------------------  def price    return prefix.price + @price + suffix.price  end   #--------------------------------------------------------------------------  # * Parameters  #--------------------------------------------------------------------------  def params    params = @params.dup    8.times {  |i| params[i] += prefix.params[i] } unless prefix == nil    8.times {  |i| params[i] += suffix.params[i] } unless suffix == nil    return params  end   #--------------------------------------------------------------------------  # * Features  #--------------------------------------------------------------------------  def features    @features.concat(prefix.features)    @features.concat(suffix.features)    return @features  end   end
Your features method is going to go really wrong after accessing it a couple times.


For the integer issue, the engine likely did not initialize it.
"Your features method is going to go really wrong after accessing it a couple times."~ Because it concats every time it is called right? BTW I took this method out for now as it is not needed to test.

"For the integer issue, the engine likely did not initialize it."~ How could I fix that.
What precisely are you attempting to do?  Are you trying to return the weapon the prefix is from?  What type of return data are you expecting?  Since you use prefix.name I can only assume you want to return the base item that stores the prefixes, but you can easily just return the base item ID number and grab the weapon that way.

I'm just not sure why you're working within the Weapon class.  Anything I can think of that you're attempting can be done already outside of it, once things are initialized.  You don't need to save or return the prefix/suffix, you just need the weapon id.  That's a standard integer, easy to save during weapon creation.
"What precisely are you attempting to do?  Are you trying to return the weapon the prefix is from?  What type of return data are you expecting?  Since you use prefix.name I can only assume you want to return the base item that stores the prefixes, but you can easily just return the base item ID number and grab the weapon that way."

If you look at the prefix method that is what I am doing, and it still doesn't work even if I do it like so...

Spoiler
Code:
#==============================================================================# ** RPG::Weapon#------------------------------------------------------------------------------#  This class handles the weapon's w_type, price and features.#==============================================================================class RPG::Weapon < RPG::EquipItem  #--------------------------------------------------------------------------  # * Public Instance Variables  #--------------------------------------------------------------------------  attr_accessor :prefix, :suffix  #--------------------------------------------------------------------------  # * Aliasing Method: Initializing Processing  #--------------------------------------------------------------------------  alias :rpgi5254252452 :initialize  #--------------------------------------------------------------------------  # * Initializing Processing  #--------------------------------------------------------------------------  def initialize    rpgi5254252452()    @prefix = @suffix = 5  end   #--------------------------------------------------------------------------  # * Name  #--------------------------------------------------------------------------  def name    return "%s + %s + %s" %[$data_weapons[@prefix].name, @name, $data_weapons[@suffix].name]  end   #--------------------------------------------------------------------------  # * Price  #--------------------------------------------------------------------------  def price    return $data_weapons[@prefix].price + @price + $data_weapons[@suffix].price  end   #--------------------------------------------------------------------------  # * Parameters  #--------------------------------------------------------------------------  def params    params = @params.dup    8.times {  |i| params[i] += $data_weapons[@prefix].params[i] } unless @prefix == nil    8.times {  |i| params[i] += $data_weapons[@suffix].params[i] } unless @suffix == nil    return params  end end
Wrap the accessor to prefix and suffix to set if nil and only use the accessor method. You can't rely on initialize here because initialize is never run on any RPG::Weapon you didn't create yourself (this includes everything in the $data_weapons array)

Code:
  attr_writer :prefix, :suffix  def prefix    @prefix ||= default_prefix  end  def suffix    @suffix ||= default_suffix  end
The assignment will only run if the instance variables on the left are nil or false, otherwise, the value in the instance var would be returned. When putting out the final version, you can use a non-nil value (in this case, 0 would be handy) as the default to indicate "no prefix/suffix present". Just remember to always use the accessor when reading these variables.
Haha, kinda reminds me of how I tried to attach sprite to cursor_rect (which is hidden).

This might be a helpful reference to you, what I did basically:

1. set attr_accessor (named nickname) on Rect class since cursor_rect is from Rect class.

2. during window initialization, set cursor_rect nickname as 'asd'

3. make method inside of the rect that can only function if self.nickname = 'asd'

Now how to implement that to your problem, since you're working with notetag, you could define and initialize the value of the prefix, suffix in the data structure, then on the method, it will return the weapon if it has the prefix that you're looking for. I don't think you need 'id' in this case since the prefix would be a clone of 'id'.

Just for trial-error

Try this approach:

Code:
class RPG::Weapon < RPG::EquipItem  def get_desired_weapon; return self if self.prefix = 'Lala_Land'; endend
@mithran~ it is still not working.

@mike~ There a much easier way to attach a sprite to a cursor then that, but thank you, it did give me an idea. 
Not quite sure what is happening there. If implemented properly, prefix and suffix would never return nil values unless the default is also nil. The other issue I see is that you will run into stack errors because you are able to call an unlimited amount of methods before actually resolving (eg., $data_weapons[a].name returns $data_weapons.name + $data_weapons[a].name + $data_weapons[c].name, which each return more and so on even if not directly recursive (and they will be directly recursive if you have a default that points to an actual item).

This will probably do what you want:

Code:
class RPG::Weapon < RPG::EquipItem    alias name_base name  def name    n = name_base.dup    n = $data_weapons[prefix].name_base + " " + n if $data_weapons[prefix]    n += " " + $data_weapons[suffix].name_base if $data_weapons[suffix]    n  end  alias price_base price  def price    p = price_base    p += $data_weapons[prefix].price if $data_weapons[prefix]    p += $data_weapons[suffix].price if $data_weapons[suffix]    p  end   alias params_base params  def params    params = params_base.dup    8.times {  |i| params[i] += $data_weapons[prefix].params_base[i] } if $data_weapons[prefix]    8.times {  |i| params[i] += $data_weapons[suffix].params_base[i] } if $data_weapons[suffix]    return params  end    def prefix    @prefix ||= 0  end    def suffix    @suffix ||= 0  end end

Original thread: https://forums.rpgmakerweb.com/threads/accesing-a-weapon-in-rpg-weapons.23207/