Page 1 of 1

Value changing on it's own??

Posted: Mon Oct 03, 2011 6:08 am
by bludshot
I have a variable that is getting its value changed somehow and I don't understand it.

In bg_misc.c I have added a typedef like this:

Code: Select all

wpinfo_t bg_weaponlist[] ={
  {  //WP_NONE, //0 Used for misc counts, rounds are for burst count.
    "",
    {0}, //weapMode
    {0},                // numClips ammo that fits in the weapon
    {0},             // rounds
    ""
  },
  { //WP_KNIFE, //1
    "icons/ammo/kbar",
    {0}, //weapMode
    {-1},
    {5},
            "models/weapons2/knife/"
  },
  {// WP_BERETTA, //2
    "icons/ammo/beretta",
    {0}, //weapMode
    {0},                // numClips ammo that fits in the weapon
    {0},             // rounds
    "models/weapons2/beretta/"
  },
... And so on for the rest of the weapons.
Then in PM_Weapon() I want to check the value of the numClips of a weapon in order to take an action if the value is 0 (if they have no clips).

However, when I check the value of numClips from the type above, it gives unreliable data, sometimes telling the truth about how many clips there are, and other times saying 0 for some reason.

So I did a print statement:

Code: Select all

Com_Printf("HEVAL: %i HEs:%i Client: %i\n", WP_HE, bg_weaponlist[WP_HE].numClips[pm->ps->clientNum], pm->ps->clientNum );
(I even checked to make sure the value of WP_HE constant wasn't changing since the behaviour makes no sense)

What this prints out is:
HEVAL: 15 HEs:2 Client: 0
HEVAL: 15 HEs:0 Client: 0
HEVAL: 15 HEs:2 Client: 0
HEVAL: 15 HEs:0 Client: 0
HEVAL: 15 HEs:0 Client: 0
HEVAL: 15 HEs:2 Client: 0
HEVAL: 15 HEs:0 Client: 0
HEVAL: 15 HEs:0 Client: 0
HEVAL: 15 HEs:0 Client: 0
HEVAL: 15 HEs:2 Client: 0
HEVAL: 15 HEs:0 Client: 0
HEVAL: 15 HEs:0 Client: 0
HEVAL: 15 HEs:0 Client: 0
HEVAL: 15 HEs:0 Client: 0
HEVAL: 15 HEs:2 Client: 0
HEVAL: 15 HEs:0 Client: 0
HEVAL: 15 HEs:2 Client: 0
HEVAL: 15 HEs:0 Client: 0
HEVAL: 15 HEs:0 Client: 0
HEVAL: 15 HEs:2 Client: 0
HEVAL: 15 HEs:0 Client: 0
HEVAL: 15 HEs:0 Client: 0
HEVAL: 15 HEs:0 Client: 0
HEVAL: 15 HEs:2 Client: 0
and so on.

I put a debug print statement on every place in they code that sets or changes the value of any bg_weaponlist[].numClips[], and none of them are happening to change it to 0 or 2.

You can see that it has a pattern, where it is 0 once, then twice, then 3, 4, 5 times, then back to once.

This is baffling me. Anybody have any idea what's up with it? (I can post the SVN of all the code once I commit some recent stuff)

Re: Value changing on it's own??

Posted: Mon Oct 03, 2011 5:33 pm
by ^misantropia^
There's not enough information here to really tell (for instance, what does wpinfo_t look like?) but one thing that might be tripping you up is that PM_Weapon() runs on both the server and the client. Unless both sides of the connection meticulously update their copy of the numClips variable, you're going to see discrepancies.

Re: Value changing on it's own??

Posted: Mon Oct 03, 2011 6:57 pm
by bludshot
Yeah but neither the client nor the server is updating the numclips in the above output example. It's changing from 2 to 0 and back (and in that increasing pattern of 0's) on it's own.

Here is wpinfo_t

typedef struct wpinfo_s {
char *ammoIcon;
int weapMode[MAX_CLIENTS];
int numClips[MAX_CLIENTS];
int rounds[MAX_CLIENTS];
char *modelPath;
} wpinfo_t;


You say the client and the server each have their own copy of numclips? When I do bg_weaponlist[WP_HE].numClips[pm->ps->clientNum] in PM_Weapon in bg_pmove.c, which copy does that refer to? And how can I check the other copy?

Re: Value changing on it's own??

Posted: Tue Oct 04, 2011 1:56 pm
by ^misantropia^
When I do bg_weaponlist[WP_HE].numClips[pm->ps->clientNum] in PM_Weapon in bg_pmove.c, which copy does that refer to?
Either and you can't really tell which. The pmove code is run by both server and client.
And how can I check the other copy?
You can't, not directly anyway. The server and client may run on different machines.

Re: Value changing on it's own??

Posted: Wed Oct 05, 2011 11:20 pm
by bludshot
So what does that mean? Does that mean we should move it out of BG and into G? Does it mean that what we were trying to use it for (and using it in that way) is just not something you should do?

Re: Value changing on it's own??

Posted: Thu Oct 06, 2011 12:32 pm
by ^misantropia^
I assume numClips is a per-player thing? client->ps.stats[STAT_YOURSTAT] seems like the perfect place for it.