Copying Issues

I was having an issue with getting a script to work properly and I found some odd behavior.


Basically, I just want to copy the effects from an item object to a skill object.
The problem is, this seems to only work once before the new value just sticks and refuses to change.


#==============================================================================
# ** Alchemy Module (WIP)
#==============================================================================
module RINOBI module Alchemy
def self.copy_item_data(skill_id, item_id)
3.times do
print "Inital Effects: #{$data_skills[skill_id].effects}\n\n"
effect_array = Array.new
$data_skills[skill_id].effects = Array.new
print "Reset Effects: #{$data_skills[skill_id].effects}\n\n"
$data_items[item_id].effects.each {|effect| effect_array.push(effect)}
effect_array.each {|effect| $data_skills[skill_id].effects.push(effect)}
print "New Effects Value: #{$data_skills[skill_id].effects}\n\n"
$data_skills[skill_id].effects[0].data_id = 102
end #--
end # copy_item_data
end # Alchemy Module


The skill I'm testing testing doesn't have any effects by default, and the data_id of the item is 100.


I would expect to see New Effects Value always print a value of 100, but this isn't the case. After the first time around, only a value of 102 is printed out, and I don't know why.


Here are the results from the console.
 


Inital Effects: []

Reset Effects: []

New Effects Value: [#<RPG::UsableItem::Effect:0x6b69850 @code=21, @data_id=100, @value1=1.0, @value2=0.0>]



Inital Effects: [#<RPG::UsableItem::Effect:0x6b69850 @code=21, @data_id=102, @value1=1.0, @value2=0.0>]

Reset Effects: []

New Effects Value: [#<RPG::UsableItem::Effect:0x6b69850 @code=21, @data_id=102, @value1=1.0, @value2=0.0>]



Inital Effects: [#<RPG::UsableItem::Effect:0x6b69850 @code=21, @data_id=102, @value1=1.0, @value2=0.0>]

Reset Effects: []

New Effects Value: [#<RPG::UsableItem::Effect:0x6b69850 @code=21, @data_id=102, @value1=1.0, @value2=0.0>]




It doesn't seem to matter how I go about copying the effects either, the above example was simply the latest thing I tried.


$data_skills[skill_id].effects = $data_items[item_id].effects.dup produces the same results for example.


How do I get around this?
Okay, Ruby makes my brain hurt, but aren't you SETTING it to 102?

Code:
[COLOR=rgb(0,0,0)]$data_skills[skill_id].effects[0].data_id = 102[/COLOR]
@taarna23


Yes, but note the order at which things occur.


New Effects Value is printed BEFORE a new data_id is set.


Also, the data_id of $data_items[item_id].effects[0] is 100.


I'll attempt to simply it here...


#==============================================================================
# ** Alchemy Module (WIP)
#==============================================================================
module RINOBI module Alchemy
def self.copy_item_data(skill_id, item_id)
print "The Item Being Copied: #{$data_items[item_id].effects}\n\n\n"
3.times do
print "Inital Effects: #{$data_skills[skill_id].effects}\n\n"
$data_skills[skill_id].effects = Array.new # Empty Array.
print "Reset Effects: #{$data_skills[skill_id].effects}\n\n"
$data_skills[skill_id].effects = $data_items[item_id].effects.dup
print "New Effects Value: #{$data_skills[skill_id].effects}\n\n"
$data_skills[skill_id].effects[0].data_id = 102
end #--
end # copy_item_data
end # Alchemy Module




Results

Code:
The Item Being Copied: [#<RPG::UsableItem::Effect:0x6c56128 @code=21, @data_id=100, @value1=1.0, @value2=0.0>]

  

Inital Effects: []

Reset Effects: []

New Effects Value: [#<RPG::UsableItem::Effect:0x6c56128 @code=21, @data_id=100, @value1=1.0, @value2=0.0>]
  
  
  

Inital Effects: [#<RPG::UsableItem::Effect:0x6c56128 @code=21, @data_id=102, @value1=1.0, @value2=0.0>]

Reset Effects: []

New Effects Value: [#<RPG::UsableItem::Effect:0x6c56128 @code=21, @data_id=102, @value1=1.0, @value2=0.0>]
  
  
  

Inital Effects: [#<RPG::UsableItem::Effect:0x6c56128 @code=21, @data_id=102, @value1=1.0, @value2=0.0>]

Reset Effects: []

New Effects Value: [#<RPG::UsableItem::Effect:0x6c56128 @code=21, @data_id=102, @value1=1.0, @value2=0.0>]
I figured out what was happening.


It turns out that the item was being referenced and copied at the same time.


So when I did this:


$data_skills[skill_id].effects[0].data_id = 102


It also changed the data_id of $data_items[item_id].effects[0]


So when it was time to copy the item again, the new value of 102 was being set instead of 100.


I was under the impression that .dup or .clone would prevent that from happening... I didn't, so I'll look into that later.
In fact, despite all of the different approaches I took towards copying the value I needed, the object was still being referenced somehow.


How annoying stubborn. >_>


Anyway, I did manage to get around it.

Code:
$data_skills[skill_id].effects = Marshal.load(Marshal.dump($data_items[item_id].effects))

Original thread: https://forums.rpgmakerweb.com/threads/copying-issues.66302/