aspose file tools*
The moose likes Game Development and the fly likes Inventory Assistance Big Moose Saloon
  Search | Java FAQ | Recent Topics | Flagged Topics | Hot Topics | Zero Replies
Register / Login


Win a copy of Spring in Action this week in the Spring forum!
JavaRanch » Java Forums » Java » Game Development
Bookmark "Inventory Assistance" Watch "Inventory Assistance" New topic
Author

Inventory Assistance

M Wallace
Greenhorn

Joined: Jul 28, 2012
Posts: 3

Hello to all,

I am working on an inventory system for a text based RPG. I need to be able to have a max inventory size, add items, remove items, and allow some items to be stacked (and by extension, a way to determine if an item can be stacked, or if it needs to go into a new inventory slot). I have two main questions for this posting. The first deals with my design choice, and the second about a specific problem I am having.

First, my design choice. I have two classes I am using (below), an Item class and an InventoryItem class. Item is going to be a base class that all items in the game will extend. InventoryItem is the actual inventory system. I have started out by creating an array of Items and initializing the array to [25] which is the current inventory size we are using. I have added methods to show the current inventory, and to add and remove items. My question is whether or not an array should be used or if I should look into list or collections more. I know this is a very general question, but I am hoping for someone with more experience to let me know if this way is feasible, or if it may be better to attempt another way.

Second, I am having some trouble with if/else. In the InventoryItem class, inside the method addItem, I am using an if/else block. The first if statement checks to see if the item being passed in is stackable. If the item is stackable, then code to add the item to the stack. ELSE, just find the next empty spot and add the item. This else block doesn't execute and I am having trouble figuring out why. My expectation of my code is: If the item is stackable, try and stack it, if it can't be stacked, just add it to an empty slot (if available), OR If the item is not stackable, just add it to the next empty spot (if available).

Thank you in advance for any help!

Mike




>


The two people in this world are those that do, and those that don't.
fred rosenberger
lowercase baba
Bartender

Joined: Oct 02, 2003
Posts: 11417
    
  16

First, there is no reason to ever do this (and actually quite a few reasons NOT to do this):



Just do this:



you are less prone to making mistakes (like leaving off one of the '=' characters).

Next, if it isn't doing what you want, put in a bunch of System.out.println statements to see what it IS doing. make sure you boolean really IS false just before you test it.


There are only two hard things in computer science: cache invalidation, naming things, and off-by-one errors
M Wallace
Greenhorn

Joined: Jul 28, 2012
Posts: 3

Thank you Fred!

I have changed my code to use if(item.stackable) as opposed to if(item.stackable==true) and I can now get my Else block to execute. I am having some trouble getting the statements after I check item.stackable to execute, but I want to try a few things before I post back about that as I think I should be able to accomplish what I need from here.....hopefully lol.

Mike

Richard Henderson
Ranch Hand

Joined: Feb 18, 2013
Posts: 42
Any chance you can post the working code? And could I use some of it in my own text RPG?

I'm not at that point yet but I want to put in an inventory system too. Probably only 10 items and not stackable though but wouldn't hurt to know how to make items stackable for a future project.
 
I agree. Here's the link: http://aspose.com/file-tools
 
subject: Inventory Assistance