M
Malcolm
I'm doing an adventure game.
Objects naturally lend themselves to C++, but I decided to implement the
thing in C.
The idea was that each object is identified by an id. These are stored in a
big array, indexed by id number.
To use an object, you convert the id to an OBJECT *.
typedef struct object
{
int id;
void * (*hasattribute)(struct object *obj, const char *name)
void *data;
} OBJECT;
Now the idea is that each object implements interfaces. So for instance a
sword would have the interfaces "ITEM" (it is a named item in the game),
"PORTABLE" (you can pick it up an carry it), and "WEAPON" (you can use it to
attack people with). You would set the hasattribute member to a function to
return these interfaces. So for instance "PORTABLE" has a member labelled
"weight", since all portable items weigh something.
One problem is that this rapidly gets ugly. For instance to get the weight
of a sword you would have to write
sword = getobject(swordid);
portable = sword->hasattribute(sword, "PORTABLE"):
if(portable)
sword->getweight(sword, portable);
Why is this necessary, since we are probably returning a single variable?
The answer is that a bag would weight the weight of a bag plus the weight of
any contents.
Another problem is that the attributes just represent interfaces, weapons,
containers, decorations, and so on. If I want a "dancing sword" then really
I want some type of inheritance to extend the "sword" code. However with the
current setup I don't have this. Instead I have to re-implement all of the
interfaces.
Has anyone any experience doing this sort of thing?
Objects naturally lend themselves to C++, but I decided to implement the
thing in C.
The idea was that each object is identified by an id. These are stored in a
big array, indexed by id number.
To use an object, you convert the id to an OBJECT *.
typedef struct object
{
int id;
void * (*hasattribute)(struct object *obj, const char *name)
void *data;
} OBJECT;
Now the idea is that each object implements interfaces. So for instance a
sword would have the interfaces "ITEM" (it is a named item in the game),
"PORTABLE" (you can pick it up an carry it), and "WEAPON" (you can use it to
attack people with). You would set the hasattribute member to a function to
return these interfaces. So for instance "PORTABLE" has a member labelled
"weight", since all portable items weigh something.
One problem is that this rapidly gets ugly. For instance to get the weight
of a sword you would have to write
sword = getobject(swordid);
portable = sword->hasattribute(sword, "PORTABLE"):
if(portable)
sword->getweight(sword, portable);
Why is this necessary, since we are probably returning a single variable?
The answer is that a bag would weight the weight of a bag plus the weight of
any contents.
Another problem is that the attributes just represent interfaces, weapons,
containers, decorations, and so on. If I want a "dancing sword" then really
I want some type of inheritance to extend the "sword" code. However with the
current setup I don't have this. Instead I have to re-implement all of the
interfaces.
Has anyone any experience doing this sort of thing?