Tek-Tips is the largest IT community on the Internet today!

Members share and learn making Tek-Tips Forums the best source of peer-reviewed technical information on the Internet!

  • Congratulations gkittelson on being selected by the Tek-Tips community for having the most helpful posts in the forums last week. Way to Go!

dynamiclly adding struct members

Status
Not open for further replies.

zanza

Programmer
Feb 18, 2002
89
US
first things first, im a newbie :p

ok, im trying to make a simple simple rpg just for s's & g's. so heres my inventory uses a structure for each weapon with its attributes therein. those will be declared in game when the player discovers the weapons. same with items, armor and spells. now, the actual inventory is another structure with counters to make sure the player doesnt have 46 weapons ;).

my question is this... when the user finds the weapon, that weapon gets its own struct, but, will i be able to add that struct to the inventory struct?

heres the source code for the inventory header:

#ifndef __INVENTORY_H__
#define __INVENTORY_H__

#endif

struct Weapon
{
char Name[255];
int Damage;
int Weight;
};

struct Armor
{
char Name[255];
int Protection;
int Weight;
};

struct Item
{
char Name[255];
int StrPlus;
int SpdPlus;
int IntelPlus;
};

struct Spell
{
char Name[255];
int Damage;
};

struct Inventory
{
int WeaponCount;
int ArmorCount;
int ItemCount;
int SpellCount;
};

void InvenGen(void);
žÅNžÅ
 
I'm not sure what you want...
To increment the counter for weapons count with each
found weapon, just: Fred.WeaponCount++;

If you would like to have linkages to weapons by index
you could add an int index to the weapon and then search
by index for that.

If you would like a linked list for weapons possessed,
than something like:
struct Inventory {
struct Weapon *ptr;
int WeaponCount;
etc..
}; should work for you.

A great source of ideas for rpg's is the source for
angband or any of it's clones. Do a google search for it.
 
ill go look through that source code... thanks :)
but what i want... i want to be able to add a weapon struct to the inventory struct for each weapon as its found. if i could do that, i would just increment the counter as you said and test that value to make sure they didnt have too much stuff. žÅNžÅ
 
You will need to understand the concept of linked lists.
A linked list is a chain of structures pointing to the
next, or in the case of a doubly linked list, pointing
to previous and next.

A simple example using your idea:

struct Inventory {
struct Weapon *ptr;
int WeaponCount;
int ArmorCount;
int ItemCount;
int SpellCount;
} Fred;

/*The weapon structure needs to be changed to add a pointer
to the next weapon..*/

struct Weapon
{
char Name[255];
int index;
int Damage;
int Weight;
struct Weapon *pnext;
};


int main(void) {
struct weapon one = { "vorpalsword +4", 120, 10, NULL };
Fred.ptr = &one;
Fred.WeaponCount += 1;
return 0;
}

Alternately you could use an array of weapon structs if
you find linked lists a little too much, but now is a good time to start to learn about them IMO.

Good Luck
 
marsd is correct about the linked lists (or other similar container). However to expand on this, from what you describe, you should really be looking to produce this program using OOP models (ie. C++) rather than C. This will make mixing and matching different items easier when they are defined as classes.
tellis.gif

programmer (prog'ram'er), n A hot-headed, anorak wearing, pimple-faced computer geek.
 
Hi,

Since you have a limit of 46 weapons you might want to consider using a long as a bitmask of the players' attributes, this will then allow you to have a maximum of 64. You can then assign each one (using an enum) an ID. When the player collects the weapon, spell or whatever you then | the current attributes with the new item's ID.

When you then need test to see if a player can cast a spell you simply & the attribute with the requested objects ID. This has major advantages of speed since you don't have to search a list to see if a user has a given object.

A hypothetical E.G.
Code:
enum eAttribs {eSpell = 1, eArmour = 2, eXBow = 4, eKnife = 8, ...} ;

struct sArmour 
{ 
    sArmour() 
    { 
        ID = eArmour ; 
        Weight = 10 ; 
        Protection = 35 ; 
    } ; // Armour's constructor
    long ID ;
    int Protection ;
    int Wieght ;
}

struct sKnife
{
    sKinfe() 
    {
        ID = eKnife ;
        ...
    }// Knife's constructor
    long ID ;
    ...
}
By using a constructor in the struct you can then ensure that each item is initialised when created for use in the game, allowing you to set defaults automatically and avoiding having to do it manually.

Player finds an Item.

Fred.Attribs |= pItem->ID ;

Later when he wants to use the Item in this case a Knife.

if (Fred.Attribs & eKnife)
{ // Stab the git. }
else
{ // Oh! sh*t no knife can't stab him, so run. }

Also, gednicks suggestion of going OO is correct, games such as this are screaming out to be OO, and by doing so will give you so much more power and flexability.

HTH
--
William
Software Engineer
ICQ No. 56047340
 
Status
Not open for further replies.

Part and Inventory Search

Sponsor

Back
Top