Sign in to follow this  

Comparing properties of an object within other classes

This topic is 3035 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

Hi, Heres my first post to the forum, and of course its a question! Its a C++ question, and hopefully I'll make it simple. If I make an vector of classes representing a bunch of enemies in the 'main' loop of the program, is it possible to define a function within each 'enemy' class that compares a value within each 'enemy' class to a value within a separate vector of classes representing the player/s 'ship'? That was a mouthful, so hopefully you understand what I mean. I am trying to define a function within the enemy class that compares the distance between the enemy and the player's/s ship... but I am kinda scratching my head at how to do that with a function defined in the class itself. I've got no issue with the calculations or doing it within the main loop, but I seem to be missing something with respect to how C++ talks between classes/vectors/main game loop. Thanks in advance for any ideas! Fitz

Share this post


Link to post
Share on other sites
You could make a public function in your Enemy class called distanceTo that takes as a parameter the position of the object you want to get the distance to. Then you can use the position of your Enemy object and do the distance calculations. Something like this


class Enemy {
public:
float distanceTo( Vector3 vOtherPos ) {
// return distance calculation
}
};




Then you could use it like this


Enemy e;
Vector3 vShipPos;
if ( e.distanceTo( vShipPos ) > 1.0f ) {
// do stuff
}




Or you could just define a MathHelper class that has a static distance function that takes in two Vectors like this:


class MathHelper {
public:
static float distance( Vector3 v1, Vector3 v2 ) {
// return distance
}
};



Then you could use it like this:


Enemy e;
Vector3 vShipPos;

if ( MathHelper::distance( e.getPosition(), vShipPos ) > 1.0f ) {
// do stuff
}



I would do it the second way

Share this post


Link to post
Share on other sites
Actually, this question isn't really C++-specific - it's more of a design question.


I'd say, pass the information around when it is needed:
class Player
{
public:
vector2 getPosition();

protected:
vector2 position;
};

class Enemy
{
public:
void update(Player* player)
{
float distance = (player->getPosition() - position).length();
}

protected:
vector2 position;
};


You could also pass the Player pointer to those enemies when you're creating them, so you can keep the update call uncluttered. But, since enemies likely need to know other things as well, writing a class that contains the enemies and the player (call it, say, Game, or World) is probably better: you can pass a pointer or reference to that around, and have it provide an interface that allows other code to find out the players position, or the closest enemy to the given point, and so on. That makes:
class Player
{
public:
vector2 getPosition();

protected:
vector2 position;
};

class Enemy
{
public:
void update(World* world)
{
float distance = (world->getPlayer().getPosition() - position).length();
}

protected:
vector2 position;
};

class World
{
public:
Player& getPlayer()
{
return player;
}
// If the game always has a player, then returning a reference
// is good. However, if you'd write a function that returns the
// closest enemy, consider returning a pointer instead:
// if there are no enemies in the world, then you couldn't return
// a reference, because a reference always has to refer to an actual
// object. With pointers, you can return NULL to indicate that there's
// no enemy. Of course, the calling code would then always need to
// check for that!

protected:
std::vector<Enemy> enemies;
Player player;
};

Of course, this World class would offer an update function, that updates all it's content - or it could contain the main game loop itself (though then the name 'World' is a bit misleading - Game would be better).


So either way, 'talking' between classes almost always goes by passing things around.

Share this post


Link to post
Share on other sites
Thanks guys, I thought it was something along the lines of what you said, I thought that passing things around would be the way to do it... but I didn't want to go down the path of passing things around only to find that there was a much simpler way to do it! I'll have a crack at it later on today.

Thanks for your help, very much appreciated :)

Share this post


Link to post
Share on other sites

This topic is 3035 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.

Create an account or sign in to comment

You need to be a member in order to leave a comment

Create an account

Sign up for a new account in our community. It's easy!

Register a new account

Sign in

Already have an account? Sign in here.

Sign In Now

Sign in to follow this