IsoRTS icon indicating copy to clipboard operation
IsoRTS copied to clipboard

Array initialization error in player.cpp

Open Ruckamongus opened this issue 5 years ago • 1 comments

In "player.h" you declare "int friendOrFoo[8];" as a private member on line 71.

In "player.cpp" you initialize it as "this->friendOrFoo[8] ={0};" on line 15. This doesn't do what you think it does because C++ only fills the array on declaration with this syntax. What this actually does is set the 8th element (which is a overflow) to 0 leaving all the other indices with whatever garabage is in RAM.

A number of options are available to fix this:

  • In player.h do "int friendOrFoo[8] = {0};"
  • In player.cpp use std::fill, memset, a for loop, or manually set each array index to 0.

Ruckamongus avatar Aug 04 '21 03:08 Ruckamongus

The friend or foo array is a placeholder for a future expansion to make allying with other players possible. It isn't used right now so that is probably why this hasn't caused any issues. I probably planned to fill the array at player creation with the appropriate value, inherited from the (to be implanted) game menu.
0 = enemy (auto attacks when in range and agressive; has to be defeated to win) 1 = neutral (does not auto attacks when in range and agressive; can be traded with; can be attacked and has to be defeated to win (depending on game mode)) 2 = ally (shared map exploration (might make it a research), can be traded with, cant be attacked, does not have to be defeated to win)

Adressed the issue here: https://github.com/switchboy/IsoRTS/commit/f65eed601825107e031ded2bbc6bb1c091085a69

switchboy avatar Aug 04 '21 07:08 switchboy