0
votes

I'm new to Unity and component-based programming and have a little problem. With so many card games, you'd think there would be some instruction on how to deal the cards, but there aren't...So, here I am.

I created a Card class, a card database, and a deck class. I created a GameObject in the Hierarchy that uses the deck class. Here's the deck class:

public class PlayerDeck : MonoBehaviour
{
    public int d;
   
    public List<Card> deck;
    public List<Card> container;
    public List<Card> distributed;
    public List<Card> discarded;
    public int decksize;

    void Start()
    {
        
        LoadVariables();
        LoadDeck();
        ShuffleDeck()   
        
        
    }

    public void LoadVariables()
    {
        d = 0;
        decksize = 64;
    }

    public void LoadDeck()
    {
       

        for (int i = 0; i < 64; i++)
        {
            d = i;
            deck[d] = CardDatabase.cardList[i];
        }
    }

    public void ShuffleDeck()
    {

 
       for (int i=0; i<decksize; i++)
        {
            container[0] = deck[i];
            int rnd = Random.Range(i, decksize);
            deck[i] = deck[rnd];
            deck[rnd] = container[0];

        }


    }

    public void DealCards()
    {
    }

}

When I run this in debug, I can see the cards are shuffled. Now I need the deck to initially deal each player 7 cards like in UNO. This is where I don't understand component-based programming. If I have a GameObject called Deck, and 4 other GameObject for players, how can the 4 player gameobjects receive the cards the Deck deals them?

Any coding help would be appreciated.

1
You shuffled the deck, then run a loop from 0 to card to give x amount of player. 4x7 for Uno. Look into % operand to know which player to give the current card. - Everts
No, this does not work for component-based programming. This is just straight programming which I already know. - user3623689
@user3623689 If verified_tinker's answer helped you, consider accepting it - Ruzihm

1 Answers

1
votes

Cards are a prime candidate for Scriptable Objects — a Unity-specific, designer-friendly method of creating new cards, instead of using code-based databases. Brackeys has an excellent tutorial on this exact use case, so I won't waste time on that here.

Once you've created the class deriving from ScriptableObject and created assets based on that, and you've created a MonoBehaviour class (component) for reading and displaying that data (the tutorial covers this also, as CardDisplay.cs), you need to save the Game Object with that component as a prefab.

Finally, you'll create another script that references the prefab and instantiates it 4 times. For example:

// You'll drag the prefab you created into the field in the inspector:
public GameObject cardPrefab;

public void DealCards()
{
    // ...And then instantiate the prefab 4 times, once for each holder.
    foreach (Transform cardHolder in cardHolders)
        GameObject card = Instantiate(cardPrefab, parent: cardHolder);
}

This will instantiate the card Game Objects in the scene, as children of their holder. Customize as needed; Instantiate() has many variants. It always returns the new Game Object, which you can save and use as you like.

If you want to shuffle and then deal cards from a deck to one player, then hold a List of card prefabs in PlayerDeck.cs and loop over that instead.