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

Simple Data Structures Question

Discussion in 'Scripting' started by decoy98, Aug 12, 2014.

  1. decoy98

    decoy98

    Joined:
    Aug 30, 2011
    Posts:
    36
    Hello, my code is below:
    Code (CSharp):
    1. using UnityEngine;
    2. using System.Collections;
    3. using System.Collections.Generic;
    4.  
    5. public class PizzaBox : MonoBehaviour {
    6.     public static int currentPizzaAmount;
    7.     public int pizzaAmount = 10;
    8.     private Sk8erboi sk8erboi;
    9.  
    10.     public Transform currentPizza;
    11.     public Transform nextPizza;
    12.  
    13.     public Transform[] Pizzas;
    14.  
    15.     public Transform[] randomPizzas;
    16.  
    17.     public bool isPlayerShoot = false;
    18.     public bool isReloadPizzas = false;
    19.  
    20.     public Stack<Transform> pizzaStack = new Stack<Transform>();
    21.  
    22.     // Use this for initialization
    23.     void Start () {
    24.         currentPizzaAmount = pizzaAmount;
    25.         sk8erboi = GameObject.FindObjectOfType<Sk8erboi> ();
    26.     }
    27.  
    28.     // Update is called once per frame
    29.     void Update () {
    30.         if (currentPizzaAmount <= 0) {
    31.             sk8erboi.canShoot = false;    
    32.         }
    33.  
    34.         randomPizzaStack ();
    35.     }
    36.  
    37.     void randomPizzaStack () {
    38.         int[] randArray = new int[10];
    39.  
    40.         if (isReloadPizzas) {
    41.             pizzaStack.Clear();
    42.             for (int v = 0; v < randomPizzas.Length-1; v++) {
    43.                 randArray[v] = Mathf.RoundToInt(Random.Range (0, 3));
    44.                 pizzaStack.Push(Pizzas[randArray[v]]);
    45.             }
    46.             isReloadPizzas = false;
    47.         }
    48.  
    49.         if (isPlayerShoot) {
    50.             pizzaStack.Pop();
    51.             isPlayerShoot = false;
    52.         }
    53.         pizzaStack.CopyTo (randomPizzas, 0);
    54.     }
    55.  
    56. }
    57.  
    ________________________________________________________________________________________
    The problem

    When randomPizzaStack is called when isPlayerShoot is called the pizzaStack<Transform> that is copied to the randomPizzaArray<Transform> will copy a Stack<Transform> with the the popped transform on the tail/end of the array instead of copying an updated Stack<Transform> with null as the end of the array.

    Any expert help would be appreciated!

    Note #1:
    I have tried different data structures other than Stack such as Queue and List, but still no luck :(
     
    Last edited: Aug 12, 2014
  2. Fluzing

    Fluzing

    Joined:
    Apr 5, 2013
    Posts:
    815
    Your problem is unreadable. Can you please rewrite it? I can't understand what you mean right now.
     
  3. decoy98

    decoy98

    Joined:
    Aug 30, 2011
    Posts:
    36
    Hi Fluzing,

    To clarify, when I choose to copy a stack to an array after choosing to pop an object, the copied array shows an array with the popped object copied on the end of the array instead of making the end null.
     
  4. Gaello

    Gaello

    Joined:
    Mar 8, 2013
    Posts:
    13
    Are you sure that popped object is popped?
    Try to put breakpoint at 50th line and debug.
    Or try to clear the stack :)
     
  5. decoy98

    decoy98

    Joined:
    Aug 30, 2011
    Posts:
    36
    Thanks for the reply,

    What do you mean by break point?

    I added "break;" on the 50th line, but since it's not a loop, it gave me an error. Also, you can't pop a cleared stack.
     
  6. Gaello

    Gaello

    Joined:
    Mar 8, 2013
    Posts:
    13
    Both Monodevelop and Visual Studio allow you to debug your code to help you find where is a problem.

    When your game try to call line with breakpoint then game will freeze and mono or vs should popup. This allow you to see what there is in variables for example :)

    And of course you need to attach debug to unity before.

    This red dot is breakpoint
     

    Attached Files:

  7. decoy98

    decoy98

    Joined:
    Aug 30, 2011
    Posts:
    36
    Sorry, I am a beginner in the art of debugging. I managed to add a break point then played the game, and same thing happens. :(((
     
  8. Gaello

    Gaello

    Joined:
    Mar 8, 2013
    Posts:
    13
    Breakpoint don't solve problem for you but allow you to see more. I think I will write little article about that.. :p

    But let's back to problem.. I'm not sure if this does not only replace existing objects.

    pizzaStack.CopyTo (randomPizzas, 0);​

    Try to clear randomPizzas before it
     
  9. decoy98

    decoy98

    Joined:
    Aug 30, 2011
    Posts:
    36
    Actually I solved it using lists!

    This the a quick and dirty trick: change the data structure from Queue to List, then implement this for "isPLayerShoot"
    Code (CSharp):
    1. if (isPlayerShoot) {
    2.             num--;
    3.             pizzaList.RemoveAt(0);
    4.             pizzaList.Insert(num, null);
    5.             isPlayerShoot = false;
    6.         }
    I totally went off the computer for a while to realize why in the world I am using the Stack object if I really wanted to access the end of the data structure. So I figured in this case change the Stack to List, because I wanted the last element to be null after the first element has been replaced by the element coming after it.

    Thanks again for trying to assist. I will read your article about breakpoints soon.

    -Apebeast