[SOLVED] FOR NEXT + ARRAY

Previous topic - Next topic

erico

I think I stumbled into a bug.

I have the following code on my game to RND 4 powerups, if you already own one powerup, it chooses the next free one.
You have 4 slots, each for one powerup specifically.

if all 4 slots are taken (slt[]=1), a fifth powerup always appears.

If I check the slot[] array inside the for/next loop, the game crashes and exit instantaneously.
if I leave the slot[] array outside the for/next everything goes as expected.

I´m using GLB v11.261.

So, this crashes:
Code (glbasic) Select
pfp=RND(3) ;//choose power!
FOR z=0 TO 3
IF slt[pfp]=1 THEN pfp=pfp+1 ;// se slot cheio avanca pro proximo
IF pfp=4 THEN pfp=0 ;// 4 ou -1 nao vale flipa reseta
IF pfp=-1 THEN pfp=3
IF slt[0]=1 AND slt[1]=1 AND slt[2]=1 AND slt[3]=1 ;// tudo cheio seta pow pra avocado ouro
pfp=4
ENDIF
NEXT


This works:
Code (glbasic) Select
pfp=RND(3) ;//choose power!
FOR z=0 TO 3
IF slt[pfp]=1 THEN pfp=pfp+1 ;// se slot cheio avanca pro proximo
IF pfp=4 THEN pfp=0 ;// 4 ou -1 nao vale flipa reseta
IF pfp=-1 THEN pfp=3
NEXT
IF slt[0]=1 AND slt[1]=1 AND slt[2]=1 AND slt[3]=1 ;// tudo cheio seta pow pra avocado ouro
pfp=4
ENDIF


pfp = powerup 0-3
z    = generic timer
slt[]= slots 0-3 (0=empty,1=used)

Ian Price

How have you DIMensioned the slt[] array? What is you maximum number eg DIM slt[4] would only cover arrays number 0,1,2,3 if you use 4 (or more) you'd be going out of bounds.

Are you sure you aren't going out of bounds?

Just to check try changing your first example -
Code (glbasic) Select

IF pfp=4 THEN pfp=0


To

Code (glbasic) Select

IF pfp>=4 THEN pfp=0



The last line could also be responsible -

Code (glbasic) Select

IF slt[0]=1 AND slt[1]=1 AND slt[2]=1 AND slt[3]=1 ;// tudo cheio seta pow pra avocado ouro
pfp=4


You are stating straight away that pfp=4 if conditions are met. After the first loop you might automatically be adding 1 to pfp to make 5 (or more if conditions continue to be true) - again a potential out of bounds problem if you've only allocated 4 or 5 slots to your array.

Increase the slt[] array to 10 and see if it still crashes. The empty arrays won't affect memory usage or the game.
I came. I saw. I played.

fuzzy70

Code (glbasic) Select

IF slt[0]=1 AND slt[1]=1 AND slt[2]=1 AND slt[3]=1 ;// tudo cheio seta pow pra avocado ouro
pfp=4
ENDIF

is what is causing the crash/problem in the 1st example because if that line executes & makes pfp=4 before the z loop has comlpletely finished then
Code (glbasic) Select
IF slt[pfp]=1 THEN pfp=pfp+1
will be 4 & out of bounds of the array.

Lee
"Why don't you just make ten louder and make ten be the top number and make that a little louder?"
- "These go to eleven."

This Is Spinal Tap (1984)

erico

oh god you guys are right! :-[

I´ve messing this code for the last few days on a 24h bases and it eluded me that setting pfp=4 would be out of bound within the for/next.
I´m ashamed hehe.

Thanks guys, good thing there are no bugs!

edit: how do I change the topic name to something like SOLVED?

Ian Price

You don't need to change to solved, as I've done it for you :)

Job's a good'un. :)
I came. I saw. I played.

fuzzy70

I have had similar problems in the past with the program just exiting for what seemed no reason. Running in DEBUG most of the time threw an out of bounds error but even then it is not always obvious the reason for the error  :D

Lee

"Why don't you just make ten louder and make ten be the top number and make that a little louder?"
- "These go to eleven."

This Is Spinal Tap (1984)

Ian Price

I did ask Gernot a couple of years back to throw an error to the user about OUT OF BOUNDS problems, rather than exiting for no obvious reason. Unfortunately it's still not implemented :(

It happens so often that any auto-exit immediately sends my "out of bounds" whiskers twitching! :P
I came. I saw. I played.

fuzzy70

Quote from: Ian Price on 2013-Feb-25
It happens so often that any auto-exit immediately sends my "out of bounds" whiskers twitching! :P

SNAP  :D :D

Lee
"Why don't you just make ten louder and make ten be the top number and make that a little louder?"
- "These go to eleven."

This Is Spinal Tap (1984)

erico

kind of me too, but this one just slipped, I should get some sleep and leave the house a bit :P

About debug mode, I never used :O. (still have to figure it out, can you believe it?)
I tend to print sets of variables on screen to go on checking things.

oldskool I guess.

Kitty Hello

If you run in debug mode, you get an information where it happens and what indices you were using.

erico

Quote from: Kitty Hello on 2013-Feb-28
If you run in debug mode, you get an information where it happens and what indices you were using.

I will give it a go soon on next project, it is just that, um, cogh, gasp, well, see, um, I will get there :)