Bug report: player.inv[variable]++; problem

Started by Scorpiorus, Tue 10/06/2003 17:13:20

Previous topic - Next topic

Scorpiorus

I tried to use '++', '--' shortcuts with character[].inv[] variable and seems like there is a bug appeared:

if I pass inventory index as a value (returned value) then all ok:
character[EGO].inv[ 4 ]++;
or
character[EGO].inv[ GetValue() ]++;

...but if it is a variable then the messy occurs:

character[EGO].inv[index]++;

The item index is increased and the next item index+1 is set to 0.

Decreasing '--' makes the similar thing:

character[EGO].inv[index]--;

but the next item can be decreased down to -1.



'+=1' and '-=1' works fine btw.

Also I declared a struct:
struct Cchar {
int inv[10];
};


Cchar Char[10];

and it works without problems.

Chris?

P.S. Here is a testgame (inv_bug.zip)
It shows inventory array for EGO, then processes character[EGO].inv[4]--; Then show the results. There is also some other tests in. I used 2.55 but the bug appears in 2.53 too.

-Cheers

Pumaman


Pumaman

Ah I see what's happened, it remembers the size of the variable when it reads it in (in this case inv[] is a short), but then that cached info gets overwritten by the size of "index" (an int) and causes it to write back 4 bytes of memory. I'll get it fixed.

Scorpiorus

Oh, got it. So that is why the next short is displayed as 0 if the value is greater or equal to 0 and displayed as -1 (FF:FF) if it's less. When I was declaring an array for testings I needed to type it as short or as char to discover the same thing as for inv[].

So other opposite way if an array were typed as int and index was a char, for example, then it would write back only first byte and thereby we should get a char always (so the int variable is changed within 256 different values). As result the next value isn't affected, but the current int most likely wrong. :P

Thanks for the explanation and tracking it down, CJ. :)

-Cheers

Pumaman

Yep, that's right.

Thanks for spotting it, this is the sort of bug which could cause headaches for ages if we couldn't work out a pattern to it :)

SMF spam blocked by CleanTalk