Jump to content
  • Advertisement
Sign in to follow this  
null;

Text Based Game, Help With Inventory?

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

(Java)

 

Okay, so I recently started learning java because I honestly love the whole idea of programming. Not for school, but I just practice it in my free time. A while ago I started creating a text-based game, kind of like Zork (or Zorg, can't remember). I was also trying to figure out how I would get an inventory system to work. The player has to be able to access the inventory at any time throughout the game, and if he picks up an item it has to be in his inventory. I don't think I should share the code because there honestly isn't a lot to work with - I just need help with the concept itself. 

Share this post


Link to post
Share on other sites
Advertisement
Aside from Java, how much other programming experience do you have? For a simple text-based game like Zork, a simple array of strings should do. You can use an ArrayList. When picking up an item, you can add that item to the list. You can then use indexOf to query if the player has an item, and then use remove to remove the item with that index:

// populate items
ArrayList<String> items = new ArrayList<String>();
items.add("room key");
items.add("lemonade");
items.add("clothes");

// display initial contents of inventory
System.out.println("Inventory:");
for ( String item : items )
	System.out.println(item);

// query and remove an item
int idx = items.indexOf("room key");
if ( idx != -1 )
	items.remove(idx);
else
	System.out.println("not found");


// display new contents of inventory
System.out.println("Inventory:");
for ( String item : items )
	System.out.println(item);

Share this post


Link to post
Share on other sites

Aside from Java, how much other programming experience do you have? For a simple text-based game like Zork, a simple array of strings should do. You can use an ArrayList. When picking up an item, you can add that item to the list. You can then use indexOf to query if the player has an item, and then use remove to remove the item with that index:
 

// populate items
ArrayList<String> items = new ArrayList<String>();
items.add("room key");
items.add("lemonade");
items.add("clothes");

// display initial contents of inventory
System.out.println("Inventory:");
for ( String item : items )
	System.out.println(item);

// query and remove an item
int idx = items.indexOf("room key");
if ( idx != -1 )
	items.remove(idx);
else
	System.out.println("not found");


// display new contents of inventory
System.out.println("Inventory:");
for ( String item : items )
	System.out.println(item);

Ah, yeah, that'll do just fine. The game won't exactly be as 'straight forward' as Zork. The user would be able to open his inventory at any time, there would be item descriptions, the player would pick up items, etc. It's just that I'm slightly confused at the idea of ArrayList, and how I would go about actually ADDING items if the player picks it up - but I THINK I got it. Thanks for the help!

Share this post


Link to post
Share on other sites

The beauty of Object Based programming is polymorphism. Which means that a class that inherits from a similar parent may have traces of the same function, just with different techniques.


That being said... you are making a text based game like Zorg. You're going to have a variety of items each with their own description, use case, functions, etc.

 

 

It'll help if you make an Item class with data fields like.

class Item {
private:
    string Name;
    string Description;
    int amount;
public:
void Use;
string ShowDescription;
}

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!