J
Jim Langston
I want to change a class's methods at run time. I'm making a game and want
NPC objects to have different methods depending on a game designer. That
is, they may decide that this mob should be able to fly for movement, be
agressivie for combat, etc... Rather than a bunch of switch statements or
if..else I was thinking it would be good to be able to plug in
functions/methods to the class.
Well, there's the old C way, which I've done here, but am not happy with it
at all. For a few reasons:
1. I have to friend every function
2. I have to assign a function pointer, and things can go wrong there, plus
it gets to be a pain to maintain
3. It's just not, IMO, the best way it could be, but I don't know of a
better way.
I've only shown the function here for movement, but in reality there would
be one for movement, one for combat, one for guarding (if it's a guard),
etc...
Any suggestions on how to improve the design?
#include <iostream>
#include <string>
class Base
{
friend void Walk( Base& base );
friend void Fly( Base& base );
friend void Swim( Base& base );
friend void Float( Base& base );
public:
Base( int X, const std::string Movement ): X(X), Move_( NULL )
{
if ( Movement == "Flies" )
Move_ = Fly;
else if ( Movement == "Walks" )
Move_ = Walk;
else if ( Movement == "Swims" )
Move_ = Swim;
else if ( Movement == "Floats" )
Move_ = Float;
else
Move_ = Walk;
}
void Move() { if ( Move_ != NULL ) { Move_( *this ); } }
private:
void (*Move_)( Base& );
int X;
};
void Walk( Base& base )
{
base.X += 5;
std::cout << "Walked to " << base.X << "\n";
}
void Fly( Base& base )
{
base.X += 7;
std::cout << "Flew to " << base.X << "\n";
}
void Swim( Base& base )
{
base.X += 3;
std::cout << "Swam to " << base.X << "\n";
}
void Float( Base& base )
{
base.X += 2;
std::cout << "Floated to " << base.X << "\n";
}
int main()
{
Base MyBase( 10, "Flies" );
MyBase.Move();
std::string wait;
std::getline( std::cin, wait );
}
Regards,
Jim Langston
NPC objects to have different methods depending on a game designer. That
is, they may decide that this mob should be able to fly for movement, be
agressivie for combat, etc... Rather than a bunch of switch statements or
if..else I was thinking it would be good to be able to plug in
functions/methods to the class.
Well, there's the old C way, which I've done here, but am not happy with it
at all. For a few reasons:
1. I have to friend every function
2. I have to assign a function pointer, and things can go wrong there, plus
it gets to be a pain to maintain
3. It's just not, IMO, the best way it could be, but I don't know of a
better way.
I've only shown the function here for movement, but in reality there would
be one for movement, one for combat, one for guarding (if it's a guard),
etc...
Any suggestions on how to improve the design?
#include <iostream>
#include <string>
class Base
{
friend void Walk( Base& base );
friend void Fly( Base& base );
friend void Swim( Base& base );
friend void Float( Base& base );
public:
Base( int X, const std::string Movement ): X(X), Move_( NULL )
{
if ( Movement == "Flies" )
Move_ = Fly;
else if ( Movement == "Walks" )
Move_ = Walk;
else if ( Movement == "Swims" )
Move_ = Swim;
else if ( Movement == "Floats" )
Move_ = Float;
else
Move_ = Walk;
}
void Move() { if ( Move_ != NULL ) { Move_( *this ); } }
private:
void (*Move_)( Base& );
int X;
};
void Walk( Base& base )
{
base.X += 5;
std::cout << "Walked to " << base.X << "\n";
}
void Fly( Base& base )
{
base.X += 7;
std::cout << "Flew to " << base.X << "\n";
}
void Swim( Base& base )
{
base.X += 3;
std::cout << "Swam to " << base.X << "\n";
}
void Float( Base& base )
{
base.X += 2;
std::cout << "Floated to " << base.X << "\n";
}
int main()
{
Base MyBase( 10, "Flies" );
MyBase.Move();
std::string wait;
std::getline( std::cin, wait );
}
Regards,
Jim Langston