New instance of a weapon

$data_weapons[$data_weapons.size] = $data_weapons[id].dup

$data_weapons[$data_weapons.size - 1].id = $data_weapons.size - 1

This is how I am trying to create a new weapon but it is still affecting the original.

Am I missing something?
I do not have rpg maker in front of me but,

try this:

new_weapon = $data_weapons[id].dup

new_weapon.id = $data_weapons.size

$data_weapons[$data_weapons.size] = new_weapon

or this(appending to the array):

new_weapon = $data_weapons[id].dup

new_weapon.id = $data_weapons.size

$data_weapons << new_weapon

hope this helps 😀
Thank you AwesomeCool I see what I was doing wrong there. It fixed the instance issue. kinda.

Now I have another problem though, it is doing the same thing for the params. They are all being affected.

Here are the methods involved.

I figured it out thanks again

Here is the correct code

Code:
  #-----------------------------------------------------------------------------  # * Create Params  #-----------------------------------------------------------------------------  def create_params(base, prefix, suffix)    params = base.params.dup    8.times {  |i| params[i] += $data_weapons[prefix].params[i] } unless prefix == 0    8.times {  |i| params[i] += $data_weapons[suffix].params[i] } unless suffix == 0    return params  end   #-----------------------------------------------------------------------------  # * Create Weapon  #-----------------------------------------------------------------------------  def create_weapon(id)    base = $data_weapons[id].dup    base.id = $data_weapons.size    prefix = create_prefix(base)    suffix = create_suffix(base)    base.name = create_name(base, prefix, suffix)    base.params = create_params(base, prefix, suffix)    $data_weapons.push base    $game_party.gain_item($data_weapons[$data_weapons.size - 1], 1, true)  end 
