Search Unity

Resolved How to duplicate a list in a new one?

Discussion in 'Visual Scripting' started by Marou1, Oct 21, 2022.

  1. Marou1

    Marou1

    Joined:
    Dec 13, 2021
    Posts:
    161
    Hi,

    When I do this:
    Lists.png

    I don't get 2 lists, but 2 variables pointing on the same list. Modifying one variable will modify the second one.

    How do I get 2 different lists, so I can modify the first list without modifying the second list?

    Do I have to use For Each Loop necessarily ?
    If yes, then do I have to copy all the fields of each item of the list A to all the field of each item of the list B?

    Thanks!
     
  2. REDACT3D_

    REDACT3D_

    Joined:
    Nov 8, 2020
    Posts:
    222
    im sorry bud i don't understand
    i kinda do but..

    you just want to change one list but not the other depending on a value?
    just don't reference it then? i dont understand sorry man.

    clearly you're trying to sort an inventory
    usually i'd use the 'for each node'
     
  3. Marou1

    Marou1

    Joined:
    Dec 13, 2021
    Posts:
    161
    Let me explain it differently:

    Usually, when I do:

    Set var a = 5
    Set var b = var a

    Then I change only var a:
    Set var a = 10

    Result is:
    var a = 10
    var b = 5

    But in the case of list, the result is:
    var a = 10
    var b = 10.

    My vague understanding is that for some types, like lists in this case, the instruction Set does not put the contenant of var a in var b, but make the var b and var a point on the same data in memory. Thus, var a, and var b will always be the same. That is not what I want.

    I'd like to have no link between var a and var b, So once I've copied var a in var b, modifying var a won't affect var b and vice versa.
     
    Last edited: Oct 21, 2022
  4. Marou1

    Marou1

    Joined:
    Dec 13, 2021
    Posts:
    161
    How do you do that?
     
  5. REDACT3D_

    REDACT3D_

    Joined:
    Nov 8, 2020
    Posts:
    222
    seems like an order of operations thing.

    if you're doing

    float a = 5;
    float b = a;
    // a would be 5 here

    //then
    b = 10;

    // b is 10 and a is 5 here
    1.png 2.png 3.png
     
    Last edited: Oct 21, 2022
  6. Marou1

    Marou1

    Joined:
    Dec 13, 2021
    Posts:
    161
    No it's not an order of operation issue.

    Both variables are the same, changing one will change the other one.

    The Set instruction is not assigning values, but an object reference. So both variables reference the same object.
     
  7. REDACT3D_

    REDACT3D_

    Joined:
    Nov 8, 2020
    Posts:
    222
    like this is the reference of the list item
    if you don't directly reference a list, it can't change right
    1.png

    1.png
    2.PNG
    3.PNG
     
    Last edited: Oct 21, 2022
  8. Marou1

    Marou1

    Joined:
    Dec 13, 2021
    Posts:
    161
    I have this list A:
    Lists.png

    I want to create a copy of this list in real time to create list B:

    If I use Set List B = List A
    And I modify list B, it modifies also list A.
    However, what I want is to modify list B content without modifying list A content. how would you do that?
     
  9. Marou1

    Marou1

    Joined:
    Dec 13, 2021
    Posts:
    161
    And doing this does not solve the issue:
    Lists.png

    Modifying the new list will still modify the initial list...
     
  10. Marou1

    Marou1

    Joined:
    Dec 13, 2021
    Posts:
    161
    In your example, the 2 lists already exist, so they are independent.
    In my case the second list does not exit. It is created in real time as a copy of the first one.

    And when it is a copy, they both have the same reference object, so modifying the first will modify the second.
     
  11. REDACT3D_

    REDACT3D_

    Joined:
    Nov 8, 2020
    Posts:
    222
    beauty

    okay what you would want to do is generate a new temp list from that list like this.
    you can see how it copies the data on runtime and adds to an empty list
    1.png

    2.PNG

    you could then save that runtime list in another instance of the list
    3.PNG


    after it runs we can see the new data that was saved
    4.PNG
     
    Last edited: Oct 21, 2022
  12. Marou1

    Marou1

    Joined:
    Dec 13, 2021
    Posts:
    161
    The saving part is not required. I need to modify Temp list without modifying the OG list.
    How would you do that?

    Did you try to modify Temp list and see what happened to OG List?
     
    Last edited: Oct 21, 2022
    REDACT3D_ likes this.
  13. REDACT3D_

    REDACT3D_

    Joined:
    Nov 8, 2020
    Posts:
    222
    Yes, taking the order of operations into consideration, we just hook this between the two to change the value before it's saved.

    the result is an unchanged OG list
    while the Templist is updated with the new string item

    6.PNG

    5.PNG

    1.png
    3.PNG



    because we're generating an entirely new list and referencing it directly, only that list can change values while the OG is still OG.
     
    Last edited: Oct 21, 2022
  14. Marou1

    Marou1

    Joined:
    Dec 13, 2021
    Posts:
    161
    I set this script. List b is empty before On Start is triggered.
    1.png


    On start, the copy is made:
    2.png

    So far so good.

    Now I connect the modification part applied only to List b:
    3.png

    On start I get this:
    4.png


    Why the list a is modified?!!!


    BTW, this works:
    5.png


    But why?!
     
  15. REDACT3D_

    REDACT3D_

    Joined:
    Nov 8, 2020
    Posts:
    222
    because you're setting it like that once the loop is done Capture.PNG
     
  16. Marou1

    Marou1

    Joined:
    Dec 13, 2021
    Posts:
    161
    No but i am asking why modifying list b modifies list a.

    I am talking about my 3rd screenshot.
     
  17. cferguson91

    cferguson91

    Joined:
    Feb 1, 2014
    Posts:
    1
    In case anyone else ends up here because of the same issue in their code...
    If you are declaring a copy of a list like this:

    List<T> ListB = ListA;

    Then performing an operation on ListB, such as ListB.Reverse(), will also apply the operation to ListA. If you do not desire this behavior, then you must declare ListB like this:

    List<T> ListB = new List<T>(ListA);

    Now, you can perform operations on ListB, such as ListB.Reverse(), and ListA will remain unaffected.
     
    ssarnov likes this.