$game_variables bonding

Discussion in 'RGSSx Script Support' started by stevo777, Oct 19, 2015.

    Tags:
  1. stevo777

    stevo777 Warper Member

    Messages:
    3
    Likes Received:
    0
    First Language:
    English
    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].
     
    #1
  2. Shaz

    Shaz Veteran Veteran

    Messages:
    37,184
    Likes Received:
    11,053
    Location:
    Australia
    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
     
    #2
  3. Sixth

    Sixth Veteran Veteran

    Messages:
    2,121
    Likes Received:
    789
    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))
     
    #3
  4. stevo777

    stevo777 Warper Member

    Messages:
    3
    Likes Received:
    0
    First Language:
    English
    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.
     
    #4
  5. stevo777

    stevo777 Warper Member

    Messages:
    3
    Likes Received:
    0
    First Language:
    English
    There isnt anything else that gets bond like that is there? Something I should watch out for? Can events be bound like that?
     
    #5

Share This Page