Java-Gaming.org    
Featured games (81)
games approved by the League of Dukes
Games in Showcase (492)
Games in Android Showcase (112)
games submitted by our members
Games in WIP (556)
games currently in development
News: Read the Java Gaming Resources, or peek at the official Java tutorials
 
    Home     Help   Search   Login   Register   
Pages: [1]
  ignore  |  Print  
  Inventory System?  (Read 534 times)
0 Members and 1 Guest are viewing this topic.
Offline 04hockey

Junior Member


Medals: 2
Projects: 1
Exp: 2 years


Founder of Fanger Games.


« Posted 2014-04-24 00:54:40 »

Okay so I'm working on a game and I've been really conflicted recently on how I want to go about doing the inventory system.

What I mean is like how it actually works.

My friend suggested that each inventory item would have an XML file where you could load data from it, but I'm not sure how I would get the rendering to work for that and I'm not sure it's efficient. Along with that, some items (weapons) need to have functionality to them when they are selected/equipped so I'm not really sure how that would work with XML.

I myself had two ideas: Classes for each type of item with each their own rendering method, or Enum values for each item where a switch statement would decide what to render and what kind of functionality it would have, although that would create a ton of lag the more items that were added into the game.

So, if I could get some opinions and ideas on this, that would be lovely! Thanks!
Offline ctomni231

JGO Wizard


Medals: 99
Projects: 1
Exp: 7 years


Not a glitch. Just have a lil' pixelexia...


« Reply #1 - Posted 2014-04-24 01:11:36 »

In your case, I'd just do it all within hard code. Make a grid-like (double array) or menu (single array) system, then assign a class called Item to it, sort of like this.

1  
2  
3  
4  
5  
6  
7  
8  
9  
public class Item{

        //The type of item (can also be string)
       public int itemType;

        //The image icon... if manageable
       public Image icon;
        //All things describing the item goes here
}


1  
2  
3  
4  
5  
6  
7  
8  
9  
10  
public class Inventory{

       public Item[][] gridofItems;

       //or...

       public Item[] menuOfItems;

       //implement code that renders and controls items here    
}


This is extremely vague, but it takes you on the right path. You want to make sure that you set up Items in a way that you can visualize their place on the inventory sheet. The link below gives a little bit more information about these systems.

http://www.java-gaming.org/topics/the-modern-rpg-and-inventory-systems/32149/view.html

XML is not really for drawing an inventory. It is more like a way of saving it to a text file. This is only useful if you wanted to make a customizable inventory, or if you wanted to save it. However, you can just as easily use JSON, or Java serialization to do it. So, I'd just ignore that advice until you feel the need to save your inventory to a save file.

Offline 04hockey

Junior Member


Medals: 2
Projects: 1
Exp: 2 years


Founder of Fanger Games.


« Reply #2 - Posted 2014-04-24 01:14:32 »

I know how to make the inventory work, I am talking about loading the data for the inventory items, obviously I would have to load an image path from the XML and have my code render it based on where it is inside the inventory.

I am going to need to save it, so that's why I was thinking of hard coding it because I could make the objects serializable.
Games published by our own members! Check 'em out!
Legends of Yore - The Casual Retro Roguelike
Offline SwordsMiner

JGO Coder


Medals: 3
Projects: 2
Exp: 1 year


The one and only.


« Reply #3 - Posted 2014-04-24 02:45:06 »

