Allegro.cc - Online Community

Allegro.cc Forums » Programming Questions » Variables...

This thread is locked; no one can reply to it. rss feed Print
Variables...
Moonsdescend
Member #4,087
December 2003
avatar

I'm pulling my hair out trying to work this one out.

heres the code :

1void draw_war_menu(BITMAP *buf,player plr)
2{
3
4 clear_to_color(black,makecol(255,0,255));
5
6 rectfill(black,200,79,200+400,94,makecol(161,165,156));
7 rectfill(black,200,119,200+400,134,makecol(161,165,156));
8 if(key[KEY_R])
9 {
10 plr.researching = 1;
11 }
12/* if(key[KEY_E])
13 {
14 plr.researching = 0;
15 }
16*/
17 set_trans_blender(255,255,255,75);
18 draw_trans_sprite(buf,black,0,0);
19 show_mouse(NULL);
20 draw_header(buf,"War Menu");
21 
22 alfont_set_font_size(mainfont, 15);
23 alfont_textprintf_aa(buf, mainfont, 203, 60, makecol(255, 255, 255),"Soldiers");
24 alfont_textprintf_right_aa(buf, mainfont, 595, 60, makecol(255, 255, 255),"%i",plr.army);
25 
26 alfont_textprintf_aa(buf, mainfont, 203, 80, makecol(255, 255, 255),"Attack");
27 alfont_textprintf_right_aa(buf, mainfont, 595, 80, makecol(255, 255, 255),"%i",plr.attack);
28 
29 alfont_textprintf_aa(buf, mainfont, 203, 100, makecol(255, 255, 255),"Defence");
30 alfont_textprintf_right_aa(buf, mainfont, 595, 100, makecol(255, 255, 255),"%i",plr.defence);
31 
32 alfont_textprintf_aa(buf, mainfont, 203, 120, makecol(255, 255, 255),"Technology");
33 alfont_textprintf_right_aa(buf, mainfont, 595, 120, makecol(255, 255, 255),"%i",plr.technology);
34 
35 alfont_textprintf_aa(buf, mainfont, 203, 140, makecol(255, 255, 255),"Technology progress");
36 draw_progress_bar(buf,320,145,599, 152, plr.currtech,plr.technext,0,makecol(255,255,255),makecol(101,110,142));
37 
38 if(plr.researching == 0)
39 {
40 rectfill(buf,605,140,655,155,makecol(101,110,142));
41 
42 if((mouse_x > 604) && (mouse_y > 140) && (mouse_x < 655) && (mouse_y < 155))
43 {
44 rectfill(buf,605,140,655,155,makecol(148,143,255));
45 strcpy(message,"Research technology");
46 if(mouse_b & 1)
47 plr.researching = 1;
48 
49 }
50 alfont_textprintf_aa(buf, mainfont, 607, 141, makecol(255, 255, 255),"Research");
51
52 }
53 if(plr.researching == 1)
54 alfont_textprintf_aa(buf, mainfont, 607, 141, makecol(255, 255, 255),"Currently Researching...");
55 
56 alfont_textprintf_right_aa(buf, mainfont, 595, 300, makecol(255, 255, 255),"%i",plr.researching);
57
58}

This function is called if plr.menu is set to 1 (or display the war menu) which works fine.
However whenever I try to set the plr.researching variable to 1 it automatically resets to 0. I've check all other files and there is no where that researching is being set at all. Researching will only stay set if I am holding the mouse button down or have the R key checked.

Any ideas?

Kitty Cat
Member #2,815
October 2002
avatar

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.

--
"Do not meddle in the affairs of cats, for they are subtle and will pee on your computer." -- Bruce Graham

Moonsdescend
Member #4,087
December 2003
avatar

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.

--
sig used to be here

Richard Phipps
Member #1,632
November 2001
avatar

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
avatar

Indeed, you are passing your structure's values. Use pointers or references.

--
RB
光子「あたしただ…奪う側に回ろうと思っただけよ」
Mitsuko's last words, Battle Royale

Moonsdescend
Member #4,087
December 2003
avatar

Thanks for all the advice.
I'll be starting the project again from scatch, so I'll keep it all in mind.

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
avatar

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" ?

--
Thomas Fjellstrom - [website] - [email] - [Allegro Wiki] - [Allegro TODO]
"If you can't think of a better solution, don't try to make a better solution." -- weapon_S
"The less evidence we have for what we believe is certain, the more violently we defend beliefs against those who don't agree" -- https://twitter.com/neiltyson/status/592870205409353730

Go to: