Reputation: 966
I'm implementing a card game in C. There are lots of types of cards and each has a bunch of information, including some actions that will need to be individually scripted associated with it.
Given a struct like this (and I'm not certain I have the syntax right for the function pointer)
struct CARD {
int value;
int cost;
// This is a pointer to a function that carries out actions unique
// to this card
int (*do_actions) (struct GAME_STATE *state, int choice1, int choice2);
};
I would like to initialize a static array of these, one for each card. I'm guessing this would look something like this
int do_card0(struct GAME_STATE *state, int choice1, int choice2)
{
// Operate on state here
}
int do_card1(struct GAME_STATE *state, int choice1, int choice2)
{
// Operate on state here
}
extern static struct cardDefinitions[] = {
{0, 1, do_card0},
{1, 3, do_card1}
};
Will this work, and am I going about this the right way at all? I'm trying to avoid huge numbers of switch statements.
Do I need to define the 'do_cardN' functions ahead of time, or is there some way to define them inline in the initialization of the struct (something like a lambda function in python)?
I'll need read-only access to cardDefinitions from a different file - is 'extern static' correct for that?
I know this is a lot of questions rolled into one but I'm really a bit vague about how to go about this.
Thanks.
Edit:
To be clear, my goal is to be able to do something like
int cost = cardDefinitions[cardNumber].cost;
or
int result = cardDefinitions[cardNumber].do_action(state, choice1, choice2);
Instead of using huge switch statements all over the place.
Upvotes: 35
Views: 78988
Reputation: 239011
Your approach is right and will work. Your function pointer syntax is right, except that you don't use parameter names - just types:
int (*do_actions)(struct GAME_STATE *, int, int);
Upvotes: 3
Reputation: 175335
That should work fine. It seems like you'd have a lot of functions if you're doing one per card, but maybe this particular game requires that level of control
You can't define them inline, but you can just do a forward declaration. You need to do &func_name
in the struct initialization though
No; extern
means a variable is declared in another file, so it doesn't make sense to have an extern variable that you're declaring at that location. Also, static
means the variable is only accessible from the current file, which is the opposite of what you want. Making it read-only would require a getter function, but if you just want to make it accessible from another file declare it normally here (struct cardDefinitions[] = {...}
) and in the other file use an extern (extern struct cardDefinitions[];
)
Upvotes: 1
Reputation: 992857
Your approach is exactly right.
switch
statements.extern
is what you want, not static
. Change your body to be this:
struct CARD cardDefinitions[] = {
{0, 1, do_card0},
{1, 3, do_card1}
};
then in an appropriate header file:
extern struct CARD cardDefinitions[];
Upvotes: 42