Search Unity

Stack, Push and Pop with a int Reference

Discussion in 'Scripting' started by frankspasm, Sep 23, 2021.

  1. frankspasm

    frankspasm

    Joined:
    Dec 16, 2015
    Posts:
    9
    is there a good way to make a int reference become static once its on a list or stack? or is there another solution to my problem that's obvious and staring me in the face?

    When I Push my Stack(s), the seedX int it returns the CURRENT value of seedX not the value it was when the seedX int was originally pushed to the stack. I'm trying to make the coordinates go back to its proceeding coordinates on the stack but the [x,y] just staying in the same spot.

    Id appreciate any way to do this or alternative. thank you.

    Code (CSharp):
    1. Stack<int> stackCellsX = new Stack<int>();
    2.         Stack<int> stackCellsY = new Stack<int>();
    3.         int testHallAmount = 0; //test option, remove later
    4.  
    5.         while (testHallAmount < 100)// this will be the whole grid in future once bug is fixed.
    6.         {          
    7.  
    8.             stackCellsX.Push(seedX); //add to stack, need a way to turn the "seedX/seedY" into a static int
    9.             stackCellsY.Push(seedY); //add to stack
    10.  
    11.             cells[seedX, seedY] = 4; //"4" makes the cell a hallway
    12.  
    13.             Debug.Log("stack: " + stackCellsX.Count + "X: " + seedX + " Y: " + seedY);
    14.             List<string> directions = new List<string>();
    15.  
    16.  
    17.             if ((seedY + 2) < _mapSizeY && (seedY + 2) > 0 && cells[seedX, seedY + 2] == 0) // North, these look for empty parts of the grid for a potential direction.
    18.             {
    19.                 directions.Add("North");
    20.             }if ((seedY - 2) < _mapSizeY && (seedY - 2) > 0 && cells[seedX, seedY - 2] == 0) // South
    21.             {
    22.                 directions.Add("South");
    23.  
    24.             }if ((seedX + 2) < _mapSizeX && (seedX + 2) > 0 && cells[seedX + 2, seedY] == 0) // East
    25.             {
    26.                 directions.Add("East");
    27.  
    28.             }if ((seedX - 2) < _mapSizeX && (seedX - 2) > 0 && cells[seedX - 2, seedY] == 0) // West
    29.             {
    30.                 directions.Add("West");
    31.             }
    32.  
    33.             if (directions.Count > 0)
    34.             {
    35.                 NSEW = directions[Random.Range(0, directions.Count)]; // if there are open direction(s), pick a random one.
    36.             }else{
    37.                 stackCellsX.Pop();//if there's no place to go, go backwards and start the loop again.
    38.                 stackCellsY.Pop();//if there's no place to go, go backwards and start the loop again.
    39.                 Debug.Log("POP: " + stackCellsX.Count + "X: " + seedX + " Y: " + seedY);
    40.                 testHallAmount++;//this is here as to not make an infnite loop, remove later.
    41.                 continue;
    42.             }
    43.  
    44.             if (NSEW == "North") // North
    45.             {
    46.                 seedY = (seedY + 2);
    47.                 cells[seedX, seedY - 1] = 4;
    48.                 testHallAmount++;
    49.                 continue;
    50.             }else if (NSEW == "South") // South
    51.             {
    52.                 seedY = (seedY - 2);
    53.                 cells[seedX, seedY + 1] = 4;
    54.                 testHallAmount++;
    55.                 continue;
    56.             }else if (NSEW == "East") // East
    57.             {
    58.                 seedX = (seedX + 2);
    59.                 cells[seedX - 1, seedY] = 4;
    60.                 testHallAmount++;
    61.                 continue;
    62.             }else if (NSEW == "West") // West
    63.             {
    64.                 seedX = (seedX - 2);
    65.                 cells[seedX + 1, seedY] = 4;
    66.                 testHallAmount++;
    67.                 continue;
    68.             }
    69.         }
    70.     }
     

    Attached Files:

  2. Kurt-Dekker

    Kurt-Dekker

    Joined:
    Mar 16, 2013
    Posts:
    38,745
    Integers are value types. This means you "put in" a fresh copy of whatever the value was with Push(x) and then you "take out" that value with Pop(); as a fresh copy.

    Just doing a Pop() operation on a stack does nothing to any values that were pushed into it before. You need to take those values out to some result variable:

    Code (csharp):
    1. int result = MyStack.Pop();
    Is this your problem? I see lines 37 and 38 just discarding stuff off the stack. Are you ever using what's on the stack?

    Are you just trying to make a flood fill?
     
    Bunny83 likes this.
  3. frankspasm

    frankspasm

    Joined:
    Dec 16, 2015
    Posts:
    9
    Thanks for the reply to my problem.

    Yes, I am attempting a flood fill through a single cell corridor and random directions. Once the corridor hits a dead end. it retreats backwards along the stack/corridor until it finds a cell corridor that has at least one empty neighbor. It repeats this pattern until the grid is filled as a maze.

    the stack holds the X Y coordinate. I want it to go to the XY coordinate (info stored on stack), check for neighbors, then pick a new direction.
     
  4. frankspasm

    frankspasm

    Joined:
    Dec 16, 2015
    Posts:
    9
    ayy! problem solved, thank you for your help Kurt.

    problem i was having wasn't so much the int, though that was a problem. it was the order of when to initialize the push. i'm going to post my current correction, though this can probably be cleaned up a lot more.


    Code (CSharp):
    1.         Stack<int> stackCellsX = new Stack<int>();
    2.         Stack<int> stackCellsY = new Stack<int>();
    3.         cells[seedX, seedY] = 4;
    4.  
    5.         int testHallAmount = 0; //test option, remove later
    6.  
    7.         while (testHallAmount < 200)// this will be the whole grid in future once bug is fixed.
    8.         {
    9.             List<string> directions = new List<string>();
    10.  
    11.  
    12.             if ((seedY + 2) < _mapSizeY && (seedY + 2) > 0 && cells[seedX, seedY + 2] == 0) // North, these look for empty parts of the grid for a potential direction.
    13.             {
    14.                 directions.Add("North");
    15.             }if ((seedY - 2) < _mapSizeY && (seedY - 2) > 0 && cells[seedX, seedY - 2] == 0) // South
    16.             {
    17.                 directions.Add("South");
    18.  
    19.             }if ((seedX + 2) < _mapSizeX && (seedX + 2) > 0 && cells[seedX + 2, seedY] == 0) // East
    20.             {
    21.                 directions.Add("East");
    22.  
    23.             }if ((seedX - 2) < _mapSizeX && (seedX - 2) > 0 && cells[seedX - 2, seedY] == 0) // West
    24.             {
    25.                 directions.Add("West");
    26.             }
    27.  
    28.             if (directions.Count > 0)
    29.             {
    30.                 NSEW = directions[Random.Range(0, directions.Count)]; // if there are open direction(s), pick a random one.
    31.             }else{
    32.                 seedX = stackCellsX.Pop();
    33.                 seedY = stackCellsY.Pop();
    34.                 testHallAmount++;//this is here as to not make an infnite loop, remove later.
    35.                 continue;
    36.             }
    37.  
    38.            
    39.  
    40.             if (NSEW == "North") // North
    41.             {
    42.                 stackCellsX.Push(seedX);
    43.                 stackCellsY.Push(seedY = (seedY + 2));
    44.                 cells[seedX, seedY - 1] = 4;
    45.                 testHallAmount++;
    46.                 cells[seedX, seedY] = 4;
    47.                 continue;
    48.             }
    49.             else if (NSEW == "South") // South
    50.             {
    51.                 stackCellsX.Push(seedX);
    52.                 stackCellsY.Push(seedY = (seedY - 2));
    53.                 cells[seedX, seedY + 1] = 4;
    54.                 testHallAmount++;
    55.                 cells[seedX, seedY] = 4;
    56.                 continue;
    57.             }
    58.             else if (NSEW == "East") // East
    59.             {
    60.                 stackCellsX.Push(seedX = (seedX + 2));
    61.                 stackCellsY.Push(seedY);
    62.                 //seedX = (seedX + 2);
    63.                 cells[seedX - 1, seedY] = 4;
    64.                 testHallAmount++;
    65.                 cells[seedX, seedY] = 4;
    66.                 continue;
    67.             }
    68.             else if (NSEW == "West") // West
    69.             {
    70.                 stackCellsX.Push(seedX = (seedX - 2));
    71.                 stackCellsY.Push(seedY);
    72.                 cells[seedX + 1, seedY] = 4;
    73.                 testHallAmount++;
    74.                 cells[seedX, seedY] = 4;
    75.                 continue;
    76.             }
    77.         }
    78.     }