Jump to content
  • Advertisement
Sign in to follow this  
Cosmic R

data structure for rpg item

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

hello, I am currently writing a 2d tile based rpg, where the player has control over multiple characters. I am struggling to work out the best way to implement items. I am using C. the first method and simplist I could think of is an array of structs with a limited maximum number of elements. ie
struct item_s {
char name[12];
int tilex;
int tiley;
int quantity;
int health;
...etc
int location;
} item[MAX_ITEMS];
the problem with this is I dont know what MAX_ITEMS would be, and also when an item is destroyed, the memory is wasted. The next road block I ran into is I also wanted some items to be able to hold items. I could do it using this struct with my location and quantity variables, but there's got to be a smarter way. I want an item to 'know' where it is as well as have the 'location' know that it has the item. The second method I thought of was using linked lists. this solves my first problem by using dynamic memory, but not the second. Has anyone got any experience in this and can offer some advice?

Share this post


Link to post
Share on other sites
Advertisement
Why are you using C instead of C++? Is there a reason?

Also, if you must use C, you can easily tell when an item is destroyed by setting a flag for it being destroyed/gone. And then make a method that sets the item to an 'empty' state. Then when you need a new item you can modify the next item in the array starting at 0 that is empty. Then you can make it so people who need to access the item will have that item's index for the array. You can also create an accessor function that takes an index that returns the item the index points to, but if its an empty or destroyed item (for instance its trying to access something it shouldnt) it can return something noting that the item is empty and it will then remove the index for it.

I dont know if that makes sense, I use something similar for my player array for my online game.

With that being said, C++ will make things a lot more intuitive to follow.

Share this post


Link to post
Share on other sites
Quote:
Original post by Cosmic R
hello, I am currently writing a 2d tile based rpg, where the player has control over multiple characters. I am struggling to work out the best way to implement items. I am using C.
*** Source Snippet Removed ***


You are pretty close with that design. All you need to do is add in an int id; field that represents a unique id for the item and implement the linked list to solve your memory issues.

Once you do that, then you can associate id's with locations for a location to 'know' which items it has as well as items storing where they are at. That is assuming each tile contains an array of items it can hold. You might want to limit the number of items possible on a tile, and any further items dropped on a 'full' tile are simply destroyed.

As a bonus, by using the id field, you also gain the ability to add another field to your item_s structure, int items[10]; (Let's say each item can only hold 10 other items). Now, you just have to set an index to the item id that is contains and you know have an item "holding" another item. Each item will exist in its own memory in your item array still.

Now, you can reference items by id and perform necessary lookups to get the information you need as well as have the basics for more advanced systems.

The important thing to remember is, you will need to sequentially generate ids as well as make sure you zero out any memory used to store id variables. That is so you can use 0 to mean 'no item', and you can easily find available slots, item counts, etc... without too much extra work.

One little change can make one huge difference ;) Hope that helps.

Share this post


Link to post
Share on other sites
I think you need to understand pointer a little more if you are using C.

For your requirements, I would go with linked lists. For containers (bags, chests, etc) put a pointer into your item struct that will point to the head of another linked list of items.

This would make it easy for you to move items from your world into containers by moving it from your list of world objects to the container contents list.

Also, even though you are using C, you can still make use of an object oriented design with a little self discipline.


struct item_s {
char name[12];
int tilex;
int tiley;
int quantity;
int health;
//...etc
int location;
item_s* contents; // Pointer to a list of items this container holds
item_s* prev; // Previous item in the list
item_s* next; // Next item in the list
};

item_s *worldItems;

void createWorldItem( item_s* newItem );
item_s* getWorldItemAtPos( const int x, const int y );
moveWorlItemIntoContainer( item_s* worldItem, item_s* container );


Share this post


Link to post
Share on other sites
Sign in to follow this  

  • Advertisement
×

Important Information

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

GameDev.net is your game development community. Create an account for your GameDev Portfolio and participate in the largest developer community in the games industry.

Sign me up!