
-----------------------------------
Hexzeus
Wed Jun 14, 2017 8:09 pm

Array subscript out of range error! I'm confused why!
-----------------------------------
What is it you are trying to achieve?
Remove data from a specific index in an array by overwriting all the indexes that come after in the array including the starting index, then resizing the array down 1 when an arrow hits the player. So basically i'm trying to take a specific index, "deleting" it, then shifting down the rest of the array to fill in the gap.


What is the problem you are having?
I always get an error saying that the array subscript is out of range in either my "arrowReset" process or my "arrowHit" process. I understand what the error means but i dont know how it was created.


Please specify what version of Turing you are using
4.1.1

-----------------------------------
Hexzeus
Wed Jun 14, 2017 8:10 pm

RE:Array subscript out of range error! I\'m confused why!
-----------------------------------
oops, i didnt mean to add the .rar twice...

-----------------------------------
Insectoid
Wed Jun 14, 2017 8:23 pm

RE:Array subscript out of range error! I\'m confused why!
-----------------------------------
Can you copy & paste your code here? I generally post from my phone, which makes opening rar files a bit of a pain.

-----------------------------------
Hexzeus
Thu Jun 15, 2017 6:26 am

Re: RE:Array subscript out of range error! I\'m confused why!
-----------------------------------
Can you copy & paste your code here? I generally post from my phone, which makes opening rar files a bit of a pain.
I could but there is like 200 lines.

-----------------------------------
Insectoid
Thu Jun 15, 2017 9:57 am

RE:Array subscript out of range error! I\'m confused why!
-----------------------------------
That is fine.

-----------------------------------
Hexzeus
Thu Jun 15, 2017 3:38 pm

Re: RE:Array subscript out of range error! I\'m confused why!
-----------------------------------
That is fine.


setscreen ("graphics:900;550")

%--------------------------------------------------

const SPEED : int := 5 %player speed

var playerRight : int := Pic.FileNew ("assets/Hero.gif")
var playerLeft : int := Pic.FileNew ("assets/HeroFlip.gif")
var player : int := playerRight %player's either left or right, default:right
var levelOne : int := Pic.FileNew ("assets/grassland.gif")
var coin : int := Pic.FileNew ("assets/coin.gif")
var arrowDown : int := Pic.FileNew ("assets/arrowDown.gif")
var arrowUp : int := Pic.FileNew ("assets/arrowUp.gif")
var arrowLeft : int := Pic.FileNew ("assets/arrowLeft.gif")
var arrowRight : int := Pic.FileNew ("assets/arrowRight.gif")
var x : int := maxx div 2 - 32 %default x and y pos pf player
var y : int := maxy div 2 - 32
var coinX : int := Rand.Int (100, maxx - 100) %sets random x and y pos of fitst coin
var coinY : int := Rand.Int (100, maxy - 100)
var score : int := 0 %score starts at zero
var numArrows : int := 1
var randArrowDirection : int

var chars : array char of boolean %Input.KeyDown information

var arrowX : flexible array 1 .. numArrows of int
var arrowY : flexible array 1 .. numArrows of int
var arrowSpeed : flexible array 1 .. numArrows of int
var arrowDirection : flexible array 1 .. numArrows of string
var arrowImg : flexible array 1 .. numArrows of int
arrowX (1) := Rand.Int (107, maxx - 107) %sets first arrow's direction, x, y, and speed
arrowY (1) := maxy
arrowSpeed (1) := 3
arrowDirection (1) := "south"
arrowImg (1) := arrowDown
for i : 2 .. 5
    numArrows := numArrows + 1 %add 1 to max amount of arrows in array
    new arrowDirection, numArrows     %resizes arrays
    new arrowX, numArrows
    new arrowY, numArrows
    new arrowSpeed, numArrows
    new arrowImg, numArrows
    randArrowDirection := Rand.Int (1, 4)     %sets new arrow to a random direction, x, y, and speed
    if randArrowDirection = 1 then
        arrowDirection (i) := "north"
        arrowImg (i) := arrowUp
        arrowX (i) := Rand.Int (107, maxx - 107)
        arrowY (i) := -16
        arrowSpeed (i) := Rand.Int (3, 5)
    elsif randArrowDirection = 2 then
        arrowDirection (i) := "east"
        arrowImg (i) := arrowRight
        arrowX (i) := -16
        arrowY (i) := Rand.Int (107, maxy - 107)
        arrowSpeed (i) := Rand.Int (3, 5)
    elsif randArrowDirection = 3 then
        arrowDirection (i) := "south"
        arrowImg (i) := arrowDown
        arrowX (i) := Rand.Int (107, maxx - 107)
        arrowY (i) := maxy
        arrowSpeed (i) := Rand.Int (3, 5)
    elsif randArrowDirection = 4 then
        arrowDirection (i) := "west"
        arrowImg (i) := arrowLeft
        arrowX (i) := maxx
        arrowY (i) := Rand.Int (107, maxy - 107)
        arrowSpeed (i) := Rand.Int (3, 5)
    end if
