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

Removing 2D array elements

Discussion in 'Scripting' started by RayDawg, Sep 24, 2014.

  1. RayDawg

    RayDawg

    Joined:
    Mar 19, 2013
    Posts:
    108
    I have a 10x10 grid stored in a 2D array. I plan on spawning objects within that grid and if the position is taken or close too close to another object, I want to remove that position so that the RNG won't select that particular position again. Could someone point me in the right direction?

    This is the code I have so far:
    Code (CSharp):
    1. Grid = GridSpace[Random.Range(0, Width), Random.Range(0, Height)].transform.position;
    The idea is to use the GridSpace object's position as a starting point and then randomly select a number between 0 - 10 for the width and height.
     
  2. A.Killingbeck

    A.Killingbeck

    Joined:
    Feb 21, 2014
    Posts:
    483
    Do the random generation in a separate list/list of lists of ints.
    Loop through this 2D list and create a grid space for each pair of ints.

    That should work, if you get my meaning!
     
  3. RayDawg

    RayDawg

    Joined:
    Mar 19, 2013
    Posts:
    108
    I ashamed to say that you lost me.
     
  4. JoeStrout

    JoeStrout

    Joined:
    Jan 14, 2011
    Posts:
    9,840
    Don't feel bad, he lost me too. :)

    You've got a 2D array of... what? GameObjects? Can't you just set any element of that array to nil once it's been used? Or, keep another 2D array of Boolean, which you flip to indicate that a cell has been used?
     
  5. RayDawg

    RayDawg

    Joined:
    Mar 19, 2013
    Posts:
    108
    The 2D array is full of integers.
     
  6. JoeStrout

    JoeStrout

    Joined:
    Jan 14, 2011
    Posts:
    9,840
    OK, you haven't told me what those integers mean, but can you set them to 0 or -1 or some such to indicate that the cell has been used?
     
  7. RayDawg

    RayDawg

    Joined:
    Mar 19, 2013
    Posts:
    108
    The integers within Gridspace represent the cell, so [0,1] would mean the cell's x position is 0, and the y position is 1.
     
  8. JoeStrout

    JoeStrout

    Joined:
    Jan 14, 2011
    Posts:
    9,840
    No, those are the array indexes. An array maps indexes to some sort of data. WHAT is in cell position 0, 1? Show us the declaration of GridSpace, please. Or show us the declaration of Grid. Or both. If you want help, provide enough information for us to help you!
     
  9. RayDawg

    RayDawg

    Joined:
    Mar 19, 2013
    Posts:
    108
    Ohhhh, you had me confused there. Here's the declarations:

    Code (CSharp):
    1.  
    2. private Vector3 Grid;
    3. private GameObject[,] GridSpace;
    4.  
     
  10. JoeStrout

    JoeStrout

    Joined:
    Jan 14, 2011
    Posts:
    9,840
    OK, so you have an array of GameObject. So, back to the first suggestion: can't you just set this to nil when a space is occupied? Or, if they all start out nil, then set it to something else (like the object you're placing there) when a space is occupied?
     
  11. RayDawg

    RayDawg

    Joined:
    Mar 19, 2013
    Posts:
    108
    .removeAt and .remove doesn't work with GridSpace. How would I go about setting it to nil/nothing?
     
  12. JoeStrout

    JoeStrout

    Joined:
    Jan 14, 2011
    Posts:
    9,840
    .Remove and .RemoveAt don't set a value to null. You set a value to null like this:
    Code (CSharp):
    1.   GridSpace[row, col] = null;
    And you check whether a particular cell is currently null like this:
    Code (CSharp):
    1.   if (GridSpace[row, col] == null) { ... }
     
  13. RayDawg

    RayDawg

    Joined:
    Mar 19, 2013
    Posts:
    108
    But wouldn't that also mean the Random.range can still find the particular gameobject when called again? I'm trying to remove the gameobject from the array so that when the random.range gets called again, it won't (or shouldn't) pick the null gameobjects.
     
  14. JoeStrout

    JoeStrout

    Joined:
    Jan 14, 2011
    Posts:
    9,840
    Yes, you'd have to use a loop that checks whether the picked row/column is free, and keeps trying another until it finds a free one.

    Alternatively, you could not use a 2D array at all, but instead a simple List<Vector2> that is initialized to all the available locations. Then, select and actually remove items from this list.
     
  15. A.Killingbeck

    A.Killingbeck

    Joined:
    Feb 21, 2014
    Posts:
    483
    That is pretty much what I suggested, just to fix the issue of not understanding what I meant. Keeping track of index locations used/which are still free to be used is the easiest way to ensure a particular position won't be selected again.

    BUT, keep in mind it will cause extra garbage which you don't necessarily need/want. There are some pretty elegant solutions to this very problem littered around which allow you to do much of it directly on the stack