$game_variables bonding

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].
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
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))
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.
There isnt anything else that gets bond like that is there? Something I should watch out for? Can events be bound like that?

Original thread: https://forums.rpgmakerweb.com/threads/game_variables-bonding.46126/