- Home /
Confused about copying Lists!
Hello everyone, I'm enjoying Unity but Im having trouble with Lists in C#, and from googling, I think my approach is all wrong.
In my game, when the player dies they leave behind a trail of waypoints. When they respawn, a new AI character also spawns and follows the waypoints. I've got this working fine by storing the waypoints in a List and using a Navmesh agent.
I'm having problems when the player dies more than once and having different AI's follow different sets of waypoints. To create a new set of waypoints, I tried clearing the List just after I've instantiated the character and sent it the waypoints its should follow. The problem with this I believe is that because Lists are a reference type, the AI's List to follow also gets cleared.
How can I make a copy of this List which won't also get cleared?
The only other way I could think of was to make a List of Lists that don't get cleared, but I can't seem to find much about this which suggests there's a better way.
private List<List> listOfLists = new List<List>();
doesn't seem to work, I can post more code if needed.
Any help at all would be awesome. - Thanks
Can you post more code? I've read this 3x now and don't quite understand what you're doing :)
Well thanks for trying Drakestar, haha.
Ok so I declare my lists of waypoints for my player and AI in their respective classes:
private List playerWays = new List(); private List aiWays = new List();
During the gameplay waypoint objects get added to playerWays. When the player dies I want to copy the contents into aiWays. I do this in my player class first by :
instance.SetWaypoints(playerWays);
and then in my Ai class:
public void SetWaypoints(List waysIn) { aiWays = waysIn; }
This is all fine until I clear playerWays to make a new set of waypoints for the players next turn:
playerWays.Clear();
This also clears aiWays, meaning that my Ai has no waypoints to follow.
So how can I give my Ai a hard copy of the list with out it being affected by what I do to the player's list?
Cheers
Answer by Drakestar · May 14, 2012 at 03:00 PM
When you do this
public void SetWaypoints(List waysIn) { aiWays = waysIn; }
you're indeed simply creating another reference to the list that waysIn already points to. Since both references refer to the same internal datastructure, clearing out that list will result in two references that both point to the cleared list. You need to do a shallow copy of the list instead (one that copies all the references - as opposed to a deep copy, which would create actual value copies of each element).
The easiest way to do a shallow copy is to iterate over the old list and add its references to the new list:
List<WayPoint> aiWays = new List<WayPoint>();
for (int i = 0; i < waysIn.Count; i++)
{
aiWays.Add(waysIn[i]);
}
There's a lot of ways to skin this cat, as you can see if you google "c# list shallow copy". But this might be enough for your purposes.
Yes this is just what I need, many thanks. I would have thought List would have a built in function for copying ins$$anonymous$$d of manually filling in each element. P.s don't you mean deep copy ins$$anonymous$$d of shallow copy?
The C# base object has 'protected Object $$anonymous$$emberwiseClone()' which creates a shallow copy, including lists. $$anonymous$$ost likely, it will work in this situation. I meant shallow copy, since I'm assu$$anonymous$$g that you don't actually want to duplicate the actual waypoints, just their references. A deep copy is more involved (and slower!) If you need that you should open a new question.