• Announcements

    • khawk

      Download the Game Design and Indie Game Marketing Freebook   07/19/17

      GameDev.net and CRC Press have teamed up to bring a free ebook of content curated from top titles published by CRC Press. The freebook, Practices of Game Design & Indie Game Marketing, includes chapters from The Art of Game Design: A Book of Lenses, A Practical Guide to Indie Game Marketing, and An Architectural Approach to Level Design. The GameDev.net FreeBook is relevant to game designers, developers, and those interested in learning more about the challenges in game development. We know game development can be a tough discipline and business, so we picked several chapters from CRC Press titles that we thought would be of interest to you, the GameDev.net audience, in your journey to design, develop, and market your next game. The free ebook is available through CRC Press by clicking here. The Curated Books The Art of Game Design: A Book of Lenses, Second Edition, by Jesse Schell Presents 100+ sets of questions, or different lenses, for viewing a game’s design, encompassing diverse fields such as psychology, architecture, music, film, software engineering, theme park design, mathematics, anthropology, and more. Written by one of the world's top game designers, this book describes the deepest and most fundamental principles of game design, demonstrating how tactics used in board, card, and athletic games also work in video games. It provides practical instruction on creating world-class games that will be played again and again. View it here. A Practical Guide to Indie Game Marketing, by Joel Dreskin Marketing is an essential but too frequently overlooked or minimized component of the release plan for indie games. A Practical Guide to Indie Game Marketing provides you with the tools needed to build visibility and sell your indie games. With special focus on those developers with small budgets and limited staff and resources, this book is packed with tangible recommendations and techniques that you can put to use immediately. As a seasoned professional of the indie game arena, author Joel Dreskin gives you insight into practical, real-world experiences of marketing numerous successful games and also provides stories of the failures. View it here. An Architectural Approach to Level Design This is one of the first books to integrate architectural and spatial design theory with the field of level design. The book presents architectural techniques and theories for level designers to use in their own work. It connects architecture and level design in different ways that address the practical elements of how designers construct space and the experiential elements of how and why humans interact with this space. Throughout the text, readers learn skills for spatial layout, evoking emotion through gamespaces, and creating better levels through architectural theory. View it here. Learn more and download the ebook by clicking here. Did you know? GameDev.net and CRC Press also recently teamed up to bring GDNet+ Members up to a 20% discount on all CRC Press books. Learn more about this and other benefits here.
Sign in to follow this  
Followers 0
stein102

Java RPG Item system

15 posts in this topic

Right now I'm working on a RPG in Java and need to implement an item/inventory system. The only way I can think of is to create a class for each item I would have in the game and create an instance of each when the game initializes, then I could make a linkedList or something that you would use for the inventory.

The problem is, if I have say 100+ items this would be very inefficient. Does anyone else have any ideas that would help with either the way I could set up my items or the way I would do the inventory?

Thanks
0

Share this post


Link to post
Share on other sites
[quote name='stein102' timestamp='1337754093' post='4942448']
, then I could make a linkedList or something that you would use for the inventory.

The problem is, if I have say 100+ items this would be very inefficient.
[/quote]
For doing what ?
Choosing the right collection class is important, but that all depends on what you want to.
0

Share this post


Link to post
Share on other sites
Well it's a 2d RPG. The inventory will have to support adding/removing items, rearranging items inside the inventory. Think like, Diablo2 but each item will only take up one space. Basically that's the functionality I want to achieve.
0

Share this post


Link to post
Share on other sites
Then a linked list is a valid choice. Rearranging items will happen too infrequently to be of any significance.
0

Share this post


Link to post
Share on other sites
That makes sense, I'll probably go with a linked list then. What about the item system though? Got any ideas?
0

Share this post


Link to post
Share on other sites
[quote name='stein102' timestamp='1337760366' post='4942471']
What about the item system though? Got any ideas?
[/quote]
Probably, if I knew more requirement details...
0

Share this post


Link to post
Share on other sites
You could have the ordering data in the Item class instead, if that floats your boat... Then reordering wouldn't involve changing the container structure, but just an int or two.
0

Share this post


Link to post
Share on other sites
It also depends on how frequently your accessing your data, if you have a hundred items that are only iterated thru once in a while like when the character is looking thru inventory then you have nothing to worry about. Now if that array is being accessed constantly lets say every render call for whatever reason then you might want to look at an alternate approach but even then depending on the actions preformed on the array it might not be an issue.

Like people previously said in this thread it all depends on what you need and what your doing so we would need more details.
1

Share this post


