|
Variables... |
Moonsdescend
Member #4,087
December 2003
|
I'm pulling my hair out trying to work this one out. heres the code :
This function is called if plr.menu is set to 1 (or display the war menu) which works fine. Any ideas? |
Kitty Cat
Member #2,815
October 2002
|
Don't pass entire structs as parameters. Either pass a pointer, or (if using C++) a reference. I don't know if that's related to your problem, but I don't see anything in that code that'd cause it to reset (a stack overflow could cause you to start getting memory corruption, though). Double-check everywhere your check researching == 0 and make sure you aren't accidently doing researching = 0. -- |
Moonsdescend
Member #4,087
December 2003
|
That's whats bugging me. The only place that researching is being used is in this function. |
miran
Member #2,407
June 2002
|
If you code in C: void draw_war_menu(BITMAP *buf,player *plr) { ... plr->whatever = something; } // call it like this: draw_war_menu(buffer, &player); If you code in C++ the above will work but this is prefered: void draw_war_menu(BITMAP *buf,player &plr) { ... plr.whatever = something; } // call it like this: draw_war_menu(buffer, player); Btw, that's what Kitty Cat said in case you didn't understand. EDIT: And in case you still don't understand, I'll try to explain. When you pass your player object by value, a temporary copy is made. You modify this temporary copy in your draw_war_menu function. When this function returns, the temporary copy is destroyed. So nothing gets reset to 0 because you never changed the original in the first place. You were modifying a temporary copy. Also you might notice if you pay close attention, that your code not only doesn't do what you want, but will also be a bit slower than the proper way. Because making a temporary copy and then destroying it takes more time than not making a temporary copy and then destroying it. And one more thing, you should always separate logic from drawing! Your code mixes logic and drawing. This is not good. -- |
Richard Phipps
Member #1,632
November 2001
|
If you are passing player ply, then won't it be passing a copy to the function? In this case you will only be setting the copy's variable and not the original. |
ReyBrujo
Moderator
January 2001
|
Indeed, you are passing your structure's values. Use pointers or references. -- |
Moonsdescend
Member #4,087
December 2003
|
Thanks for all the advice. I think it might also have something to do with the fact that the plr struct is a global variable as well. Would this cause a conflict using player plr as a global and player plr as a parameter? |
Thomas Fjellstrom
Member #476
June 2000
|
No conflicts that I know of. Its a bit messy, but thats about it. OT: Am I the only one who wants to post: "No, use HTML" ? -- |
|