Copying Issues

Rinobi

Veteran
Veteran
Joined
Mar 24, 2014
Messages
579
Reaction score
219
First Language
English
Primarily Uses
RMVXA
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?
 
Last edited by a moderator:

taarna23

Marshmallow Princess
Global Mod
Joined
Jul 20, 2012
Messages
2,402
Reaction score
4,969
First Language
English
Primarily Uses
RMMZ
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]
 

Rinobi

Veteran
Veteran
Joined
Mar 24, 2014
Messages
579
Reaction score
219
First Language
English
Primarily Uses
RMVXA
@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>]
 
Last edited by a moderator:

Rinobi

Veteran
Veteran
Joined
Mar 24, 2014
Messages
579
Reaction score
219
First Language
English
Primarily Uses
RMVXA
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))
 
Last edited by a moderator:

Users Who Are Viewing This Thread (Users: 0, Guests: 1)

Latest Threads

Latest Profile Posts

Frostorm wrote on Featherbrain's profile.
Hey, so what species are your raptors? Any of these?
... so here's my main characters running around inside "Headspace", a place people use as a safe place away from anxious/panic related thinking.
Stream will be live shortly! I will be doing some music tonight! Feel free to drop by!
Made transition effects for going inside or outside using zoom, pixi filter, and a shutter effect
I have gathered enough feedback from a few selected people. But it is still available if you want to sign up https://forums.rpgmakerweb.com/index.php?threads/looking-for-testers-a-closed-tech-demo.130774/

Forum statistics

Threads
105,992
Messages
1,018,189
Members
137,771
Latest member
evoque
Top