Sign in to follow this  
XTAL256

[C++] Member function pointer array

Recommended Posts

I have a function pointer array for player actions in my game. I use a std::map to map the key code of the key pressed to the action that has been assigned to it (i.e. Key D -> move left). That worked ok as far as i know but now i am trying to use it and i don't know how. The functions are in the Player class and i want to call the function for a particular player object.
// This is what i am trying to do
player->Settings::actions[keyCode()]();


// Player.h
class Player {
public:
    String name;
    Mech* mech;
    int health;
    Colour colour; // Team colour
    float aimAngle;
    int aimPower;
    int x, y;      // x, y position

    Player() {}
    Player(String name, Colour colour);
    ~Player();

    void moveLeft();   // Action functions
    void moveRight();
    void jump();
    void fireWeapon();
    ...

    inline void setMech(Mech* newMech) {
        if (mech) delete mech;
        mech = newMech;
        health = mech->maxHealth;
    }

    void draw();
};

class HumanPlayer : public Player {
public:
    HumanPlayer() {}
    HumanPlayer(String name, Colour colour) :
        Player(name, colour) {}
    ~HumanPlayer() {}
};

typedef void (HumanPlayer::*ActionFunc)(void); // Player action function
extern HumanPlayer* player;   // Global player object

// Settings.h
class Settings {
    static map<String,int> keyMap;          // Maps keycodes to string values
    static map<String,ActionFunc> actionMap;// Maps action functions to their string names
    static map<int,ActionFunc> actions;     // Maps player actions to key codes
    ...
};

// Settings.cpp
#define MAKE_FUNC_PAIR(func) make_pair(#func, &Player::func)

void Settings::init() {
    ...
    // Map player action functions to their string names
    actionMap.insert(MAKE_FUNC_PAIR(moveLeft));
    actionMap.insert(MAKE_FUNC_PAIR(moveRight));
    actionMap.insert(MAKE_FUNC_PAIR(jump));
    actionMap.insert(MAKE_FUNC_PAIR(fireWeapon));
    ...
    // I read key bindings from a file and map the actions to the keycodes
}


Basically the first line of code is what i am trying to do but i'm not sure of the syntax. The code below it is for reference.

Share this post


Link to post
Share on other sites
Quote:
Original post by DevFred
Something like this maybe?

(player->*(Settings::actions[keyCode()]))();


This will work, But it will use your default null function pointer if a key that has no function mapped to it is pressed. Most likely a crash, at best it will just allocate memory.

Share this post


Link to post
Share on other sites
Ah, or course, dereference the pointer. But why do you need parentheses around player->*Settings::actions[keyCode()]? Is that not the same as player->(*Settings::actions[keyCode()])()?.

@vs322: yeah, i will check to make sure i have a function mapped to the key before i try to call it.
EDIT: oh, i just understood what you mean. I was debugging it and found that when i pressed a key that wasn't mapped to an action, that key (as in map key-value) was added with the value of NULL (0). But that's still ok because i will do the check before i even try to access a value:

if (map contains keyCode)
(player->*Settings::actions[keyCode()])()


That way the std::map won't try to add a key that's not already mapped

[Edited by - XTAL256 on September 17, 2008 4:42:15 AM]

Share this post


Link to post
Share on other sites
Quote:
Original post by XTAL256
Ah, or course, dereference the pointer. But why do you need parentheses around player->*Settings::actions[keyCode()]? Is that not the same as player->(*Settings::actions[keyCode()])()?.


No, they are not the same.

player->(*Settings::actions[keyCode()])() is a sintax error like player->(*Settings::actions[keyCode()]()).
player->*Settings::actions[keyCode()]() is the same as player->*(Settings::actions[keyCode()]()), which is clearly (:P)different than (player->*Settings::actions[keyCode()])().

Share this post


Link to post
Share on other sites

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