Jump to content
  • Advertisement
Sign in to follow this  


This topic is 3649 days old which is more than the 365 day threshold we allow for new replies. Please post a new topic.

If you intended to correct an error in the post then please contact us.

Recommended Posts

Sorry, but i'm kind of stucked in a simple question, I have a class called theTeam, and thePlayers. /* in theTeam.h */ #include "thePlayers.h" class theTeam { public theTeam(); ~theTeam(); thePlayers * m_players; // all players in the team } /* in thePlayers.h */ class thePlayers { public: thePlayers(); ~thePlayers(); thePlayers getClosestTeammate(); //get the closest teammate frm the team } /* end */ problem here i have is, how do i actually apply the function 'getClosestTeammate()'? since i can't access the data in theTeam from thePlayers class, thru using getClosestTeammate(). Any better solutions?

Share this post

Link to post
Share on other sites
Some possible solutions:

1. Implement getClosestTeammate() in theTeam and pass a player to the function.

2. Add the team as a parameter to the getClosestTeammate() function.

3. When a player is created, tell it what team it belongs to.

4. Store all your teams in a place accessible to all thePlayers and have getClosestTeammate() search through all the teams to find itself. This is probably the least optimum solution in most cases.

5. Pass a pointer to the m_players variable in theTeam to all players when they are created. As long as you don't tell anyone that I told you to do that.

6. Don't have teams or don't have players.

I would suggest one of the first three if possible. It seems to me, without knowing anything about what your doing, the first solution would be the best.

[Edited by - wprintf on June 28, 2008 2:12:03 AM]

Share this post

Link to post
Share on other sites
Thanks for helping,
i wouldn't try sth like pTeam->m_players->getClosestTeammate(),
because i have too many activities to let players to handle solely,
so it's not ideal to call from pTeam,
and i have other things to let pTeam to handle as well.

the 2nd method u mention is what i'm seekin for,
but how to pass theTeam as a parameter to the GetClosestTeammate() function in thePlayer?
since i'm loading the thePlayer.h header from theTeam.h,
so it only will do recursive loading and will corrupt before running.

what i hope to do is,
i have Move(), Run(), Think() and similiar activities in thePlayer class,
so for e.g, when i Think(),
i hope it can find the closest teammate to him,
but i hope this GetClosestTeammate() to be launched only when it's needed,
so keep updating who's the closestTeammate from theTeam class is not very ideal,
that's what i think.

Any possible solution to this?

Thanks in advance.

Share this post

Link to post
Share on other sites
The solution is to use forward declarations.

#### theTeam.h ####

#include "thePlayers.h"

class theTeam {

thePlayers * m_players;

#### thePlayers.h ####

#include "theTeam.h"

class theTeam; //forward declaration

class thePlayers {

thePlayers* getClosestTeammate(theTeam* team);

If you wanted to implement getClosestTeammate() in theTeam you could pass theTeam to any function in thePlayers that might need it. Which is what you would have to do anyway to make getClosestTeammate() to work in thePlayers anyway. The only difference is instead of calling getClosestTeammate(team) you would call team->getClosestTeammate(player). Not that either way makes any difference.

Share this post

Link to post
Share on other sites
ohh forward declaration,

thanks for the help,
i got it worked =)

Deeply appreciated ^^.
God Bless~!

Share this post

Link to post
Share on other sites
Sign in to follow this  

  • Advertisement

Important Information

By using GameDev.net, you agree to our community Guidelines, Terms of Use, and Privacy Policy.

Participate in the game development conversation and more when you create an account on GameDev.net!

Sign me up!