Link to post
Share on other sites
The more details you give, the better.
-> How big is the inventory? If every Item takes maximum of 1 'spot' in your inventory, you could go for an 2 dimentional array.

[CODE]
Item Inventory[HorizontalSizeOfInventory][VerticalSizeOfInventory];

[/CODE]

You can then loop them and everywhere Item[x][y] != null, you display that item.

For the Item structure, I'd work with a system like this:
Have one base Item class with the basics that every Item has; If every item has the same, leave it this way.
If some Items are different (for instance, Health items, Weapon Items ...) inherit from the Item base class and use the [url="http://en.wikipedia.org/wiki/Factory_method_pattern"]factory design pattern[/url]. This allows you to 'load' your items from a database or XML. Don't create a class for every Item, as it makes your code to 'implementation-wise' and adding Items will be more difficult.

The following UML can make things clear: make the following. Then create some class that will spawn the items (factory pattern) and add them to the inventory.

[img]http://i49.tinypic.com/x10oyt.jpg[/img]

Of course, the array idea only works with everything of size 1. Otherwise you could use some system that has a 'size' field in the Item class and prevent from inserting new items to zones which are taken :)
1

Share this post


Link to post
Share on other sites
[quote name='The_Neverending_Loop' timestamp='1337787072' post='4942591']
It also depends on how frequently your accessing your data, if you have a hundred items that are only iterated thru once in a while like when the character is looking thru inventory then you have nothing to worry about. Now if that array is being accessed constantly lets say every render call for whatever reason then you might want to look at an alternate approach but even then depending on the actions preformed on the array it might not be an issue.

Like people previously said in this thread it all depends on what you need and what your doing so we would need more details.
[/quote]

I am going to want to access them during each render call. The inventory is always on screen.

Also, as Miklas said I could use XML or a database for storing my items. I suppose I will need to do some reading in regards to that if I'm to go with that approach, I'm not familiar with either of them, but willing to learn. What would be the advantage of one over the other?

Thanks
0

Share this post


Link to post
Share on other sites
[quote name='stein102' timestamp='1337809353' post='4942700']
[quote name='The_Neverending_Loop' timestamp='1337787072' post='4942591']
It also depends on how frequently your accessing your data, if you have a hundred items that are only iterated thru once in a while like when the character is looking thru inventory then you have nothing to worry about. Now if that array is being accessed constantly lets say every render call for whatever reason then you might want to look at an alternate approach but even then depending on the actions preformed on the array it might not be an issue.

Like people previously said in this thread it all depends on what you need and what your doing so we would need more details.
[/quote]

I am going to want to access them during each render call. The inventory is always on screen.

Also, as Miklas said I could use XML or a database for storing my items. I suppose I will need to do some reading in regards to that if I'm to go with that approach, I'm not familiar with either of them, but willing to learn. What would be the advantage of one over the other?

Thanks
[/quote]

Well those are just methods of storage, like where the data will live and in what form which seems to be a completely different issue then the one u first stated. If the items are going to be always be displayed and constantly accessible and there will be 100 possible items, then there are no shortcut around, you might at times have to iterate through all 100, at which point a LinkedList is perfectly fine.

Dont worry to much either about it being efficient or not, optimizing your code should come after you already got something working. You might THINK your saving time or playing it smarter by trying to come up with efficient solutions before they become a problem, but thats exactly the issue you are trying to come up with solutions to problems that don't exist yet.

Once your game is completed you might not have any significant issues with speed, and if you do! I highly doubt a 100 item inventory is going to be the cause of it all. Long story short try not to worry to much about smaller things and try just focusing on the bigger picture, its the best thing you can do in the long run. I say just go with the LinkedList and have fun coding! [img]http://public.gamedev.net//public/style_emoticons/default/biggrin.png[/img] Edited by The_Neverending_Loop
0

Share this post


Link to post
Share on other sites
Well I want to figure out how to store the data too. Should I just make a class for each item then? Idk...
0

Share this post


Link to post
Share on other sites
Well if you refer to the diagram provided by Miklas....
[img]http://i49.tinypic.com/x10oyt.jpg[/img]

This is the best way to do it. You should look into polymorphism that way you can make your code easier to use and follow. By creating a super class Called say Items, and creating other classes that implement or extend from Items to represent their special functions like "HealthItem" or "WeaponItem", then you can call a common method they both share say "use()" and it will use the appropriate function for that particular health item or weapon item.

As for the storage medium, its honestly up to you to decide whats best for you. But all it basically is meant to do is pass the arguments to one of your Item Constructor in order to recreate that object in your game, so whether you get your data from a SQL Database, or some XML file located on your hard drive, its all up to you.

