Creating card game - copy of list to new list - creating cycle
Hi, I am new in Unity and I am trying to make a card game (not classic cards).
I have a deck from 40 cards. 10 cards are added to one panel, 3 cards to another one and then 27 left are displayed in another panel on button press (player see only one, but it's opening 3 cards). Everything works perfect, but when those 27 cards ends I need them to go back to the same list that player could press button and see it again that it would work as a cycle.
I was trying to create one more list and add those there, but the result is that after the last card on button press it shows only that one (since the function has to open three cards at one press so it opens the same card three times). This is my code. Does someone have an idea how can I fix it?
public GameObject number; //for number of deck array
public List<GameObject> deck = new List<GameObject>();
private List<GameObject> displayed = new List<GameObject>();
//it's code only for those 27 cards that left
public void OpenThreeCards(GameObject panel)
{
for (int i = 0; i < 3; i++) {
int number = deck.Count - 1;
if (number == 0) {
deck = displayed;
}
GameObject k;
int r;
r = 0;
k = Instantiate (deck [number]);
k.transform.SetParent (panel.transform);
k.GetComponent<RectTransform> ().localRotation = Quaternion.identity;
displayed.Add (deck [number]);
deck.Remove (deck [number]);
}
}
Answer by troien · Apr 18, 2016 at 03:40 PM
I'm not sure whether i understand what you want, so I'm going to interpret your explanation as best as I can :p
You have 2 lists, deck and displayed.
At start, deck has 27 cards, displayed has 0 cards.
At the end displayed has 27 cards, deck has 0 cards.
When 2 hapens you want to go back to 1.
You currently call deck = displayed, I suppose you do that in an effort to achieve this. However, the first thing thats wrong with this is that List is a reference type, not a value type. When you set deck to displayed. They are from then on the same object (not just 2 objects with the same value). Which means that changes to deck are also applied to displayed and vise versa. you probably want to do deck = new List(displayed) instead. As this would create a new list with the same values. Secondly, you do this before you do something with the last card. I'm assuming this is not intentional, not sure though :p
I think you want to do something like this:
public void OpenThreeCards(GameObject panel)
{
// assumes amount of cards (27) can be devided by 3 (3, 6, 9, 12, etc.)
for (int i = 0; i < 3; i++)
{
int number = 0; // first instead of last to keep displayed in the same order as deck
GameObject k;
k = Instantiate(deck[number]);
k.transform.SetParent(panel.transform);
k.GetComponent<RectTransform>().localRotation = Quaternion.identity;
displayed.Add(deck[number]);
deck.Remove(deck[number]);
}
if (deck.Count == 0)
{
deck = new List<GameObject>(displayed);
displayed.Clear();
}
}
Another thing you might want to consider for efficiency is using 2 Queues instead Lists.
Ok, it works but..if (deck.Count == 0) { deck = new List<GameObject>(displayed); displayed.Clear(); }
It gives plus 27 cards to deck, but doesn't remove all those 27 cards that were displayed before. So after this I have 27 cards + 27 cards + more and more... But I need that in the deck would be only 27 cards
With removing, do you mean removing from the list? Because they are removed from the list. The list should never be greater then 27. Or do you mean removing the instantiated gameobjects themselves?
If you mean removing the instantiated gameobjects. I don't know when you intend to destroy them, only on reset or while playing aswell?, as currently you are not holding a reference to your instantiated gameobject anywhere. ("GameObject k" is your instantiated gameobject, but you don't store this reference anywhere in your code) So if you want to destroy all 27 insantiated gameobjects at the moment you reset. You'll need to store the reference in a list or something, and call Destroy() for each item in that list when you reset. Then also clear hat list at reset.
And are you using the displayed list anywhere outside of the sniped of code you posted?