Search Unity

  1. Welcome to the Unity Forums! Please take the time to read our Code of Conduct to familiarize yourself with the forum rules and how to post constructively.
  2. Dismiss Notice

Please Help Me Understand This Script..

Discussion in 'Scripting' started by Omnitay, Jul 4, 2014.

  1. Omnitay

    Omnitay

    Joined:
    Jun 30, 2014
    Posts:
    7
    I'm interested in making a card game, so after going through some tutorials and posts on UnityAnswers I found this script.. I'm still learning the C# language so hopefully this isn't asking for too much but I've commented on the code here below to explain what I understand and decide what doesn't make sense to me. I believe I understand most the basics to coding but im still a total newb to C#. I would really appreciate it if anyone experienced enough could help explain this person's script in the places I seem to be confused.

    Code (CSharp):
    1. using UnityEngine;
    2. using System.Collections;
    3. using System.Collections.Generic;
    4.  
    5. //Gives the Script a class
    6. public class DeckOfCards : MonoBehaviour
    7. {
    8.     public List<GameObject> deck = new List<GameObject>();            //declaring of deck, cards, and hand lists
    9.    
    10.     private List<GameObject> cards = new List<GameObject>();
    11.     private List<GameObject> hand = new List<GameObject>();
    12.     private int cardsDealt = 0;            //declaring an integer that counts the cards dealt
    13.     private bool showReset = false;        //declaring a true or false statement for showing the reset button
    14.    
    15.     void ResetDeck()    //function for reseting the deck, returns nothing.
    16.     {
    17.  
    18.         cardsDealt = 0;    //start the function with an integer of 0 on the cards dealt variable (no cards are dealt once reset)
    19.         for (int i = 0; i < hand.Count; i++) { //for loop (not sure what this does..)
    20.             Destroy(hand[i]); //?
    21.         }
    22.         hand.Clear(); //?
    23.         cards.Clear(); //?
    24.         cards.AddRange(deck); //?
    25.         showReset = false; //?
    26.     }
    27.    
    28.     GameObject DealCard() //Button for dealing a card from the deck
    29.     {
    30.         if(cards.Count == 0) //If the deck is out of cards..
    31.         {
    32.             showReset = true; //Show the reset button
    33.             return null; //?
    34.             //Alternatively to auto reset the deck:
    35.             //ResetDeck();
    36.         }
    37.        
    38.         int card = Random.Range(0, cards.Count - 1); //I think this randomizes the cards that you draw. dont really understand how it works
    39.         GameObject go = GameObject.Instantiate(cards[card]) as GameObject;
    40.         cards.RemoveAt(card);
    41.        
    42.         if(cards.Count == 0) {
    43.             showReset = true;
    44.         }
    45.        
    46.         return go;
    47.     }
    48.    
    49.     void Start() //At the start of the scene when this object is loaded, reset the deck
    50.     {
    51.         ResetDeck();
    52.  
    53.     }
    54.    
    55.     void GameOver() //Not sure what this function is for or what it does ???
    56.     {
    57.         cardsDealt = 0;
    58.         for (int v = 0; v < hand.Count; v++) {
    59.             Destroy(hand[v]);
    60.         }
    61.         hand.Clear();
    62.         cards.Clear();
    63.         cards.AddRange(deck);
    64.     }
    65.    
    66.     void OnGUI()//?
    67.     {
    68.         if (!showReset) { //I think this means if showReset is false, then.. display the deal button on the GUI..
    69.             //  draws Deal button
    70.             if (GUI.Button(new Rect(10, 10, 100, 20), "Deal"))
    71.             {
    72.                 MoveDealtCard(); //Moves the card after it's chosen to be dealt - function declared here, method of function is below
    73.             }
    74.         }
    75.         else { //If showReset isnt false.. then show and draw the reset button..
    76.             // draws Reset button
    77.             if (GUI.Button(new Rect(10, 10, 100, 20), "Reset"))
    78.             {
    79.                 ResetDeck(); //Calls on the reset deck function.. to reset the deck.. duh
    80.             }
    81.         }
    82.         // draws game over button.. dont understand how this is called..
    83.         if (GUI.Button(new Rect(Screen.width - 110, 10, 100, 20), "GameOver"))
    84.         {
    85.             GameOver();
    86.         }
    87.     }
    88.    
    89.     void MoveDealtCard() //function for moving the card once its dealt
    90.     {
    91.         GameObject newCard = DealCard();
    92.         // checks if the deck has any cards in it..
    93.         if (newCard == null) {
    94.             Debug.Log("Out of Cards");
    95.             showReset = true;
    96.             return;
    97.         }
    98.         //dont understand how this works..
    99.         //changes position of the cards ( plan to change to hand style and not stacked.. )
    100.         //newCard.transform.position = Vector3.zero; - idk what this means!?
    101.         newCard.transform.position = new Vector3((float)cardsDealt / 1, (float)cardsDealt / 1, (float)cardsDealt / 1); // place card 1/4 up on all axis from last
    102.         hand.Add(newCard); // add card to hand (not sure how this even works..
    103.         cardsDealt ++;
    104.     }
    105. }
     
  2. Omnitay

    Omnitay

    Joined:
    Jun 30, 2014
    Posts:
    7
    Anyone? Too much to ask..? :/
     
  3. rrh

    rrh

    Joined:
    Jul 12, 2012
    Posts:
    331
    1. for (int i = 0; i < hand.Count; i++) { //for loop (not sure what this does..)
    2. Destroy(hand); //?
      [*] }[/code]

    You could think of Destroy as the opposite of Instantiate. All those cards that have been created are destroyed.


    Clear and AddRange are functions of the List class. AddRange in this case recopies the contents of deck to cards. Notice this is the only line that references deck so it will always still contain the orignial 52 cards or whatever it was filled with.

    Random.Range() will return a random number.

    Random.Range(0, cards.Count - 1) will, if you have 10 entries in the cards list will return a number between 0 and 9.
     
    Omnitay likes this.
  4. hpjohn

    hpjohn

    Joined:
    Aug 14, 2012
    Posts:
    2,190
    http://docs.unity3d.com/ScriptReference/

    Assuming the script works, just look up the bits in the docs.
    a few quickies tho:
    xxx.Clear removes all elements from a list
    AddRange is putting the entirety of deck into cards
    return null means 'dont bother doing what is below in this function'
    Line 101 has some super redundant maths.... anything/1 is anything
     
    Omnitay likes this.
  5. Omnitay

    Omnitay

    Joined:
    Jun 30, 2014
    Posts:
    7
    yeah I was messing with the floats, i sorta realize that now, and i'll definitely check out the link, really helpful. Also thanks alotfor the quick reply and simple explanations

    Thanks a lot, just now saw this, really appreciate the help, makes it much easier for me.
     
    Last edited: Jul 5, 2014
  6. Omnitay

    Omnitay

    Joined:
    Jun 30, 2014
    Posts:
    7
    How could I change the placement of the cards inside the MoveDealtCard function so that they appear next to each other with some space inbetween on the x axis.. (instead of on top of each other) Do i need to give them rigid bodies or use instantiate? I've tried looking it up but can't find out how
     
  7. rrh

    rrh

    Joined:
    Jul 12, 2012
    Posts:
    331
    The position of the cards is changed by the "newCard.transform.position = " assignment. Right now the XY and Z position are all set the same, but if you tried different math based on cardsDealt you could change their spacing.