Programming C, C++, Java, PHP, Ruby, Turing, VB
Computer Science Canada 
Programming C, C++, Java, PHP, Ruby, Turing, VB  

Username:   Password: 
 RegisterRegister   
 Array subscript out of range error! I'm confused why!
Index -> Programming, Turing -> Turing Help
View previous topic Printable versionDownload TopicSubscribe to this topicPrivate MessagesRefresh page View next topic
Author Message
Hexzeus




PostPosted: Wed Jun 14, 2017 8:09 pm   Post subject: 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



unnamedGame.rar
 Description:
turing file + assets

Download
 Filename:  unnamedGame.rar
 Filesize:  7.9 KB
 Downloaded:  17 Time(s)


unnamedGame.rar
 Description:
turing file + pictures used

Download
 Filename:  unnamedGame.rar
 Filesize:  7.9 KB
 Downloaded:  14 Time(s)

Sponsor
Sponsor
Sponsor
sponsor
Hexzeus




PostPosted: Wed Jun 14, 2017 8:10 pm   Post subject: RE:Array subscript out of range error! I\'m confused why!

oops, i didnt mean to add the .rar twice...
Insectoid




PostPosted: Wed Jun 14, 2017 8:23 pm   Post subject: 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




PostPosted: Thu Jun 15, 2017 6:26 am   Post subject: Re: RE:Array subscript out of range error! I\'m confused why!

Insectoid @ Wed Jun 14, 2017 8:23 pm wrote:
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




PostPosted: Thu Jun 15, 2017 9:57 am   Post subject: RE:Array subscript out of range error! I\'m confused why!

That is fine.
Hexzeus




PostPosted: Thu Jun 15, 2017 3:38 pm   Post subject: Re: RE:Array subscript out of range error! I\'m confused why!

Insectoid @ Thu Jun 15, 2017 9:57 am wrote:
That is fine.


Turing:

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) <= 30 then
                for n : m .. numArrows - 1
                    arrowDirection (n) := arrowDirection (n + 1)
                    arrowImg (n) := arrowImg (n + 1)
                    arrowX (n) := arrowX (n + 1)
                    arrowY (n) := arrowY (n + 1)
                    arrowSpeed (n) := arrowSpeed (n + 1)
                end for
                numArrows := numArrows - 1
                new arrowDirection, numArrows %resizes arrays
                new arrowX, numArrows
                new arrowY, numArrows
                new arrowSpeed, numArrows
                new arrowImg, numArrows
            end if
        end for
    end loop
end arrowHit

process coinCheck
    loop
        if Math.Distance (x + 32, y + 32, coinX + 16, coinY + 16) <= 20 then %checks if the distance between the middle of the player and the middle of the coin is less than 20 px
            score := score + 1 %if so, add 1 to score
            coinX := Rand.Int (100, maxx - 100) %sets new x and y pos of next coin
            coinY := Rand.Int (100, maxy - 100)
        end if
    end loop
end coinCheck

process addArrow
    loop
        delay (2000) %how often a new arrow gets added
        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 (numArrows) := "north"
            arrowImg (numArrows) := arrowUp
            arrowX (numArrows) := Rand.Int (107, maxx - 107)
            arrowY (numArrows) := -16
            arrowSpeed (numArrows) := Rand.Int (3, 5)
        elsif randArrowDirection = 2 then
            arrowDirection (numArrows) := "east"
            arrowImg (numArrows) := arrowRight
            arrowX (numArrows) := -16
            arrowY (numArrows) := Rand.Int (107, maxy - 107)
            arrowSpeed (numArrows) := Rand.Int (3, 5)
        elsif randArrowDirection = 3 then
            arrowDirection (numArrows) := "south"
            arrowImg (numArrows) := arrowDown
            arrowX (numArrows) := Rand.Int (107, maxx - 107)
            arrowY (numArrows) := maxy
            arrowSpeed (numArrows) := Rand.Int (3, 5)
        elsif randArrowDirection = 4 then
            arrowDirection (numArrows) := "west"
            arrowImg (numArrows) := arrowLeft
            arrowX (numArrows) := maxx
            arrowY (numArrows) := Rand.Int (107, maxy - 107)
            arrowSpeed (numArrows) := Rand.Int (3, 5)
        end if
    end loop
end addArrow

process arrowReset
    loop
        for k : 1 .. numArrows
            if (arrowDirection (k) = "north" and arrowY (k) > maxy) or (arrowDirection (k) = "east" and arrowX (k) > 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




PostPosted: Thu Jun 15, 2017 5:43 pm   Post subject: 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




PostPosted: Thu Jun 15, 2017 6:10 pm   Post subject: RE:Array subscript out of range error! I\'m confused why!

ok thanks
Display posts from previous:   
   Index -> Programming, Turing -> Turing Help
View previous topic Tell A FriendPrintable versionDownload TopicSubscribe to this topicPrivate MessagesRefresh page View next topic

Page 1 of 1  [ 8 Posts ]
Jump to:   


Style:  
Search: