Jump to content
  • Advertisement

Archived

This topic is now archived and is closed to further replies.

Kranar

Efficient Structures

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

Okay, I''m using C and working on making a semi-efficient data structure for objects in my game. Now an object can be one of many varieties, it can be a weapon, or a shield, or a container, and heck technically it can be all of them or none of them. My one data structure must take into account all of these different types of objects for functionality reasons which I''d rather not get into. Every object has some essential data members. They include things like name, basic description, weight, size, and some other info. But for other members, not all objects need them, some members are only used for shields, other members are only used for containers, so on so forth. My question is this, how can I have a data structure that sometimes allocates memory for a set of members, and sometimes does not. That way the shield members of an object are only stored in RAM if the object is a shield, and that way I don''t have to store a bunch of variables/members in RAM when I''m not even using them. Thank you.

Share this post


Link to post
Share on other sites
Advertisement
Use OOP and inheritance

class CObject
{
public:

//Accessors for private members
string GetName();
void SetName(string newName);

int GetWeight();
void SetWeight(int newWeight);

private:

//Essential data members
string name;
int weight;

};

class CWeapon: public CObject
{
public:

int GetDamage();

private:

//Weapon specific attributes
int damage;
}


This way CWeapon will get the members that all objects need as well as the ones only weapons need.

Also, using polymorphism, you can cast a CWeapon instance to a CObject (casting from a CObject to a CWeapon is bit more tricky, and I don''t recommend doing it as there is a speed hit involved).

That way you can keep a linked list of pointers to CObject''s and add all your weapons, shields, or whatever.

Share this post


Link to post
Share on other sites
quote:
Original post by glassJAw
Use OOP and inheritance



That means you should use C++ instead of C





--{You fight like a dairy farmer!}

Share this post


Link to post
Share on other sites
One way I KNOW it will work would be to just make a WEAPON structure, and then a vector of weapons in my object structure, like so. I can switch to C++, it''s no big deal if I must.



struct WEAPON {
int damage;
};

struct OBJECT {
string name;
int id;
vector<WEAPON> weapon;
bool is_weapon;
};


This way, vector starts off empty, and if I want I can set it to 1 element and access the weapon''s damage through:

object.weapon[0].damage

I was hoping that perhaps something like this exists, without having to use object.weapon[0].damage, and instead just using object.weapon.damage

Any ideas?

Share this post


Link to post
Share on other sites
Ah sorry I didn''t see you wanted to use straight C.

Your way should work, but you''re using C++ there
vector is a template class.

Share this post


Link to post
Share on other sites
just use void pointers and emulate inheritence via the void pointer array. also for efficentcy reasons try to pad your data structures to multiples of 32 bits as most processors can cache them better

Share this post


Link to post
Share on other sites
He''s got a point, you can''t use templates if you are using C anyway! I''d recommend getting a good c++ book (one with console examples is fine) and learning all about OOP and templates.


Brian J
DL Vacuum - A media file organizer I made | MM

Share this post


Link to post
Share on other sites
Hello Kranar,

I remember way back when I work on arcade game for SEGA back in 95-96 when they still did arcade games we did something along the line of what Vaneger said about use void pointers.

This is what I would do.

have a default struct that would share what all struct have and also have a member for type of struct it is.

example:

typedef struct base_info_tag
{
int type;
float x, y, z;
float weight;
char[32] name;
...
} base_info

next define your item structures.
example
typedef struct shield_tag
{
base_info base;
float radius;
} shield;

now what you do is when every you create a new item store it in a list or array as a void pointer then cast to base_info pointer and then check type and cast to the type it is. Or cast void pointer to a int pointer check theis int which should be type int for type and cast to proper type. Do you get what I mean?

Or you could have in base_info a void pointer that would be assigned a address to extra data. You still need to check type and then cast void pointer to type of extra data was and use. In this case extra data would not have base_info struct in it.
If void pointer is null the no extra data.

Get what I writing?

Lord Bart

Share this post


Link to post
Share on other sites
quote:
Original post by vaneger
just use void pointers and emulate inheritence via the void pointer array. also for efficentcy reasons try to pad your data structures to multiples of 32 bits as most processors can cache them better


Most compilers automatically pad to multiples of four. You can easily get them to pad to multiples of 32 by using #pragma pack(32)

Share this post


Link to post
Share on other sites

  • Advertisement
×

Important Information

By using GameDev.net, you agree to our community Guidelines, Terms of Use, and Privacy Policy.

We are the game development community.

Whether you are an indie, hobbyist, AAA developer, or just trying to learn, GameDev.net is the place for you to learn, share, and connect with the games industry. Learn more About Us or sign up!

Sign me up!