I am making this MUD-type game and each player struct has two other structs called "gear" and "inventory"
Gear is what is worn, and that is an array of item-structs (item_t item[11], it is typedef'd)
Inventory is the same but with a size of 20 instead of 11.
When I want to load a character, I access the "item ID" from a file (a regular integer) and pass it to a function that should return the correct item, so that it can be applied to the correct inventory/gear slot before the game starts. My problem is that the program crashes as soon as I try to fill these inventory/gear slots... It reads all attributes and strings from the save-file perfectly, so I have narrowed it down to being a definition error (I just started using structs and C)
This is the stuff:
Code:
typedef struct item_s {
... all kinds of normal data types
} item_t;
typedef struct inventory_s {
item_t* item[20];
} inventory_t;
typedef struct gear_s {
item_t* item[MAX_SLOTS]; //#define MAX_SLOTS 11
} gear_t
Then when I try to initialize these, I do this:
Code:
if(pos == 13) p->backpack.item[pos-13] = get_item(atoi(buf));
p is a pointer to a player struct and backpack is an inventory-struct.
And I do this for every slot. buf is not wrong, that I am certain of.
Here's get_item(int id):
Code:
item_t* get_item(int id) {
item_t *t;
strcpy(t->name, "empty");
return t;
}
I havent implemented the ID-thing yet so it just returns an item called "empty".
Need help badly :|
This was much easier with classes and "new ()".