One thing I would say tho is that if this is a game you install on your computer that you dont play with anyone else you can go with XML if you'd like, if not and there is some kind of networking involved use a Database.
0

Share this post


Link to post
Share on other sites
You should find a way to store your data in a way that you can read it, [i]without[/i] having to recompile. This means that you should have some files (XML, INI, TXT...) or database that contains the items.
Why?[list]
[*]Because updating and adding Items is nothing to worry about then: you can then send updates by sending a new [url="http://www.java-tips.org/java-se-tips/javax.xml.parsers/how-to-read-xml-file-in-java.html"]XML[/url]/[url="http://www.rgagnon.com/javadetails/java-0024.html"]INI[/url]/TXT.. file instead of having to send a whole new build to your clients. You could also [url="http://en.wikipedia.org/wiki/Serialize"]serialize[/url] your classes. This means that you create your classes once, and them saves them to disk to be loaded later. ([url="http://java.sun.com/developer/technicalArticles/Programming/serialization/"]tutorial[/url]) The advantage of this is that your code is harder to manipulate (as people can 'cheat' by editing your XML/INI's) but you have to do more work when creating your serialized objects.
[*]You should always program to an interface and not to an implementation. What's the difference? With an interface (look at my UML schema) you can reuse your code. You can then just give new models & new 'item'-files to your clients and you have a whole new game - without touching your source code. You could even do this for your GUI! (For example the source engine does this (known from counter-strike:source, gmod, half-life2, team fortress, portal...). They have created a way to parse '.res' files (see attachement, ChatScheme.txt - this file is from a new GUI for Counter-Strike:Source). By editing nothing else then these files, they have created a whole new GUI)
[/list]
[img]http://screenshot.xfire.com/s/117027243-3.jpg[/img]
Default CSS Gui


[img]http://archer.gamebanana.com/img/ss/guis/_2183-.jpg[/img]
[url="http://gamebanana.com/guis/24676"]HolmGUI[/url]

So your Java RPG engine should be capable of parsing files like this. Of course, a 'basic'(hardcoded) system (like the_neverending_loop suggested) is fine, and then you can expand it to be able to 'parse' the GUI. The more you keep your engine separated from your game, the more you can reuse it in future projects.
0

Share this post


Link to post
Share on other sites
Regarding persistence, I'd start with simply storing them serialized to a file. That is a minimum amount of coding work and you won't have to change any code as your game evolves, you add new item types, change existing ones etc. Once your game starts maturing and if you have certain requirements on the file format (e.g. editable in a text editor) you can rewrite this part.

And just to throw another perspective into the discussion: If the items don't carry individual state, or very simple state, you may want to invert the object model to instead being compartment-centric: Your inventory holds 100 item compartments. Each compartment has a "held item type" and possibly some parameters (such as item count). If you can stack items, e.g. 100 bullets in a single compartment and the bullets don't have individual state, this is perhaps the better approach. (Then again, if all you'll ever have are 100 inventory slots in a single player game, processing and memory usage will probably not be headache either way!)
0

Share this post


Link to post
Share on other sites
If I were you, I'd use a 2d array of Item referenes. Item would be a sealed/final class that in other words cannot be inherited. It would have fields like name, graphicsId, isStackable, stackedAmount, equipableOnSlots, listOfEnhancements and listOfActions (or just a single action).

You can also have a reference to a ItemBaseType which is all of that excluding listOfEnhancements and stackedAmount. You may also need some additional state such as durability, action cooldowns etc depending on your game.

listOfEnhancements is a list of stat changes, +20 mana, +4 strength etc. Whenever you change equipment/inventory, you create a per-character combined list of all active enhancements which you can then use for your calculations (instead of looping through each item multiple times each frame).

listOfActions is a list of Actions which is ways to use your item. In Diablo 2 for example, a staff could grant the user spells and if I remember correctly, it could sometimes even grant multiple additional spells. This might not apply to your game. If there's a single action (right-clicking a potion to drink it), you can use a single action-field in the Item class or the ItemBaseType class if you use that.

It's very easy to look at inheritance/polymorphism for stuff like this, but in my experience it often creates problems with serialization/network communication as well as code structure. What if an item has two actions? Or what if two different items have the same (or very similar) actions? For additional info and tips, google the strategy pattern.
0

Share this post


Link to post
Share on other sites

Create an account or sign in to comment

You need to be a member in order to leave a comment

Create an account

Sign up for a new account in our community. It's easy!


Register a new account

Sign in

Already have an account? Sign in here.


Sign In Now
Sign in to follow this  
Followers 0