Arduboy changing a variable value on its own

I’m developing a game, and storing a global int8_t variable to hold the players x & y (player_x, player_y). from the main game loop I call a function that is a mini-game, and in that function I create an array of 15 Rect objects. Before I create this array, player_x has a value of 13, but when I create the array, player_x ends up with a value of 7, even though I don’t change it!
I really can’t explain this! has anyone else had a similar thing?

Global declarations:

Rect rBaddies[15];
Rect rPlayer{64,0,8,7};

//Global Variables
int8_t lives;
int8_t player_x,player_y;
int8_t win=1,lose=0;
int8_t rnd_game=0;
int8_t rnd_step=0;
bool moved=false;

//Graphics
#include "graphics.h"
#include "functions.h"
#include "mini_games.h"

void setup()
{
	arduboy.begin(); // initialize Arduboy
    lives = 3;
    player_x = 10;
    player_y = 30;
    gameState=GameState::Title;
}

Then I call this function, and you can see I have put a print to see the value of player_x before & after, and it gets changed every time from 13 to 7! I’ve tried this on my physical arduboy & on project abe emulator.


    //create Rect arrays for baddies & player heart
    int8_t bad_row=57,play_x=64,play_dead=0;
    //now made the below global, as otherwise seem to run out of dynamic mem
    //Rect rBaddies[15];
    //Rect rPlayer{64,0,8,7};

      //!!!!!!!!global var player_x is correct at 13 or more
      arduboy.clear();
                arduboy.print(player_x);
                arduboy.display();
            delay(3000);

    for(int8_t num_bad=1;num_bad<16;num_bad++){
            rBaddies[num_bad].x=random(0, 120);
            rBaddies[num_bad].y=57;
            rBaddies[num_bad].width=5;
            rBaddies[num_bad].height=7;
    }

          //!!!!!!now player_x is 7 !!!!
      arduboy.clear();
                arduboy.print(player_x);
                arduboy.display();
            delay(3000);

The function called was having issues of running out of dynamic memory I think as I would get blank screens & unresponsive, but after moving the Rect[15] declaration to a global level this then seemed OK. So the function (mini-game) runs OK, but when I get back to the main loop, player_x has been changed!

1 Like

Arrays are zero-based. When you write to the 16th element you are actually out of bounds and writing over other variables.

3 Likes

@filmote Thanks Simon - just changed it from 0-14, and you are absolutely right!!! It’s fixed, that’s been driving me nuts all day, and might explain some other funny behaviour I was having
lifesaver - thanks!

2 Likes

Just out of curiosity … did the compiler give you a warning for that? (Do you have verbose output set for compilation?)

@filmote No warnings in normal logging. Just turned on verbose logging, and no, still no warnings for that & I have set array elements higher than declared.

1 Like

I believe Compiler warnings: would have to be set to All rather than Default.

As far as I’m aware verbose doesn’t necessarily include the extended range of warnings, it just logs information more regularly. I.e. theoretically if extended warnings aren’t enabled then there’s nothing to log because the compiler won’t bother doing the extra checks - it doesn’t need to do them to compile the program so it won’t bother unless asked to do so.