end for

%--------------------------------------------------

process displayScore %used for testing score
    loop
        Input.KeyDown (chars)
        if chars (' ') then
            put score
            delay (10)
        end if
    end loop
end displayScore

process displayArrow %used for testing arrow count
    loop
        Input.KeyDown (chars)
        if chars ('p') then
            put numArrows
            delay (10)
        end if
    end loop
end displayArrow

process arrowHit
    loop
        for m : 1 .. numArrows
            if Math.Distance (x + 32, y + 32, arrowX (m) + 8, arrowY (m) + 8)  maxx) or (arrowDirection (k) = "south" and arrowY (k) < -16)
                    or (arrowDirection (k) = "west" and arrowX (k) < -16) or (numArrows = 0) then
                %if an arrow going in a specif direction reaches the boundaries, set new random direction, x, y, and speed
                randArrowDirection := Rand.Int (1, 4)
                if randArrowDirection = 1 then
                    arrowDirection (k) := "north"
                    arrowImg (k) := arrowUp
                    arrowX (k) := Rand.Int (107, maxx - 107)
                    arrowY (k) := -16
                    arrowSpeed (k) := Rand.Int (3, 5)
                elsif randArrowDirection = 2 then
                    arrowDirection (k) := "east"
                    arrowImg (k) := arrowRight
                    arrowX (k) := -16
                    arrowY (k) := Rand.Int (107, maxy - 107)
                    arrowSpeed (k) := Rand.Int (3, 5)
                elsif randArrowDirection = 3 then
                    arrowDirection (k) := "south"
                    arrowImg (k) := arrowDown
                    arrowX (k) := Rand.Int (107, maxx - 107)
                    arrowY (k) := maxy
                    arrowSpeed (k) := Rand.Int (3, 5)
                elsif randArrowDirection = 4 then
                    arrowDirection (k) := "west"
                    arrowImg (k) := arrowLeft
                    arrowX (k) := maxx
                    arrowY (k) := Rand.Int (107, maxy - 107)
                    arrowSpeed (k) := Rand.Int (3, 5)
                end if
            end if
        end for
    end loop
end arrowReset

procedure move %movement, 8 directions
    Input.KeyDown (chars)
    if chars ('w') then
        y := y + SPEED
    end if
    if chars ('a') then
        x := x - SPEED
        player := playerLeft
    end if
    if chars ('s') then
        y := y - SPEED
    end if
    if chars ('d') then
        x := x + SPEED
        player := playerRight
    end if
end move

procedure arrowDraw %draws all arrows
    for i : 1 .. numArrows
        Pic.Draw (arrowImg (i), arrowX (i), arrowY (i), picMerge)
    end for
end arrowDraw

procedure arrowMove %moves all arrows
    for j : 1 .. numArrows
        if arrowDirection (j) = "north" then
            arrowY (j) := arrowY (j) + arrowSpeed (j)
        elsif arrowDirection (j) = "east" then
            arrowX (j) := arrowX (j) + arrowSpeed (j)
        elsif arrowDirection (j) = "south" then
            arrowY (j) := arrowY (j) - arrowSpeed (j)
        elsif arrowDirection (j) = "west" then
            arrowX (j) := arrowX (j) - arrowSpeed (j)
        end if
    end for
end arrowMove

procedure draw %all draws here to be organized
    Pic.Draw (levelOne, 0, 0, picMerge)
    Pic.Draw (coin, coinX, coinY, picMerge)
    Pic.Draw (player, x, y, picMerge)
    arrowDraw
    arrowMove
    move
    View.Update
    delay (15)
end draw

%---------------------------
%starts all functions to begin the game
fork displayScore
fork displayArrow
fork coinCheck
fork arrowReset
fork arrowHit
fork addArrow
loop
    draw
end loop

%--------------------------



-----------------------------------
Insectoid
Thu Jun 15, 2017 5:43 pm

RE:Array subscript out of range error! I\'m confused why!
-----------------------------------
Uh, there's a very good chance your issue is caused by your use of processes. Processes don't execute at the same time, they execute out of order. You should have zero processes. None. You should only have procedures and functions. Processes shouldn't even be taught in school. 

If your code executes out of order, you can wind up in a situation where a process wants to look at arrow n, but then it pauses to let your arrowHit process run, which deletes arrow n. The first process then resumes and tries to look at arrow n, but can't, because it's been deleted. The program then crashes.

There is no point even trying to debug the code when unpredictable things like this can happen. You absolutely must rewrite this to completely eliminate processes.

-----------------------------------
Hexzeus
Thu Jun 15, 2017 6:10 pm

RE:Array subscript out of range error! I\'m confused why!
-----------------------------------
ok thanks
