C#: Storing items in a text-based rpg

Started by
4 comments, last by dxFoo 18 years, 8 months ago
If you're familiar with J2SE 5, they had enumerators that were class types. It was fairly easy to add item names, prices, descriptions, and so forth. For example,

enum Item
{
    dagger(10, "short description"), spear(80, "short description");

    private int price;
    private String description; 
 
    // constructor
    public Item(int price, string description)
    { 
        this.price = price;
        this.description = description; 
    }

    public void getPrice()
    {
        return price; 
    }
 
    public void setPrice(int price)
    {
        this.price = price;
    }

    // ...
}



My question: While knowing C# enumerators are value types, how can I get my C# console program to do something similar?
Advertisement
As far as I could find the in the MSDN, that's not supported. However, what I don't understand, why are you wanting to create it like that?

Why not just make a small class a la:
public class Item{    private string desc;    private int price;    public Item(string desc, int price)    {        this.desc = desc;        this.price = price;    }    public string Description    {        get { return desc; }    }    public int Price    {        get { return price; }    }}ArrayList itemList = new ArrayList();itemList.Add(new Item("Spear", 80));itemList.Add(new Item("Dagger", 10));


Of course, you don't have things like if (weapon == Item.dagger) or similar, you just pass around a reference to it. You could even replace the ArrayList with a HashTable and be able to search it quickly.

Toolmaker

That works fine.

itemList.Add(new Item("Spear", 80));

How can I access that object if it's thrown into an arraylist, like getting the price of item "spear"?
Is this for a look-up table (as opposed to a character's inventory)?

If so, I'd recommend a Hashtable. They link a key (think: index) with a value (think: class instance).

System.Collections.Hashtable items = new System.Collections.Hashtable();// StoreItem spear = new Item("Spear", 80);Item dagger = new Item("Dagger", 30);// Key and Valueitems.Add(spear.Description, spear);items.Add(dagger.Description, dagger);// RetrievalItem item = null;if(items.ContainsKey("Spear"))    item = (Item)items["Spear"]; // Just like an array, but using a string instead.if(item != null)   // Do something with it
The way I implemented an item system in my rpg was this

// base classpublic abstract class Item  { public string Name; // other common vars (description etc)... // constructor etc public abstract void DoEffect(  /* pass target health/mana, your health/mana etc */  );}// weaponpublic class Weapon : Item  { public override void DoEffect( /* stuff */ ) {  // add code here to perform weapon related functions  // like subtracting target health }}// potion examplepublic class Potion : Item { public override void DoEffect( /* stuff */ ) {  // add health/mana or whatnot }}// to add to a collection ArrayList myItems = new ArrayList();Item healthpot = new Potion();myItems.Add(healthpot);// to use item 1((Item) myItems[0]).DoEffect( /* stuff */ );


Just some ideas
Thanks for the help this far. I'm able to add/remove item objects from my inventory using an ArrayList, and I also have a detailed inventory method as well, using foreach() to sort through each item.

The most trouble is finding an item on the ArrayList object, place an index holder, then modify its details, then put it back in that index holder. Since object data will be changed often in the game, what's the easiest way of going about this?

This topic is closed to new replies.

Advertisement