Allegro.cc - Online Community

Allegro.cc Forums » Programming Questions » issue with friend function.

This thread is locked; no one can reply to it. rss feed Print
issue with friend function.
Mizuki
Member #12,667
March 2011

I've been working on making a basic mario bros like game. I don't use classes often so the problem i'm having could be silly. here is a code showing the issue with the friend function name collision_detec(); the issue is that when the bools are updated it doesn't seem to be telling the collision function that they are.

#SelectExpand
1 class enemy; 2// deals with the hero. 3class hero 4{ 5 private: 6 int x,y; // coord of hero. 7 bool jumping, standing, walking; // hold the state the hero is in. 8 public: 9 void draw_stuff(); // draws the hero. 10 private: 11 void update_bool(); // updates the state of the hero. 12 void update_pos(); // updates the position of the hero. 13 14 friend int collision_detec(hero &a, enemy &b,hero &c); 15}; 16 17class enemy 18{ 19 private : 20 int enemX,enemY; // coord of enemy 21 public: 22 void draw_enem(); // draws the enemy 23 private: 24 void update_enem_pos(); // updates the coords 25 26 friend int collision_detec(hero &a, enemy &b,hero &c); 27}; 28 29void hero::update_bool() 30{ 31 // updates bool 32} 33 34void hero::update_pos() 35{ 36 // calls update_bool(); 37 // does stuff 38} 39 40void hero::draw_stuff() 41{ 42 // calls update_pos(); 43 // draws 44} 45 46 47int collision_detec(hero &a, enemy &b,hero &c) 48{ 49 // which part of the hero is hitting the enemy. 50 const int h_bott=1; 51 const int h_top =2; 52 const int h_right=3; 53 const int h_left =4; 54 55 // the variables that will hold the coord of the bounding box of the hero and enemy. 56 int leftH,leftE; 57 int bottomH,bottomE; 58 59 // this is the problem the program is not going through this loop even when jumping is true. 60 if (c.jumping == true) 61 { 62 63 //more if statements 64 return h_bott; 65 } 66 67 return false; 68}

jmasterx
Member #11,410
October 2009