I dont have the time to pseudo code everything for you right now  Roll Eyes, but you could make a item id enum, an abstract generic item type, inventory, and extend specific items on the generic item and make them load their stuff and give the item an id from the id enum. Then when you save just save it in some array that accords to the ids and then loading is vice versa. If you need a format use a premade one (my game is using notch's NBT), or simply put them in an array like:

0 1 0 0 0 1 0 1 0 1

and save it as a .txt

If you need a better point in the right direction I can pseudo code for you tomorrow or a lovely person here could extend off of my points.

If I made you laugh, helped you at all, or did something cool, I only ask that you smash that appreciate button with your nose.
Offline 04hockey

Junior Member


Medals: 2
Projects: 1
Exp: 2 years


Founder of Fanger Games.


« Reply #4 - Posted 2014-04-24 05:05:26 »

With using an Enum, I would need to have a switch statement to decide how to render the item, wouldn't this slow performance the more items I add?
Offline trollwarrior1
« Reply #5 - Posted 2014-04-24 05:16:55 »

Here is how I did it for one of my games.

Inventory is made up of like 8*5 inventory slots or something. Basically Item[] items=new Item[8*5];
Each slots contains an item. When I want to render inventory, I go through all the item slots and if there is an item in the slot, I do item[slotIndex].render();
I mean, what do you even want to ask?

If you're asking about item system itself, I made it like this. Every item extends Item.class. Every item has its own class. Every items needs to define few parameters like sprite used, name and some other custom ones. I also have a static instance of each item in my Item class.

When I want to add item to inventory, I just do items[index]=item and the item appears in the inventory.

If you want to have more customized items like each having each own's durability, special effects, you will need to create a class like 'SpecializedItem.class'. It would accept 'base item' as parameters. When you render specializeditem, render the base item, and render the effects you want on top of it.

It is probably really hard to understand, but the easiest way to do it, would be to actually do it, and not think about it. Think about performance and saving/loading later.
Offline 04hockey

Junior Member


Medals: 2
Projects: 1
Exp: 2 years


Founder of Fanger Games.


« Reply #6 - Posted 2014-04-24 06:23:13 »

What I really wanted to know was whether I should have the Arrays hold Enum values or actual Objects.

But I'm pretty positive that actually having Objects is probably the better option. Thanks everyone!
Offline cylab

JGO Ninja


Medals: 45



« Reply #7 - Posted 2014-04-24 15:10:25 »

Enum values in java are Objects...
- http://docs.oracle.com/javase/tutorial/java/javaOO/enum.html
- http://javahowto.blogspot.de/2008/04/java-enum-examples.html
- http://howtodoinjava.com/2012/12/07/guide-for-understanding-enum-in-java/

Mathias - I Know What [you] Did Last Summer!
Offline 04hockey

Junior Member


Medals: 2
Projects: 1
Exp: 2 years


Founder of Fanger Games.


« Reply #8 - Posted 2014-04-24 16:41:59 »

I know this, but creating a class specifically for each item I could have rendering properties, update properties, ect. for each item.
Offline cylab

JGO Ninja


Medals: 45



« Reply #9 - Posted 2014-04-24 23:08:11 »

Well you can do that with enums as well, but I don't want to talk you into this Wink

Other than that, I don't know if I get your problem.

I would say, just create a Player class with an Inventory property that can have a ArrayList<Item> in there. Make your subclasses of Item, fill in the desired functionality and resource properties. If you need to save the player state, either serialize it yourself or use 3rd party libs like xstream to make things easy.

To avoid serializing everything and the kitchen sink, make all fields transient that can be reconstructed dynamically after loading (like textures, sound clips etc.), so only serialize primitive types and simple datamodel like objects.

Then if you need to load the player state, reconstruct the transient fields again. To do that you'll need to implement readResolve() or readObject(instream)

To render your inventory, create a render() method in your Inventory class that iterates over the ArrayList<Item> and render each Item at the desired position. You can either implement a render(x,y) method in your Item class, but I would probably just get an Image from the Item and put the render-code in the Inventory class.

To handle equiping your Player, you can create a special ArrayList<Item> named slots or something in your Inventory. Create another class like PlayerAttributes that hold armor, strength etc. properties. Every time the content of the slots change, reset the PlayerAttributes and iterate over all Items in the slots, calling a method like equip(PlayerAttributes attributes) and let the Items modify the given attributes object.

Just give it a shot, even if the post is not fully detailed. You'll find out the bits and pieces on your way.

Btw. don't think about something being slow when you are dealing with <10000 objects. Just make it so that you can easy manage the code und functionality. Think about performance, when you have performance problems (or you plan something with >10000 objects/items/entities)


Mathias - I Know What [you] Did Last Summer!
Pages: [1]
  ignore  |  Print  
 
 

 

Add your game by posting it in the WIP section,
or publish it in Showcase.

The first screenshot will be displayed as a thumbnail.

Nickropheliac (16 views)
2014-08-31 22:59:12

TehJavaDev (23 views)
2014-08-28 18:26:30

CopyableCougar4 (33 views)
2014-08-22 19:31:30

atombrot (42 views)
2014-08-19 09:29:53

Tekkerue (41 views)
2014-08-16 06:45:27

Tekkerue (35 views)
2014-08-16 06:22:17

Tekkerue (26 views)
2014-08-16 06:20:21

Tekkerue (37 views)
2014-08-16 06:12:11

Rayexar (73 views)
2014-08-11 02:49:23

BurntPizza (49 views)
2014-08-09 21:09:32
List of Learning Resources
by Longor1996
2014-08-16 10:40:00

List of Learning Resources
by SilverTiger
2014-08-05 19:33:27

Resources for WIP games
by CogWheelz
2014-08-01 16:20:17

Resources for WIP games
by CogWheelz
2014-08-01 16:19:50

List of Learning Resources
by SilverTiger
2014-07-31 16:29:50

List of Learning Resources
by SilverTiger
2014-07-31 16:26:06

List of Learning Resources
by SilverTiger
2014-07-31 11:54:12

HotSpot Options
by dleskov
2014-07-08 01:59:08
java-gaming.org is not responsible for the content posted by its members, including references to external websites, and other references that may or may not have a relation with our primarily gaming and game production oriented community. inquiries and complaints can be sent via email to the info‑account of the company managing the website of java‑gaming.org
Powered by MySQL Powered by PHP Powered by SMF 1.1.18 | SMF © 2013, Simple Machines | Managed by Enhanced Four Valid XHTML 1.0! Valid CSS!