- Home /
Copying one list to another list
How do I take the contents of one list and copy that data into another list? I have a class which handles deck related things, such as building decks, loading files, etc. Now when I use Debug.Log here to list the contents of the lists, it works fine.
What I am having trouble with is taking the contents from the deck list, and placing it into a playerDeck/enemyDeck list. I don't know if more of my code is needed to see what is happening, if it is I can provide it. Also maybe it is important, maybe not, but the list is a list of gameObjects.
deck.cs
public void AddToDeck(string[] myDeck)
{
deck.Clear();
foreach(string line in myDeck)
{
if(line == "0")
{
WarriorCard battleChariot = new WarriorCard("Battle Chariot", 1, 12, 1);
deck.Add(battleChariot);
Debug.Log(battleChariot.Name);
Debug.Log(battleChariot.HP);
Debug.Log(battleChariot.DMG);
}
}
mainGame.cs
void BuildDeck(string file)
{
if(m_state == GameState.PlayerTurn)
{
deckInstance.Load(file);
deckInstance.Shuffle();
playerDeck = deckInstance.deck;
Debug.Log(playerDeck.battleChariot.Name);
/*deck is the list variable used within the deck class and I have a new instance within this class*/
}
else if(m_state == GameState.AITurn)
{
deckInstance.Load(file);
deckInstance.Shuffle();
aiDeck = deckInstance.deck;
}
}
are you asking to copy a regular array or a generic list? cause your title says 'list', but your code uses an array...
Answer by vexe · Mar 17, 2014 at 04:15 PM
How about System.Array.Copy?
If you want to copy a generic list, see CopyTo.
Or you could just:
var listCopy = new List<Stuff>(originalList);
Notice that just copies the list contents - if it was holding reference types, it would just copy the references over, it won't clone the items themselves.
i.e.
public class Enemy
{
public float damage;
}
var list = new List<Enemy>();
list.Add(new Enemy() { damage = 100 });
list.Add(new Enemy() { damage = 50 });
list.Add(new Enemy() { damage = 25 });
var copy = new List<Enemy>(list);
list[0].damage = 123;
print(copy[0].damage); // 123!
It doesn't seem to be passing the information for me. However Unity is throwing an error when I run the program which is "ArgumentOutOfRangeException: Argument is out of range.". However it still runs the program.
Could this problem be why I the information doesn't appear to be copying?
Firstly I don't think I included enough information in my initial question. $$anonymous$$aybe I'm wrong and it isn't needed but hopefully it'll help.
Within deck.cs I have created a list of cards as follows:
public List<System.Object> deck = new List<System.Object>();
Now this is done because a deck can contain two types of cards, each with there own class as they have different attributes. You can have a warrior card or an active card in the deck. They're added to the deck based on an id which is obtained from a external file.
So perhaps I'm wrong in thinking this, but maybe Array.Copy isn't going to work for this. Regardless I have put the following code in for copying the data:
System.Array.Copy(deckInstance.deck, playerDeck, 30);
Are you sure they're of the same length? they must be in order for the copying to go well.
if (deckInstance.deck.Length == playerDeck.Length)
System.Array.Copy(deckInstance.deck, playerDeck, playerDeck.Length);
Worst case scenario you write your own copy function:
public static void CopyArray<T>(T[] source, T[] dest)
{
if (source.Length != dest.Length)
throw new InvalidOperationException("[CopyArray] lengths don't match");
for(int i = 0; i < source.Length; i++)
dest[i] = source[i];
}
Another thing, don't use a List of System.Object
as that won't serialize. Ins$$anonymous$$d, create a Serializable
abstract Card
class, and inherit from it making sure you mock every child with System.Serializable
[System.Serializable]
public abstract class Card
{
// common and abstract stuff...
}
public class WariorCard : Card
{
// warior specific stuff...
}
and then have List < Card >
as your deck...
If there's nothing really special about your other cards other than being different in name (WarriorCard, SpellCard, etc) - then you could just use a Card
class with an enum CardType
Well the active cards have a few unique things that the warriorcards dont have and same visa versa.
From debugging it seems it is not getting to the part where it copies the data across due that out of range error. At the very least you've likely solved my potential issue once I get the out of range error solved.
$$anonymous$$uch appreciated.
Your answer
Follow this Question
Related Questions
Multiple Cars not working 1 Answer
Distribute terrain in zones 3 Answers
[Answered](C#)Comparing two Lists for a Crafting System 2 Answers
How do I compare two lists for equality (not caring about order) C# 6 Answers
List/Array should handle another List/Array - Upgrade System - Performance 1 Answer