Subscript value out of Bounds
- Posted by Icy_Viking Mar 26, 2023
- 592 views
Hello all,
I have finished wrapping Raylib 4.5. However I am having a subscript value out of bounds issue when trying to make the Camera 2D example.
Error: subscript value 0 is out of bounds, assigning to a sequence of length 1 - in subscript #1 of 'buildings'
Wrapper code for Raylib 4.5 here: https://github.com/gAndy50/EuRayLib4
Working on this example kinda shows the short-comings of the FFI wrapper. Having a memstruct type would make it easier to deal with structs. Instead of having to type variable_name[1][2] etc. Or using enum, which kinda helps allievate the issue.
include raylib.e constant MAX_BUILDINGS = 100 constant WIDTH = 800, HEIGHT = 450 InitWindow(WIDTH,HEIGHT,"2D Camera Example") sequence player = {400,280,40,40} --Rectangle sequence buildings = {} --Rectangle sequence buildColors = {} --Color enum player_x = 0, player_y, player_width, player_height enum building_x = 0, building_y, building_width, building_height enum cam_offset_x = 0, cam_offset_y enum cam_target_x = 0, cam_target_y integer spacing = 0 for i = 0 to MAX_BUILDINGS do buildings[i] = GetRandomValue(50,200) --error is coming from this line buildings[i] = GetRandomValue(100,800) buildings[i] = HEIGHT - 130.0 - buildings[i][building_height] buildings[i][building_x] = -6000.0 + spacing spacing += buildings[i][building_width] buildColors = {GetRandomValue(200,240), GetRandomValue(200,240), GetRandomValue(200,250), 255} end for sequence cam = {0} cam[cam_target_x+cam_target_y] = {player[player_x] + 20.0, player[player_y] + 20.0} cam[cam_offset_x+cam_offset_y] = {WIDTH / 2.0, HEIGHT / 2.0} cam[3] = 0.0 --rotation cam[4] = 1.0 --zoom SetTargetFPS(60) while not WindowShouldClose() do --player movement if IsKeyDown(KEY_RIGHT) then player[player_x] += 2 elsif IsKeyDown(KEY_LEFT) then player[player_x] -= 2 end if --camera follow player cam[cam_target_x+cam_target_y] = {player[player_x] + 20, player[player_y] + 20} --camera rotation controls if IsKeyDown(KEY_A) then cam[3] -= 1 elsif IsKeyDown(KEY_S) then cam[3] += 1 end if if cam[3] > 40 then cam[3] = 40 elsif cam[3] < - 40 then cam[3] = -40 end if --camera zoom cam[4] += GetMouseWheelMove() * 0.05 if cam[4] > 3.0 then cam[4] = 3.0 elsif cam[4] < 0.1 then cam[4] = 0.1 end if --reset camera rotation and zoom if IsKeyPressed(KEY_R) then cam[3] = 1.0 cam[4] = 0.0 end if BeginDrawing() ClearBackground(RAYWHITE) BeginMode2D(cam) DrawRectangle(-6000,320,13000,8000,DARKGRAY) for i = 0 to MAX_BUILDINGS do DrawRectangleRec(buildings[i],buildColors[i]) end for DrawRectangleRec(player,RED) DrawLine(cam[cam_target_x], - HEIGHT*10, cam[cam_target_x], HEIGHT*10, GREEN) DrawLine(-WIDTH*10, cam[cam_target_y], WIDTH*10, cam[cam_target_y], GREEN) EndMode2D() DrawText("SCREEN AREA",640,10,20,RED) DrawRectangle(0,0,WIDTH,5,RED) DrawRectangle(0,5,5,HEIGHT - 10,RED) DrawRectangle(WIDTH - 5,5,5, HEIGHT - 10,RED) DrawRectangle(0,HEIGHT - 5, WIDTH,5,RED) DrawRectangle(10,10,250,113,Fade(SKYBLUE,0.5)) DrawRectangleLines(10,10,250,113,BLUE) EndDrawing() end while CloseWindow()