Search Unity

  1. Welcome to the Unity Forums! Please take the time to read our Code of Conduct to familiarize yourself with the forum rules and how to post constructively.
  2. We have updated the language to the Editor Terms based on feedback from our employees and community. Learn more.
    Dismiss Notice
  3. Join us on November 16th, 2023, between 1 pm and 9 pm CET for Ask the Experts Online on Discord and on Unity Discussions.
    Dismiss Notice

List problem

Discussion in 'Scripting' started by JPCannon, Dec 3, 2014.

  1. JPCannon

    JPCannon

    Joined:
    Sep 25, 2013
    Posts:
    37
    Hi I have real strange problem. I have two Lists. One in SaveAndLoad and one in PlayerData. Both get player skills list. In constructor of SaveAndLoad I equates One List to this from PlayerData. Everythink works ok but when i try Add some skill to my PlayerData List script adds it to my List from SaveAndLoad too. I dont now how it can do this. When i equates it my List from one and second class are the same? :p

    Debug.Log("TestSkilla777: " + SaveAndLoad._myActualSkills999.Count);
    PlayerData.myActualSkills.Add((SkillType)actualSelectedSkill);
    Debug.Log("TestSkilla666: " + SaveAndLoad._myActualSkills999.Count);

    First SaveAndLoad._myActualSkills999.Count == 0
    Second SaveAndLoad._myActualSkills999.Count == 1

    How? :p Pls for any help
     
  2. Dantus

    Dantus

    Joined:
    Oct 21, 2009
    Posts:
    5,667
    Why are you creating two lists if you just need one? As you are creating the list, you can pass or query it from the other place where you need it. Like that you only have one list and you have a reference to it from two places.

    Please use code tags even for such simple snippets:
    http://forum.unity3d.com/threads/using-code-tags-properly.143875/
     
  3. Ruekaka

    Ruekaka

    Joined:
    Sep 12, 2014
    Posts:
    119
  4. JPCannon

    JPCannon

    Joined:
    Sep 25, 2013
    Posts:
    37
    I must use two lists couse i use it to check if any changes are making in PlayerData. _myAcutalSkills are temporary List where I have old values of list from PlayerData

    So how to do that to only equates values from one list to another not creating same point for both? Or it is another way to simple check that my value in List have change?
     
    Last edited: Dec 3, 2014
  5. Dantus

    Dantus

    Joined:
    Oct 21, 2009
    Posts:
    5,667
    It is pretty simple. Whenever you add a new value, do it in both lists. In order to have a clean solution, create a method just for that, or even better, create a class that deals with both of those lists and is responsible for it.
     
  6. JPCannon

    JPCannon

    Joined:
    Sep 25, 2013
    Posts:
    37
    But I must update _myActualSkills only after save. I create SaveAndLoad class with one method which are checking if my temp value of list is the same as new version of list with skills. If yes game is saving progress and my temporary values of _myActualSkills is updated. Only then. So how to construct this in another, good working way. I dont want to creat save for gold, level, equipment, skills etc in another methods. Whant to have all in one.


    I simply want to assign value from one list to another. Is some way to do this but without create same point to both lists?
     
  7. Dantus

    Dantus

    Joined:
    Oct 21, 2009
    Posts:
    5,667
    No. At the end you have to do that. But you could create a new method that internally does that, but allows you do only call that method if needed.
     
  8. Ruekaka

    Ruekaka

    Joined:
    Sep 12, 2014
    Posts:
    119
    Why not create one list and assign this list to the second instance. If you do so they point to the same object and changes on one list reflects in the second one, too (because they are the same).

    The following code

    Code (CSharp):
    1. List<string> sl1;
    2. List<string> sl2;
    3.  
    4. sl1 = new List<string>();
    5. sl2 = sl1;
    6.  
    7. sl1.Add("Test 1, L1");
    8. sl1.Add("Test 2, L1");
    9. sl1.Add("Test 3, L1");
    10.  
    11. sl2.Add("Test 1, L2");
    12. sl2.Add("Test 2, L2");
    13. sl2.Add("Test 3, L2");
    14.  
    15. AddLogMsg("");
    16. AddLogMsg("Content of list 1:");
    17. for (int i = 0; i < sl1.Count; i++)
    18. {
    19.     AddLogMsg(sl1[i]);
    20. }
    21.  
    22. AddLogMsg("");
    23. AddLogMsg("Content of list 2:");
    24. for (int i = 0; i < sl2.Count; i++)
    25. {
    26.     AddLogMsg(sl2[i]);
    27. }
    creates the following output:

    [03.12.2014 13:06:18] Content of list 1:
    [03.12.2014 13:06:18] Test 1, L1
    [03.12.2014 13:06:18] Test 2, L1
    [03.12.2014 13:06:18] Test 3, L1
    [03.12.2014 13:06:18] Test 1, L2
    [03.12.2014 13:06:18] Test 2, L2
    [03.12.2014 13:06:18] Test 3, L2

    [03.12.2014 13:06:18] Content of list 2:
    [03.12.2014 13:06:18] Test 1, L1
    [03.12.2014 13:06:18] Test 2, L1
    [03.12.2014 13:06:18] Test 3, L1
    [03.12.2014 13:06:18] Test 1, L2
    [03.12.2014 13:06:18] Test 2, L2
    [03.12.2014 13:06:18] Test 3, L2


    Maybe I misunderstood what you try to achieve, but as you can see both lists contains the same values.
     
  9. JPCannon

    JPCannon

    Joined:
    Sep 25, 2013
    Posts:
    37
    Ok Topic to close. I simply change equates List to List on List.Count to simple int ;) Working the same and dont create any problems.