$game_variables bonding

stevo777

Warper
Member
Joined
Oct 19, 2015
Messages
3
Reaction score
0
First Language
English
Primarily Uses
OK, I don't know what is happening. Even with a new project, after copying an array with another array they bind together and cant modify one without the other being effected. 

  def ea_test  

    test = [0, 0, 0]

    test2 = [0, 0, 0]

    test2 = test #<<< Problem?

    test[0] += 1

    test[1] += 1

    test2[2] += 1

    msgbox test, test2

  end

 

Test2 shouldn't be [0, 0, 0].
 

Shaz

Veteran
Veteran
Joined
Mar 2, 2012
Messages
38,255
Reaction score
11,860
First Language
English
Primarily Uses
RMMV
I've moved this thread to RGSSx Script Support. Please be sure to post your threads in the correct forum next time. Thank you.


test2 = test.clone
 

Sixth

Veteran
Veteran
Joined
Jul 4, 2014
Messages
2,136
Reaction score
805
First Language
Hungarian
Primarily Uses
RMVXA
Yeah, arrays and hashes needs to be cloned or else whenever you modify one variable storing them, the other will be modified too.


Note that if you got nested arrays or hashes, simply cloning them won't help, because the arrays one or more level inside will still be bound together.


For those, you can use:

Code:
varname2 = Marshal.load(Marshal.dump(varname1))
 

stevo777

Warper
Member
Joined
Oct 19, 2015
Messages
3
Reaction score
0
First Language
English
Primarily Uses
OK that is soooo helpful. I'm teaching myself rgss and ruby. 

Thanks for moving the post Shaz, I see where I should have posted now.
 

stevo777

Warper
Member
Joined
Oct 19, 2015
Messages
3
Reaction score
0
First Language
English
Primarily Uses
There isnt anything else that gets bond like that is there? Something I should watch out for? Can events be bound like that?
 

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

Latest Threads

Latest Posts

Latest Profile Posts

Stuffing three people in a room and thinking they'll study together is so wrong XD I don't get how people do group studies. We just end up gossiping about everything except studying lol
Been parallax mapping nigh all night. :D
2nd character based on the same random inputs
the coffee shop i go to for open mic every friday only has 2 more open mics left and then they're done :'( i'm gonna die!
Everytime someone says Intel "kabylake" I hear either baby lake or ladycake.

Forum statistics

Threads
93,385
Messages
911,880
Members
122,887
Latest member
Commander
Top