Jump to content

  • Log In with Google      Sign In   
  • Create Account

FREE SOFTWARE GIVEAWAY

We have 4 x Pro Licences (valued at $59 each) for 2d modular animation software Spriter to give away in this Thursday's GDNet Direct email newsletter.


Read more in this forum topic or make sure you're signed up (from the right-hand sidebar on the homepage) and read Thursday's newsletter to get in the running!


#ActualKhaiy

Posted 20 March 2013 - 07:10 PM

Thank you both for the great advice. Would it be easier to store the players inventory in a multidimensional array in order to store the objects name and price?

 

 

It depends on how you want to represent the items that would be in the inventory, but that sounds a little over-engineered to me. What you are describing would work, but I would prefer either a Dictionary with a string key and an int value or a distinct Item class with a string field for the name and an int field for the price.

I can flesh this out a bit more, but later; my phone limits my post quality :/

 

EDIT: Home now, let me expand a little.

 

I think that creating an Item class would be a good, intuitive, direct approach to take. That way all of the information relevant to the items in your program are grouped together to be conveniently used as you need.

 

If you had something like:

 

 

public class Item
{
string name;
 
int price;

}

 

then you could have a List<Item> to represent inventory and another List<Item> to represent the store. An array of items (Item[]) would also be fine, though the size would be fixed.

 

The player's inventory doesn't seem like it would benefit from having item prices, so unless the price is included as a field in an Item class I wouldn't include it in the inventory at all. If you don't use an Item class, I would have the player's inventory be a List<string> or an array of strings, similar to above. The Buy interface is the only place that really needs the prices, so you could use a Dictionary<string, int> where the string is the item name and the int is the price as a field in the Buy class definition.


#2Khaiy

Posted 20 March 2013 - 07:07 PM

Thank you both for the great advice. Would it be easier to store the players inventory in a multidimensional array in order to store the objects name and price?

 

 

It depends on how you want to represent the items that would be in the inventory, but that sounds a little over-engineered to me. What you are describing would work, but I would prefer either a Dictionary with a string key and an int value or a distinct Item class with a string field for the name and an int field for the price.

I can flesh this out a bit more, but later; my phone limits my post quality :/

 

EDIT: Home now, let me expand a little.

 

I think that creating an Item class would be a good, intuitive, direct approach to take. That way all of the information relevant to the items in your program are grouped together to be conveniently used as you need.

 

If you had something like:

 

public class Item
{
string name;
 
int price;

}

 

then you could have a List<Item> to represent inventory and another List<Item> to represent the store. An array of items (Item[]) would also be fine, though the size would be fixed.

 

The player's inventory doesn't seem like it would benefit from having item prices, so unless the price is included as a field in an Item class I wouldn't include it in the inventory at all. If you don't use an Item class, I would have the player's inventory be a List<Item> or an array, as above. The Buy interface is the only place that really needs the prices, so you could use a Dictionary<string, int> where the string is the item name and the int is the price as a field in the Buy class definition.


#1Khaiy

Posted 20 March 2013 - 06:09 PM

Thank you both for the great advice. Would it be easier to store the players inventory in a multidimensional array in order to store the objects name and price?


It depends on how you want to represent the items that would be in the inventory, but that sounds a little over-engineered to me. What you are describing would work, but I would prefer either a Dictionary with a string key and an int value or a distinct Item class with a string field for the name and an int field for the price.

I can flesh this out a bit more, but later; my phone limits my post quality :/

PARTNERS