Out of Bounds Problems

BlitzMax Forums/BlitzMax Beginners Area/Out of Bounds Problems

I keep getting out of bounds when I run the following function on the sdir array. It usually happens on the last line, but occassionally on the line before. In debug trace, it is only showing values assigned to element 0,1,2,3. Any suggestions?

Local sdir:Int[5]=New Int[5]

  If R.w > m_x Then R.w = m_x
  If R.h > m_y Then R.h = m_y

  'Work out the difference between the room width and Map width etc
  spaceX = m_x - R.w
  spaceY = m_y - R.h

  'Choose a random position For the New room
  newx = Rand(1,spacex - 1)
  newy = Rand(1,spacey - 1)

  If RoomOverlaps(newx,newy,R.w,R.h) = False
    'Fine: Room can go here
    R.x = newx 
    R.y = newy
    Return True
  Else
    'Otherwise, move out in a spiral from this point.
    'Choose a random sequence of directions forming a spiral
    sdir[1] = Rand(1,4) 
    sdir[4] = 1
    Select sdir[1]
    Case r_NORTH,r_SOUTH
      sdir[2] = Rand(3,4)
      If sdir[1] = r_NORTH Then sdir[3] = r_SOUTH Else sdir[3] = r_NORTH
      If sdir[2] = r_EAST Then sdir[4] = r_WEST Else sdir[4] = r_EAST
    Case r_EAST,r_WEST
      sdir[2] = Rand(1,2)
      If sdir[1] = r_EAST Then sdir[3] = r_WEST Else sdir[3] = r_EAST
      If sdir[2] = r_NORTH Then sdir[4] = r_SOUTH Else sdir[4] = r_NORTH
    End Select


Thanks much,

Well the only thing I see is that shouldn't you be declaring the array like:

local sdir:Int[] = New Int[5]


I'm also noting that sdir[0] is going unused.

Are you sure you're posting that exactly like it's in your program? I can't even compile the line
Local sdir:Int[5] = New Int[5]

Try doing it the way undomiel has it, or even better, if you are not going to be redimentioning the array later, just use
Local sdir:Int[5]