I'm not sure what the problem is but a few issues come to mind. You shouldn't use friend. (C++ friends just wanna get access to your class's privates ;) )

You should have at least, an .h file that declares your collision function. Then a cpp file that implements it. Then, where ever you choose to use it, just include the .h file for it and call the function.

As for the value of the jump, you should try this.

Create a setJump and isJumping() functions.

Make them public or make the getter public. Don't use friend.

Inside they will set and return your jump variable. But, also add code that will output the value of jump and which function is being called. This way you can keep track of the value and find out if something is wrong.

You can also output when your collide function is called to make sure it is called.

The other problem I see is that your collision function returns an int but you return a bool. You should not do this in C++.

Also make sure that all your class variables have an initial value. Otherwise they get a random value which can affect your game.

Mizuki
Member #12,667
March 2011

i'm not familiar with using .h files and stuff but if need be i can google it.
i have a make_jump() which is part of the hero class it deals with changing the x and y position when a jump occurs it doesn't return anything cause it's a void. but it works fine. The friend function gets called but its the if statement that doesn't work. My thinking was that since the bool function will update jumping to true that value should be passed down to the collision function it doesn't seem like this happens. btw my collision function is only called in main() should i call it within a function??

jmasterx
Member #11,410
October 2009

It should be okay, but could you show your main() code, it might make it clearer.

Mizuki
Member #12,667
March 2011

ok my main looks something like this.

#SelectExpand
1 int main() 2{ 3 // regular allegro stuff 4 // create buffer and load bitmaps. 5 6 hero anim_hero; 7 enemy anim_enemy; 8 hero a; 9 enemy b; 10 hero a; 11 12 int w; 13 while(!key[KEY_ESC]) 14 { 15 anim_hero.draw_stuff();// calling draw_stuff 16 anim_enemy.draw_enem();// calling draw_enem 17 w = collision_detec(a,b,c) // calling the collision function. 18 draw_sprite(screen,buffer,0,0); 19 clear_bitmap(buffer); 20 rest(100); 21 cout << w; // checking if collision works fine. 22 } 23 24 return 0; 25}

jmasterx
Member #11,410
October 2009

Well, other than the fact that you have 'Hero 'a twice, everything looks in order. Maybe someone else can figure it out.

Mizuki
Member #12,667
March 2011

thanks for trying to help.

someone972
Member #7,719
August 2006
avatar

It may be better if you use inheritance in this case. For example, you could make a class called CollisionRect which both hero and enemy inherit. Or you could just have an instance of a CollisionRect in each class. This way the only class a collision function needs to know about is the CollisionRect class. It also makes it easier to expand.

______________________________________
As long as it remains classified how long it took me to make I'll be deemed a computer game genius. - William Labbett
Theory is when you know something, but it doesn't work. Practice is when something works, but you don't know why. Programmers combine theory and practice: Nothing works and they don't know why. -Unknown
I have recklessly set in motion a chain of events with the potential to so-drastically change the path of my life that I can only find it to be beautifully frightening.

Stas B.
Member #9,615
March 2008

Mizuki said:

ok my main looks something like this.

Are you sure it looks like that?
I don't see any calls to update_position or update_bool or anything so there's nothing to set the jump flag you're checking. ???
If your real code actually does game logic and it still doesn't work, the code you have provided is irrelevant. The problem is obviously not with the if statement that checks the flag but with the code that sets the flag.

Mizuki
Member #12,667
March 2011

someone972 I think i might take your advice on that one as i can't figure out what is wrong with the current code i have.
stas B. update_pos() calls update_bool in turn draw_suff() calls update_pos() see the previous code i posted for more details. And of course it's not with the if statement cause the program doesn't go through the if statement the problem is the value set by update_bool() does not seem to be passed down to the collision function, but it passes down to all the other functions i need it to.

Stas B.
Member #9,615
March 2008

Mizuki said:

stas B. update_pos() calls update_bool in turn draw_suff() calls update_pos() see the previous code i posted for more details. And of course it's not with the if statement cause the program doesn't go through the if statement the problem is the value set by update_bool() does not seem to be passed down to the collision function, but it passes down to all the other functions i need it to.

My bad. I missed that. You really shouldn't call update_pos from draw_stuff. They have nothing to do with eachother and you don't want the speed of your game to depend on the FPS rate. You should call update_pos independently at a constant rate.

In any case, nothing is being passed down anywhere.
Either update_bool does not set the bool or something else changes it before it's read by collision_detect. If you don't post the relevant code, nobody can possibly help you.

Mizuki
Member #12,667
March 2011

the codes i've posted are all the relevant ones. my actually program is pretty long and not commented so i doubt anyone would want to go through all of it. there are only two functions that affect the bool jumping 1) update_bool() sets jumping to true when i press the key up and 2) a function named make_jump() (not shown in my code) sets jumping to false when the hero hits the ground. Now even when i remove make_jump() from the program which means that when i hit the key up jumping will remain true till the program closes, the same issue remains. So i'm thinking it has to do with the function being a friend.

Stas B.
Member #9,615
March 2008

Mizuki said:

the codes i've posted are all the relevant ones. my actually program is pretty long and not commented so i doubt anyone would want to go through all of it. there are only two functions that affect the bool jumping 1) update_bool() sets jumping to true when i press the key up and 2) a function named make_jump() (not shown in my code) sets jumping to false when the hero hits the ground. Now even when i remove make_jump() from the program which means that when i hit the key up jumping will remain true till the program closes, the same issue remains. So i'm thinking it has to do with the function being a friend.

I can tell you with a 100% certainity that it has nothing to do with the function being a friend. Either the bool is never set in the first place or it's being overwritten somewhere else, so any code that touches it is relevant. If you want anybody to be able to help you, post the code for update_bool and make_jump. Otherwise, you're on your own. I won't even bother to ask again.

bamccaig
Member #7,536
July 2006
avatar

Mizuki
Member #12,667
March 2011

Thanks everyone for trying to help, it turns out the problem occurred because the variable jumping was not Static.

Stas B.
Member #9,615
March 2008

Mizuki said:

Thanks everyone for trying to help, it turns out the problem occurred because the variable jumping was not Static.

It shouldn't be static. If it doesn't work unless its static, you're doing something terribly, terribly wrong. :P

Go to: