Jump to content

  • Log In with Google      Sign In   
  • Create Account

#ActualEarthBanana

Posted 22 July 2013 - 11:45 AM

Well just think of your stuff in terms of objects.. it makes since though I would use a struct for the InventorySlot rather than a class - just because you dont really need a public/private interface for the inventory slot..

 

So.. you have an inventory that needs to be able to contain data and manipulate that data - sounds like a class is in order (like you have already)

 

and you have an inventory slot which needs to simply contain some data that can be easily accessed from within the inventory class - sounds like a struct is in order

 

I would do something like...

#define INVENTORY_SLOT_MAX 20

#include <vector>

class Inventory
{
	struct Slot
	{
		Slot(): item(NULL), count(0)  // make sure to initialize your data!
		{};

		Item * item;
		unsigned int count;
	};
public:

	Inventory()
	{
		inventorySlots.resize(INVENTORY_SLOT_MAX);  // in constructor resize the inventorySlots vector to the max inventory slot count
	};
	// Constructors/Destructors and functions to manipulate the inventory


private:

	std::vector<Slot> inventorySlots;  // A vector containing all of your inventory slots

};

please forgive if something is a bit syntactically incorrect i just whipped this up as a quick example of an approach you could take


#1EarthBanana

Posted 22 July 2013 - 11:44 AM

Well just think of your stuff in terms of objects.. it makes since though I would use a struct for the InventorySlot rather than a class - just because you dont really need a public/private interface for the inventory slot..

 

So.. you have an inventory that needs to be able to contain data and manipulate that data - sounds like a class is in order (like you have already)

 

and you have an inventory slot which needs to simply contain some data that can be easily accessed from within the inventory class - sounds like a struct is in order

 

I would do something like...

class Inventory
{
	struct Slot
	{
		Slot(): item(NULL), count(0)  // make sure to initialize your data!
		{};

		Item * item;
		unsigned int count;
	};
public:

	Inventory()
	{
		inventorySlots.resize(INVENTORY_SLOT_MAX);  // in constructor resize the inventorySlots vector to the max inventory slot count
	};
	// Constructors/Destructors and functions to manipulate the inventory


private:

	std::vector<Slot> inventorySlots;  // A vector containing all of your inventory slots

};

please forgive if something is a bit syntactically incorrect i just whipped this up as a quick example of an approach you could take


PARTNERS