Jump to content
  • Advertisement
Sign in to follow this  
dxFoo

C#: Storing items in a text-based rpg

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

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?

Share this post


Link to post
Share on other sites
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

Share this post


Link to post
Share on other sites
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"?

Share this post


Link to post
Share on other sites
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();

// Store
Item spear = new Item("Spear", 80);
Item dagger = new Item("Dagger", 30);

// Key and Value
items.Add(spear.Description, spear);
items.Add(dagger.Description, dagger);

// Retrieval
Item 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

Share this post


Link to post
Share on other sites
The way I implemented an item system in my rpg was this


// base class
public 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 */
);
}

// weapon
public class Weapon : Item {
public override void DoEffect( /* stuff */ ) {
// add code here to perform weapon related functions
// like subtracting target health
}
}

// potion example
public 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

Share this post


Link to post
Share on other sites
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?

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.

We are the game development community.

Whether you are an indie, hobbyist, AAA developer, or just trying to learn, GameDev.net is the place for you to learn, share, and connect with the games industry. Learn more About Us or sign up!

Sign me up!