here is the code I came up with (I didn't test)

def create_params(base, prefix, suffix) pre = $data_weapons[prefix].dup unless prefix == 0 suf = $data_weapons[suffix].dup unless suffix == 0 unless prefix == 0 8.times { |i| base.params += pre.params } end unless suffix == 0 8.times { |i| base.params += suf.params } end return base end #----------------------------------------------------------------------------- # * Create Weapon #----------------------------------------------------------------------------- def create_weapon(id) prefix = new_weap(id) prefix = create_prefix(prefix) suffix = new_weap(id) suffix = create_suffix(suffix) base = new_weap(id) base = create_params(base, prefix, suffix)#~ base = create_name(base, prefix, suffix) $data_weapons.push(base) $game_party.gain_item($data_weapons[$data_weapons.size - 1], 1, true) end def new_weap(id) weap = $data_weapons[id].dup weap.id = $data_weapons.size return weap endNote that objects (like weapons) are passed by reference.  So they ALL point to base in your code (make one change then they all change).

 If you do not know what a reference variable is, it is a object that when set equal to something, it contains a code(pointer) that points to the specified data instead of the data itself.

Code:
x = object #object starts equaling to 1y = xx.add(5) #x now equals 6print (y.print) #y will print 6#it will be this way unless you clone/dup for EVERY instance of the object
I figured it out already (and a little shorter too.) Thank you though.

Yes, I did know that about the reference variable I just forgot the .dup here

params = base.params.dupBTW This won't work, I can tell just looking at it.

prefix = new_weap(id)prefix = create_prefix(prefix)suffix = new_weap(id)suffix = create_suffix(suffix)You are calling this twice

new_weap(id)which is creating a new weapon every time it is called.

Also you do not need the () brackets when using push they're optional

I am having problems combining the features though.

Here is the method I'm using

I was looking to far into it. I didn't need to add them together Rpg Maker does that for me, here is the correct way to add the features.

  #-----------------------------------------------------------------------------  # * Create Features  #-----------------------------------------------------------------------------  def create_feats(base, prefix, suffix)    feats = base.features.dup    pre = $data_weapons[prefix].features.dup unless prefix == 0    suf = $data_weapons[suffix].features.dup unless suffix == 0    pre.each { |pf| feats.push pf } unless prefix == 0    suf.each { |sf| feats.push sf } unless suffix == 0    return feats  endThe method above it is checking if the codes match. if so it adds the values. What it does not currently do is add the new code to the feats array if the code does not exist.

How can I do this correctly?
I didn't know that about push XD

Learn new things every day XD
"Learn new things every day XD" Me too. Thanks again for the help.
I'm not sure what you mean by the correct way to add features.
I probably would have just dumped and loaded it to be sure that I wasn't keeping any references to the same objects as the original, like this:

new_weapon = Marshal.load(Marshal.dump($data_weapons[id]))Be cautious about just dupping* an array. It won't create copies of the objects contained by the array.

Also, just in case it is helpful, I created an Instance Items Base script a little while ago. I never really did anything with it and I do not remember if it is any good, but if it is you can either use it as a base or take some ideas that you like from it in coding your own, as you wish.

As far as I know, base is not a keyword in RGSS3. I am told that it is in C# though.

*dup probably isn't a verb, but let's just pretend.
@M.A~ LOL. Thank you very much. I am done with the script now but I will take a look at it.

"As far as I know, base is not a keyword in RGSS3. I am told that it is in C# though."

I knew about the C# that's another reason I asked originally, and you are correct about Ruby I looked it up last night just to be sure. Here is a link to all the keywords Ruby uses.

@Tsuki~ I was tired and trying to add each code's values together. In the end I realized it was extremely unnecessary, seeing as how you can added multiples of the same feature and it adds them together automatically by default.
You should avoid duping mutable objects unless you set some rules.

The purpose of instance items is exactly to change them. Or at least, that's what I think they are for anyways.

By providing a deep copy, you can guarantee that if you grab an instance, it is its own entity and has nothing to do with any other object in the game.

If you're just attaching things to your instances, like a prefix or a suffix, that's ok.

However, once you modify any of the values, you're setting yourself up for bugs.

For example, let's say you have your Hand Ax and you duped its name. Strings are expensive; an empty string takes up 10 bytes when serialized and an extra byte for each ASCII character! Descriptions are long and therefore can get pretty expensive once you have 100 copies of the weapon.

Now you go and take your second Hand Ax and modify its name to Hand Ax+1.

You've suddenly changed the first ax's name as well to Hand Ax+1 because they both point to the same string.

This is not a problem if, as mentioned, you just "attach" things to your instance. Or stick with integers and symbols for ID purposes.

For example, instead of changing the second axe's name, you instead define a method that will build the name whenever it's requested

def get_name return "%s +%d" %[@name, @level]endYou do not modify the name. You simply read it and return a new value.But you will need to make sure that this is consistently followed by anyone that wishes to write an add-on using your script as a base.

The trade-off here is time vs space. You save memory and storage space (in the case of save files) by duping references, at the expense of having to generate the name at run-time everytime you need it. It is up to you which is more important.
Would you suggest that I do this in the base RPG modules.

like so

#==============================================================================# ** RPG::EquipItem#------------------------------------------------------------------------------#  A superclass of weapons and armor.#==============================================================================class RPG::EquipItem < RPG::BaseItem  #--------------------------------------------------------------------------  # * Public Instance Variables  #--------------------------------------------------------------------------  attr_accessor 😛refix  attr_accessor :suffix  #--------------------------------------------------------------------------  # * Aliasing Method: Initialize Proccessing  #--------------------------------------------------------------------------  alias :rpgeii416516161 :initialize  #--------------------------------------------------------------------------  # * Initialize Proccessing  #--------------------------------------------------------------------------  def initialize    super    @prefix = 0    @suffix = 0  end    endI have never used them before so I'm not to sure about the "Hidden Classes"
https://github.com/Hime-Works/Requests/issues/42#issuecomment-32239800


https://github.com/Hime-Works/Requests/issues/88


I would look at these topics.
Looking now. That's quite a bit of thumbing threw. Thanks for the read.

Edited~

Alright, First of thank you Tsukihime. That was a lot to ingest. I will really have to think about how I am going to work things before I do any rewriting.

Original thread: https://forums.rpgmakerweb.com/threads/new-instance-of-a-weapon.23085/