This is driving me crazy! I can't seem to figure out why the ammo is not being changed! It is staying at 0.
Here is the code:
Shooter.h:
Shooter.cpp:
Main.cpp:
When I run the program, I see the two ships...like I should. I can move them around...like I should. I can "fire" the weapon because it allows me to see the ammo count going down. ammo[x] should be valid, but will not change! Don't mind all of those pesky // everywhere...still trying to track down the bug.
I think it's a bit unfair to let us look through all your source. You should play around with your debugger, adding in-code logging messages and reduce the code to the minimum part that causes you problems.
AFTER THAT you can post here! And please a bit more specific than "the ammo is not being changed! It is staying at 0."
Sorry if I am too harsh.
And use TRACE to trace the execution
I HAVE tryed to find the bug! I'm not one to just post as soon as I run into problems...I would like to find this on my own, but I can't seem to think of a reason that would make it act the way it is!
To be more specific:
1. Player class is created in main AFTER allegro is initialized, because it holds BITMAP data.
2. Player class creates the Weapon class.
3. The Weapon class creates an array of Ammo class.
4. The Player class is manipulated in main to allow the user to "fly" around and "fire" it's weapons.
5. The Player class delegates the Update function to Weapon who delegates to Ammo.
6. The Player class delegates the Fire function to Weapon who delegates to Ammo.
All seems to work just fine. The Player class contains valid data. The Weapons class contains valid data. The Ammo class contains valid data. It acts as though Weapon->Update(...) and / or Weapon->Fire(...) are not calling Ammo's functions. So, to rule this out, I tryed to just manually set the values in main....still, ammo->x, ammo->y are set to 0...even thought I manually set them to 50 with:
pLocalPlayer->pWeapon->ammo[0]->x = 50.0f; pLocalPlayer->pWeapon->ammo[0]->y = 50.0f;
This may have been cut out of the above code...as I am still trying to solve this. I keep changing the code around to minimize the possible flaws...but I can't seem to think of WHY it is acting this way anyway! I mean, if ammo[0] is valid, pWeapon is valid, and pLocalPlayer is valid....then calling: pLocalPlayer->pWeapon->ammo[0]->x = 50.0f should actually set ammo[0]->x to 50.0f! But when I go to retreive the values of these...I get 0!
the ammo is not being changed! It is staying at 0
it allows me to see the ammo count going down.
You seem to be confused.
ammo[x] should be valid, but will not change!
staying at 0, going down, or no change?
One advice: set MAX_AMMO at 1, and fix until it works with one bullet.
The most I can comment is:
| 1 | Ammo ammo[MAX_AMMO]; // OK, valid indices are 0 to MAX_AMMO - 1 |
| 2 | (...) |
| 3 | maxAmmo = MAX_AMMO-1; // OK, maxAmmo points to the top of your array |
| 4 | (...) |
| 5 | curAmmo = maxAmmo; // OK, curAmmo points to the top of the array too |
| 6 | // I assume value -1 is for "too many shots in air to shoot now", and 0 to maxAmmo for 'the next bullet to shoot' |
| 7 | (...) |
| 8 | for ( int i = 0; i < maxAmmo; i++ ) // these " i < maxAmmo" are ALL off by one, you should check i <= maxAmmo |
| 9 | (...) |
| 10 | // in void Weapon::Fire( void ) |
| 11 | if ( curAmmo <= 0 ) |
| 12 | { |
| 13 | curAmmo = 0; // is wrong: you're "recycling" ammo[0] for no reason! just don't shoot : return. |
| 14 | return; |
| 15 | } |
| 16 | (...) |
| 17 | for ( int i = 0; i < maxAmmo; i++ ) |
| 18 | { |
| 19 | ammo<i>.bActive = true; |
| 20 | ammo<i>.bFired = true; |
| 21 | ammo<i>.bVisible = true; |
| 22 | } // this is all wrong, I hope this is for "debugging" |
| 23 | //pAmmo[curAmmo]->bActive = true; |
| 24 | //pAmmo[curAmmo]->bFired = true; // this is the part to keep |
| 25 | //pAmmo[curAmmo]->bVisible = true; // but don't forget the x, y etc. |
| 26 | curAmmo--; |
Yeah...there is ALOT of debugging crap in that code! And I am just using one of Ammo class...pLocalPlayer->pWeapon->ammo[0] to be exact!
Quote:
the ammo is not being changed! It is staying at 0
Quote:
it allows me to see the ammo count going down.
You seem to be confused.
Quote:
ammo[x] should be valid, but will not change!
staying at 0, going down, or no change?
No...I'm not confused by WHAT is not changing! ammo count is in weapon...which IS changing! The problem I am having is with ammo. I ran the debugger...which sucks (MSVC 6), and I see that ammo[?]->x and ammo[?]->y are jumping all over the place...but in the when the code is ran...it is just 0. Confusing. I may just scrap this and start again...looks like some evil little voodoo spirit has corrupted this beast! It's just frustrating that it should NOT be acting like this....even the debugger shows that all data structures are VALID!
I see that ammo[?]->x and ammo[?]->y are jumping all over the place...but in the when the code is ran...it is just 0.
Most likely memory corruption in your code, a bit too typical in pointer-oriented C code. Maybe using C++, STL and proper interfaces would help?
there is ALOT of debugging crap in that code
Well, I can't tell what is INTENDED to be debugging crap from what is the cause of your bug - honestly, it's truly hard to guess what you voluntarily cut, from what was already missing 
I'm pretty sure it would be much more helpful to provide the code 'that should work perfectly'. Extra printf's hurt only if you really botch them, but here you really mutilated the behaviour of your Ammo class...
For example, you try to hard-code x = y = 50 for ammo, but it's in Ammo::Update, which is called by Weapon::Update, which is never called... You commented out that part too.
Ok...I've got this worked out now! Thanks all that helped... Sorry about all the crappy code I posted...was hard to tell what was suppose to be there, since I was trying to pick away at areas I thought could cause the problem. Was something stupid and quickly overlooked...even in the debugger. It was showing int values instead of float...that's why it looked so confusing. The ammo's Display(...) function was never being called...
Anyway, if anyone is interested in seeing this demo so far...it's attached. It is a windows app. It includes the GFX and SFX that it needs to play. Has some bg music, some cheesy fire sounds, engine humm....blah blah blah. It is going to be my test engine for real time network play...the source is changing drastically, but is coming along quite nicely though!