$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
40,100
Reaction score
13,705
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,162
Reaction score
822
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

I should realize that error was produced by a outdated version of MZ so that's why it pop up like that
Ami
i can't wait to drink some ice after struggling with my illness in 9 days. 9 days is really bad for me,i can't focus with my shop and even can't do something with my project
How many hours have you got in mz so far?

A bit of a "sparkle" update to the lower portion of the world map. :LZSexcite:
attack on titan final season is airing tomorrow, I'm excited and scared at the same time!

Forum statistics

Threads
105,882
Messages
1,017,231
Members
137,607
Latest member
Maddo
Top