Search Unity

an array inside of an array

Discussion in 'Scripting' started by eteeski, Mar 2, 2010.

  1. eteeski

    eteeski

    Joined:
    Feb 2, 2010
    Posts:
    476
    what im looking to do is design AI that will detect oncoming obstacles. if there is an oncoming collision, the AI will find a point to move towards in order to avoid getting hit.

    of coarse, if multiple enemies do this across the playing field every frame, this would take up way too much cpu. so i've come up with a few things that would speed this up.

    -first make a "grid" that will raycast evenly across the playing field to find a collision free path

    -because the oncoming collision isn't going to move too much each frame, only raycast, say, 15 times a second

    -last, because all the enemies will be on the same playing field, make just one object that will raycast from each square on the grid and store weather or not there is a collision there. then the enemies only have to raycast on their current path, and if there is a collision, ask the object where the closest free path is

    this is where i am a little stuck. mainly my problem is figuring out the best way to keep track of where all the free and nonfree paths/spots are on the grid. My guess is that i could create an array with a number of slots, representing rows, and in each row, an array for the collums.

    i've read that an array inside of an array is slow, but is it too slow to raycast about 20*15 times 15 times a second?

    if there is another way to do this, that would be great, because i can imagine it could get a little messy using arrays
     
  2. Mirage

    Mirage

    Joined:
    Jan 13, 2010
    Posts:
    230
    Google A*Pathfinding. Someone already wrote a pathfinder for Unity, it would save alot of headache if you could use his. Pathfinders are not fun to write.
     
  3. eteeski

    eteeski

    Joined:
    Feb 2, 2010
    Posts:
    476
    A* pathfinding does sound interesting, but it looks like it deals with multiple point paths. All i need is a single point.

    i can make the programing to find this path, i just need to know if there is an easier way to store a bunch of true and falses based on a grid.

    so far the only way i can think of to do this is to make an array inside of an array. one array would contain a number of arrays, and each of those arrays will contain a number of true and falses.

    i want it so that an enemy can ask "hey, im about to hit something. where is a free point i can go towards?"

    this would be exactly like if you were to program a version of Battle Ships. how can i keep track of what squares have a hit and which have a miss.
     
  4. RobbieDingo

    RobbieDingo

    Joined:
    Jun 2, 2008
    Posts:
    484
    Hi, it is possible to make multi-dimensional arrays, but do you really need to do that?

    For example, your BattleShips analogy could be achieved using a single array of booleans, as follows:

    If the game board is 10 x 10 (you need an array of 100 elements)

    - Co-ordinate X1,Y1 (ie. top-left corner) would be the first element of the array.

    - Co-ordinate X10,Y10 (ie. bottom-right corner) would be the last element of the array.

    - Co-ordinate X3,Y8 could be queried by looking at element 38, as in (3*10) + 8


    Just a thought.
     
  5. eteeski

    eteeski

    Joined:
    Feb 2, 2010
    Posts:
    476
    thats a good idea robbie, lets see... array length = grid width * grid height. array number = (y-1 * width) + x. yeah, i think that'll work. thanks.