RPG item numbers?

Recommended Posts

Lordimm    122
While trying to make a text-based RPG, to make a shop, I used a huge amount of lines, and I was planning on making more shops, so it was obviously not worth it to go the way I was going. I had the idea to make a function to make a shop with the item (listed in numbers) as parameters to the function. The items in the RPG would have to be objects of an ITEM class, but I was wondering, how would I make the shop function check to see what items are used as parameters? In other words: void Shop(item1, item2, item3, item4...) { } How would you be able to check to see what items are used as parameters in the function, and be able to display the items into the shop menu to buy.

Share on other sites
Toolmaker    967
Assuming you use C++, why no go with arrays?

Arrays are exactly what you want in this case, and your function will then look something like this:
void Shop(Item[] items, unsigned int numItems)

However, you still need to define the shop item lists then, but you could either hardcode those, or load them from a file.

Toolmaker

Share on other sites
Lordimm    122
I figured I'd need to use arrays in some form, but I don't know how to tie arrays and objects of the class ITEM together. I assume that you're talking about using a for loop to increment item[] by 1, checking it against a default, but I have no clue how to, once it knows what item I'm talking about, access the object without a giant block of code.

Share on other sites
krez    443
I'd recommend a std::vector or std::list instead of an array (depending on if you need the random access of an array / vector, or just a list to go through in order).

void Shop(std::list<ITEM>& ItemList)  {  for(std::list<ITEM>::iterator i = ItemList.begin(); i != ItemList.end(); ++i)    {    // do whatever you need with each item    // To get the actual ITEM objects to "play" with,    //   dereference the iterator: (*i)    //   e.g.    //      (*i).methodInTheItemClass();    };  };

A vector would be used almost exactly the same, except you can ALSO refer to the ITEM objects as you would in an array (e.g. ItemList[0] would be the first ITEM object in the vector).

Share on other sites
_Austin    100
You can also do an array of objects.

Say you had a class named CItems:
// You'd declare it like you normally would declare an object but as an arrayCItems arrItem[100]; // use them the same as wellfor( int i = 0; i < 100; ++i )     std::cout << arrItem[i].GetName() << std::endl;// it's a healthy mixarrItem[0].Update();

Share on other sites
Lordimm    122
Alright, I got it to work - thanks for the help =).