Hi there,
im having this excruciatingly hard to find bug that only gives the error message "Runtime Error 118- Array doesnt exist or subscript out of bounds at line 0"
usually this error say "at line 147" or something like that which makes finding it easy but line 0 is kinda hard to find
i know waht causes the error but cant pinpoint the section of my code as nearly every line uses arrays and there are only 2 different arrays in the game both of which might be the cause.
any help?
loads of


to the one who has some advice
Rem Project: chessinvaders
Rem Created: Wednesday, December 12, 2012
Rem ***** Main Source File *****
sync on : sync rate 60
sw = screen width() : sh = screen height()
REM GRIDDATA
grid_width = 8 : grid_height = 16 : grid_square = 31 : grid_x = sw/2-(grid_width+1)*grid_square/2 : grid_y = sh/2-(grid_height+2)*grid_square/2
grid_rim = 3 : grid_cornerx1# = grid_x+grid_square-grid_rim : grid_cornery1# = grid_y+grid_square-grid_rim : grid_cornerx2# = grid_x+(grid_width+1)*grid_square+grid_rim : grid_cornery2# = grid_y+(grid_height+1)*grid_square+grid_rim
REM DIMS
dim grid(grid_width,grid_height) as UDT_square
dim piece(1000) as UDT_piece
dim state(400)
rem GRIDINITIATE
for i = 1 to grid_width : for j = 1 to grid_height
grid(i,j).occupied = 0 :
if sgn(ceil(j/2.0)-j/2.0) = 1 then grid(i,j).color = rgb(0,10+50*sgn(ceil(i/2.0)-i/2.0),30+150*sgn(ceil(i/2.0)-i/2.0)) else grid(i,j).color = rgb(0,60-50*sgn(ceil(i/2.0)-i/2.0),180-150*sgn(ceil(i/2.0)-i/2.0)) : clr = 1
NEXT : next
rem PIECEINITIATE
cheatmode = 1
rem showtext = 1
turn = 1 : turntime = 120
piece(0).i = x : piece(0).j = y : piece(0).class = 0 : piece(0).cheat = 1
do
REM GG RR AA PP HH II CC SS
REM GRID
a2fillbox grid_cornerx1#,grid_cornery1#,grid_cornerx2#,grid_cornery2#,rgb(70,100,230)
a2fillbox grid_cornerx1#+grid_rim,grid_cornery1#+grid_rim,grid_cornerx2#+-grid_rim,grid_cornery2#-grid_rim,rgb(0,0,0)
for i = 1 to grid_width : for j = 1 to grid_height
a2fillbox grid_x+i*grid_square+1,grid_y+j*grid_square+1,grid_x+(i+1)*grid_square-1,grid_y+(j+1)*grid_square-1,grid(i,j).color
if mx = i and my = j then a2fillbox grid_x+i*grid_square,grid_y+j*grid_square,grid_x+(i+1)*grid_square,grid_y+(j+1)*grid_square,rgb(90,20,10)
if showtext = 1 then text grid_x+i*grid_square+1,grid_y+j*grid_square+1,str$(i)+","+str$(j)
NEXT : next
REM PIECES
for i = 0 to maxpiece
if piece(i).team <> 0 or i = 0
x = grid_x+piece(i).i*grid_square+grid_square/2 : y = grid_y+piece(i).j*grid_square+grid_square/2
if piece(i).class = 1 then a2fillcircle x,y,grid_square/3,rgb((piece(i).team+1)*120,(piece(i).team-1)*-120,255*(1-ceil(i/(1.0+i))))
if piece(i).class = 2 then a2circle x,y,grid_square/3,rgb((piece(i).team+1)*120,(piece(i).team-1)*-120,255*(1-ceil(i/(1.0+i))))
if piece(i).class = 3 then a2cross(x,y,grid_square/3,rgb((piece(i).team+1)*120,(piece(i).team-1)*-120,255*(1-ceil(i/(1.0+i)))))
rem text x,y,str$(piece(i).i)+" "+str$(piece(i).j)
if cheatmode = 1 and piece(i).cheat = 1
a2setblendmode 2,2,1
nx = x-grid_square/2 : ny = y-grid_square/2 : start = (piece(i).movei-1)/-2
if piece(i).team = 1
if piece(i).class < 3
for j = 1 to 16 : a2box nx,ny,nx+grid_square,ny+grid_square,rgb(255-j*16,0,0) : inc nx,piece(i).movei*grid_square : inc ny,piece(i).movej*grid_square : if nx > grid_cornerx2#-grid_square-grid_rim or nx < grid_cornerx1#+grid_square+grid_rim or ny > grid_cornery2#-grid_square-grid_rim or ny < grid_cornery1#+grid_square+grid_rim then exit
next
else
for j = 1+start to 16 : a2box nx,ny,nx+grid_square,ny+grid_square,rgb(255-j*16,0,0) : inc nx,(1+2*sgn(floor(j/2.0)-j/2.0))*grid_square : inc ny,piece(i).movej*grid_square : if nx > grid_cornerx2#-grid_square-grid_rim or nx < grid_cornerx1#-grid_square-2 or ny > grid_cornery2#-grid_square-grid_rim or ny < grid_cornery1#+grid_square+grid_rim then exit
next
endif
endif
if piece(i).team = -1
if piece(i).class < 3
for j = 1 to 16 : a2box nx,ny,nx+grid_square,ny+grid_square,rgb(0,255-j*16,0) : inc nx,piece(i).movei*grid_square : inc ny,piece(i).movej*grid_square : if nx > grid_cornerx2#-grid_square-grid_rim or nx < grid_cornerx1#+grid_square+grid_rim or ny > grid_cornery2#-grid_square-grid_rim or ny < grid_cornery1#+grid_square+grid_rim then exit
next
else
for j = 1+start to 16 : a2box nx,ny,nx+grid_square,ny+grid_square,rgb(0,255-j*16,0) : inc nx,(1+2*sgn(floor(j/2.0)-j/2.0))*grid_square : inc ny,piece(i).movej*grid_square : if nx > grid_cornerx2#-grid_square-grid_rim or nx < grid_cornerx1#+grid_square+grid_rim or ny > grid_cornery2#-grid_square-grid_rim or ny < grid_cornery1#+grid_square+grid_rim then exit
next
endif
endif
a2setblendmode 2,1,1 : a2dot 0,0,rgb(0,0,0)
endif
endif
NEXT
REM GG AA MM EE MM EE CC HH AA NN II CC SS
REM PIECELOOP
if delay < 0
turn = turn*-1
for i = 0 to maxpiece
eat = 0
if piece(i).team <> 0 or i = 0
if turn = piece(i).team or i = 0
if piece(i).class = 1 and i <> 0
if piece(i).i < grid_width-1
if piece(grid(piece(i).i+1,piece(i).j+piece(i).movej).occupied).team = -turn then eat = 1
endif
if piece(i).i > 1
if piece(grid(piece(i).i-1,piece(i).j+piece(i).movej).occupied).team = -turn then eat = -1
endif
if eat = 0
oi = piece(i).i : oj = piece(i).j : grid(oi,oj).occupied = 0
inc piece(i).i,piece(i).movei : inc piece(i).j,piece(i).movej
else
oi = piece(i).i : oj = piece(i).j : grid(oi,oj).occupied = 0
inc piece(i).i,eat : inc piece(i).j,piece(i).movej
endif
endif
if piece(i).class = 2
if piece(i).i >= grid_width or piece(i).i <= 1
if piece(i).team = -1
if piece(i).i < grid_width or piece(i).j < grid_height-1 then piece(i).movei = piece(i).movei*-1
endif
if piece(i).team = 1
if piece(i).i > 1 or piece(i).j > 2 then piece(i).movei = piece(i).movei*-1
endif
endif
if i <> 0
oi = piece(i).i : oj = piece(i).j : grid(oi,oj).occupied = 0
inc piece(i).i,piece(i).movei : inc piece(i).j,piece(i).movej
if piece(i).i > grid_width or piece(i).i < 1 then dec piece(i).i,piece(i).movei/2 : dec piece(i).j,piece(i).movej/2
endif
ENDIF
if piece(i).class = 3
if piece(i).i = grid_width or piece(i).i = 1
piece(i).movei = piece(i).movei/abs(piece(i).movei)*sgn(1.0/(piece(i).i+piece(i).movei)-1.0/8.0)
else
piece(i).movei = piece(i).movei*-1
endif
if i <> 0
oi = piece(i).i : oj = piece(i).j : grid(oi,oj).occupied = 0
inc piece(i).i,piece(i).movei : inc piece(i).j,piece(i).movej
endif
ENDIF
if piece(i).j <= grid_height and piece(i).j >= 1 and i <> 0
if grid(piece(i).i,piece(i).j).occupied > 0
occupant = grid(piece(i).i,piece(i).j).occupied
if piece(occupant).team = -turn
if turn = -1 then kills1$ = kills1$+str$(piece(occupant).class*piece(occupant).team)
if turn = 1 then kills2$ = kills2$+str$(piece(occupant).class*piece(occupant).team)
piece(occupant).team = 0 : piece(occupant).cheat = 0
else
if occupant < i
if piece(occupant).class <= piece(i).class
if turn = -1 then kills1$ = kills1$+str$(piece(occupant).class*piece(occupant).team)
if turn = 1 then kills2$ = kills2$+str$(piece(occupant).class*piece(occupant).team)
piece(occupant).team = 0 : piece(occupant).cheat = 0
else
if turn = -1 then kills1$ = kills1$+str$(piece(i).class*piece(i).team)
if turn = 1 then kills2$ = kills2$+str$(piece(i).class*piece(i).team)
piece(i).team = 0 : piece(i).cheat = 0
endif
endif
endif
endif
endif
if piece(i).j > grid_height or piece(i).j <= 0
piece(i).team = 0 : piece(i).cheat = 0
else
grid(piece(i).i,piece(i).j).occupied = i
endif
if piece(i).team <> 0 then piece(i).cheat = 1
endif
endif
next
delay = turntime
endif : dec delay
piece(0).i = mx : piece(0).j = my
REM MOUSE and MISC
mx = (mousex()-grid_x)/grid_square : my = (mousey()-grid_y)/grid_square
if mx > 0 and mx < grid_width+1 and my > 0 and my < grid_height+1
hide mouse : ingrid = 1
else show mouse : ingrid = 0
endif
if ingrid = 1
if grid(mx,my).occupied = 0
if keypress(-1)= 2
if my <= 2 then maxpiece = makepiece(mx,my,1,1,maxpiece)
if my > grid_height-2 then maxpiece = makepiece(mx,my,1,-1,maxpiece)
ENDIF
if keypress(-2)= 2
if my <= 2 then maxpiece = makepiece(mx,my,2,1,maxpiece)
if my > grid_height-2 then maxpiece = makepiece(mx,my,2,-1,maxpiece)
ENDIF
if keypress(-4)= 2
if my <= 2 then maxpiece = makepiece(mx,my,3,1,maxpiece)
if my > grid_height-2 then maxpiece = makepiece(mx,my,3,-1,maxpiece)
ENDIF
endif
endif
for i = 1 to fast len(kills1$)
value = val(mid$(kills1$,i))
if value = 0 then inc i : value = -val(mid$(kills1$,i)) : dec var,grid_square/4.0
if abs(value) = 1 then a2fillcircle 50+i*grid_square/4.0+var,25,grid_square/9.0,rgb((sgn(value)+1)*120,(sgn(value)-1)*-120,0)
if abs(value) = 2 then a2circle 50+i*grid_square/4.0+var,25,grid_square/9.0,rgb((sgn(value)+1)*120,(sgn(value)-1)*-120,0)
if abs(value) = 3 then a2cross(50+i*grid_square/4.0+var,25,grid_square/9.0,rgb((sgn(value)+1)*120,(sgn(value)-1)*-120,0))
NEXT : var = 0
for i = 1 to fast len(kills2$)
value = val(mid$(kills2$,i))
if value = 0 then inc i : value = -val(mid$(kills2$,i)) : dec var,grid_square/4.0
if abs(value) = 1 then a2fillcircle 50+i*grid_square/4.0+var,25+grid_square*2,grid_square/9.0,rgb((sgn(value)+1)*120,(sgn(value)-1)*-120,0)
if abs(value) = 2 then a2circle 50+i*grid_square/4.0+var,25+grid_square*2,grid_square/9.0,rgb((sgn(value)+1)*120,(sgn(value)-1)*-120,0)
if abs(value) = 3 then a2cross(50+i*grid_square/4.0+var,25+grid_square*2,grid_square/9.0,rgb((sgn(value)+1)*120,(sgn(value)-1)*-120,0))
NEXT : var = 0
if scancode() = 2 then piece(0).class = 1 : piece(0).movei = 0 : piece(0).movej = -turn
if scancode() = 3 then piece(0).class = 2 : piece(0).movei = -turn*2 : piece(0).movej = -turn*2
if scancode() = 4 then piece(0).class = 3 : piece(0).movei = -turn : piece(0).movej = -turn*2
print "MX :"+str$(mx)+ " MY :" +str$(my)
sync
cls
LOOP
function a2cross(x#,y#,r#,color as dword)
a2line x#-r#,y#,x#+r#,y#,color : a2line x#,y#-r#,x#,y#+r#,color
ENDFUNCTION
function makepiece(x,y,class,team,maxpiece)
for i = 1 to 1000 : if piece(i).team = 0 then n = i : exit
next
piece(n).team = team : piece(n).i = x : piece(n).j = y : piece(n).class = class : grid(x,y).occupied = n
if class = 1
piece(n).movei = 0
piece(n).movej = team
endif
if class = 2
piece(n).movei = team*2
piece(n).movej = team*2
endif
if class = 3
piece(n).movei = team
piece(n).movej = team*2
endif
if n > maxpiece then maxpiece = n
ENDFUNCTION maxpiece
function button(x1,y1,x2,y2)
if mousex() > x1 and mousex() < x2 and mousey() > y1 and mousey() < y2
selected = 1
if mouseclick()=1 then selected = 2
if mouseclick()=2 then selected = 3
endif
endfunction selected
function colbox(x1,y1,x2,y2,x#,y#)
if x# > x1 and x# < x2 and y# > y1 and y# < y2 then col = 1
endfunction col
function keypress(key)
if key > 0
if keystate(key) = 1
accept = 1
if state(key) = 0
accept = 2
state(key) = 1
endif
ENDIF
if keystate(key) = 0 then state(key) = 0
endif
if key < 0
if mouseclick() = -key
accept = 1
if state(400+key) = 0
accept = 2
state(400+key) = 1
endif
endif
if mouseclick() = 0 then state(400+key) = 0
ENDIF
ENDFUNCTION accept
type UDT_square
occupied
color as dword : clr
border
endtype
type UDT_piece
i : j
movei : movej
class
team
cheat
endtype
code might be incomprehensible, requires advanced 2d (a2d) plugin and maybe also Matr1x utils