Wayback Machinekoobas.hobune.stream
May JUN Jul
Previous capture 13 Next capture
2021 2022 2023
1 capture
13 Jun 22 - 13 Jun 22
sparklines
Close Help
  • Products
  • Solutions
  • Made with Unity
  • Learning
  • Support & Services
  • Community
  • Asset Store
  • Get Unity

UNITY ACCOUNT

You need a Unity Account to shop in the Online and Asset Stores, participate in the Unity Community and manage your license portfolio. Login Create account
  • Blog
  • Forums
  • Answers
  • Evangelists
  • User Groups
  • Beta Program
  • Advisory Panel

Navigation

  • Home
  • Products
  • Solutions
  • Made with Unity
  • Learning
  • Support & Services
  • Community
    • Blog
    • Forums
    • Answers
    • Evangelists
    • User Groups
    • Beta Program
    • Advisory Panel

Unity account

You need a Unity Account to shop in the Online and Asset Stores, participate in the Unity Community and manage your license portfolio. Login Create account

Language

  • Chinese
  • Spanish
  • Japanese
  • Korean
  • Portuguese
  • Ask a question
  • Spaces
    • Default
    • Help Room
    • META
    • Moderators
    • Topics
    • Questions
    • Users
    • Badges
  • Home /
avatar image
1
Question by RideTheT · May 14, 2012 at 01:33 PM · c#ailistwaypoint

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

Comment
Add comment · Show 2
10 |3000 characters needed characters left characters exceeded
▼
  • Viewable by all users
  • Viewable by moderators
  • Viewable by moderators and the original poster
  • Advanced visibility
Viewable by all users
avatar image Drakestar · May 14, 2012 at 02:02 PM 0
Share

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

avatar image RideTheT · May 14, 2012 at 02:45 PM 0
Share

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

1 Reply

· Add your reply
  • Sort: 
avatar image
2
Best Answer

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.

Comment
Add comment · Show 3 · Share
10 |3000 characters needed characters left characters exceeded
▼
  • Viewable by all users
  • Viewable by moderators
  • Viewable by moderators and the original poster
  • Advanced visibility
Viewable by all users
avatar image RideTheT · May 14, 2012 at 04:04 PM 0
Share

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?

avatar image Drakestar · May 14, 2012 at 04:19 PM 0
Share

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.

avatar image RideTheT · May 14, 2012 at 04:37 PM 0
Share

Ah cool, think I understand now. Thanks again.

Your answer

Hint: You can notify a user about this post by typing @username

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

Answers Answers and Comments

6 People are following this question.

avatar image avatar image avatar image avatar image avatar image avatar image

Related Questions

Movement around a huge object by avoiding obstacles 1 Answer

RTS dynamic formation width and length 0 Answers

Fast way to find a specific myClass across many List? 1 Answer

Make player follow waypoints, but still control it's X axis 3 Answers

AI Waypoint help or suggestions!? 2 Answers


Enterprise
Social Q&A

Social
Subscribe on YouTube social-youtube Follow on LinkedIn social-linkedin Follow on Twitter social-twitter Follow on Facebook social-facebook Follow on Instagram social-instagram

Footer

  • Purchase
    • Products
    • Subscription
    • Asset Store
    • Unity Gear
    • Resellers
  • Education
    • Students
    • Educators
    • Certification
    • Learn
    • Center of Excellence
  • Download
    • Unity
    • Beta Program
  • Unity Labs
    • Labs
    • Publications
  • Resources
    • Learn platform
    • Community
    • Documentation
    • Unity QA
    • FAQ
    • Services Status
    • Connect
  • About Unity
    • About Us
    • Blog
    • Events
    • Careers
    • Contact
    • Press
    • Partners
    • Affiliates
    • Security
Copyright © 2020 Unity Technologies
  • Legal
  • Privacy Policy
  • Cookies
  • Do Not Sell My Personal Information
  • Cookies Settings
"Unity", Unity logos, and other Unity trademarks are trademarks or registered trademarks of Unity Technologies or its affiliates in the U.S. and elsewhere (more info here). Other names or brands are trademarks of their respective owners.
  • Anonymous
  • Sign in
  • Create
  • Ask a question
  • Spaces
  • Default
  • Help Room
  • META
  • Moderators
  • Explore
  • Topics
  • Questions
  • Users
  • Badges