x


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

more ▼

asked May 14 '12 at 01:33 PM

RideTheT gravatar image

RideTheT
2 1 1 1

Can you post more code? I've read this 3x now and don't quite understand what you're doing :)

May 14 '12 at 02:02 PM Drakestar

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

May 14 '12 at 02:45 PM RideTheT
(comments are locked)
10|3000 characters needed characters left

1 answer: sort voted first

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.

more ▼

answered May 14 '12 at 03:00 PM

Drakestar gravatar image

Drakestar
916 6 7 14

Yes this is just what I need, many thanks. I would have thought List would have a built in function for copying instead of manually filling in each element. P.s don't you mean deep copy instead of shallow copy?

May 14 '12 at 04:04 PM RideTheT

The C# base object has 'protected Object MemberwiseClone()' which creates a shallow copy, including lists. Most likely, it will work in this situation. I meant shallow copy, since I'm assuming 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.

May 14 '12 at 04:19 PM Drakestar

Ah cool, think I understand now. Thanks again.

May 14 '12 at 04:37 PM RideTheT
(comments are locked)
10|3000 characters needed characters left
Your answer
toggle preview:

Up to 2 attachments (including images) can be used with a maximum of 524.3 kB each and 1.0 MB total.

Follow this question

By Email:

Once you sign in you will be able to subscribe for any updates here

By RSS:

Answers

Answers and Comments

Topics:

x5084
x4165
x960
x356
x170

asked: May 14 '12 at 01:33 PM

Seen: 896 times

Last Updated: May 14 '12 at 04:37 PM