- Home /
Null first array index and shift rest by 1?
Hello!
I am having an issue shifting all array members down by 1 while deleting the first.
Currenly I have the following code: I set the player's deck and populate it using Unity's inspector. It is set to 30. At this point the entire deck is populated.
I then set the player's hand and set it to 5 in Unity's inspector. The array however isn't populated and everything is left null.
Next I check if the first index in the player's hand is empty, if it is, I copy the first card in the deck to the player's hand. I then call the ShiftDeck function to move everything down by 1.
ShiftDeck works by setting the current index gameobject to the next indexed gameobject. So i = i + 1. This continues until all objects are pushed over, then the last one is set to null.
public GameObject[] playerDeck;
public GameObject[] playerHand;
if (playerHand[0] == null)
{
playerHand[0] = playerDeck[0];
shiftDeck(playerDeck);
}
if (playerHand[1] == null)
{
playerHand[1] = playerDeck[0];
shiftDeck(playerDeck);
}
This continues until playerHand[4]
public void shiftDeck(GameObject[] deck)
{
for (int i = 0; i < deck.Length - 1; i++)
{
if (deck[i] != null)
{
deck[i] = deck[i + 1];
}
}
deck[deck.Length - 1] = null;
}
This however does not seem to work...sometimes other elements in the middle of the array are null rather than the last 5 on the list. Other times it functions just fine. when it does function fine however, player's hand is not copied correctly and all values are left as null.
Anyone have any examples as to how I would properly shift all array indexed items down by 1?
Other times it functions just fine.
The loop looks O$$anonymous$$ to me. I'm not sure why you have the if function inside your loop. Let it just move any nulls it finds.
for (int i = 0; i < deck.Length - 1; i++)
deck[i] = deck[i + 1];
deck[i] = null;
That being said, you might want to try using a dynamic container, like a List< GameObject >
, rather than a static length array. This link looks like a good tutorial on em: http://csharp.net-informations.com/collection/list.htm
Why not just use a List ins$$anonymous$$d of re-inventing the wheel?
Answer by Alanisaac · Feb 01, 2015 at 11:35 PM
In situations like this, I would personally use a different collection type. It depends on your particular mechanics, but you might want to consider using:
A
Queue
, if the only behaviors you need are removing from the top and adding to the end of the deck.A
List
if removing and inserting at certain places in the middle of the deck is also important.
You should still be able to use Unity's inspector to populate the other collection types in the same way. Here's a List
example (and don't forget to make sure your code file is using System.Collections.Generic
!):
public List<GameObject> playerDeck;
public GameObject[] playerHand;
public void MyMethod()
{
if (playerHand[0] == null)
{
playerHand[0] = playerDeck[0];
// Remove the first value in the deck
playerDeck.RemoveAt(0);
// If you want to preserve the "array shifting" effect,
// so your last item is null, just uncomment this next line:
// playerDeck.Add(null);
}
// Repeat as needed...
if (playerHand[1] == null)
{
playerHand[1] = playerDeck[0];
playerDeck.RemoveAt(0);
}
}
As to your original question, I'm not sure I can see where the problem is. I will say if in any of those "etc." statements you didn't list, you're accidentally referencing the wrong array, you might end up with null values in odd places. I would encapsulate a method like below, to avoid potential errors in writing that code five times.
private void DrawCard(int handPosition)
{
if (playerHand[handPosition] == null)
{
playerHand[handPosition] = playerDeck[0];
shiftDeck(playerDeck);
}
}
Otherwise, see this link. The approach described looks the same as yours.