Java-Gaming.org Hi !
Featured games (81)
games approved by the League of Dukes
Games in Showcase (513)
Games in Android Showcase (119)
games submitted by our members
Games in WIP (576)
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  
  Player Inventory  (Read 5066 times)
0 Members and 1 Guest are viewing this topic.
Offline Keteri

Junior Newbie




pwning n00bs in high heels since 1986


« Posted 2009-03-30 04:28:47 »

Hello~!

I'm new to Game Programming using Java, and I'm sort of kind of just playing around with various tutorials to grasp the concepts of using Java to create Games.
There is one thing I'm having a little trouble with, and I was hoping someone could shed some light for me.

First
Are there any tutorials specifically to learn how to create RPG games using Java?
I've tried to use Google, but of the 1928390182630916208 billion websites on game programming, only about 200 are written in Java, and of those 200, only about 3 are specific to RPG games >.< (common sense, and yes, im exaggerating)

Disclaimer: Yes, I do realize Java is already not the best option for creating games, let alone RPGs. I simply enjoy the challenge; and yes, I do have knowledge of the Java (and many other) language(s).

Second
I realize that's a TON of code (well, at least a good chunk), so I won't ask anyone to "code for me", but can someone perhaps give me a detailed, step by step explanation on how I should go about creating an inventory class that is easy and effective?

I suppose I could create an inventory method:
inventory( ADD, "Herb", 1); //add or delete, name, quantity

To add the item, I would create an object of class Item:
Item item = new Item( "Herb", "Description of Herb", 50, 200, 0  ); //name, description, buy value, baseValue, type

How would I go about deleting that?
Item item = new Item( "", "", 0, 0, 0 ); <== override object item?
^ that wouldn't delete it though right? If I were to view the player's inventory, I think I'd just see a blank line. rawr

Offline zammbi

JGO Coder


Medals: 4



« Reply #1 - Posted 2009-03-30 05:07:53 »

Welcome to Java game programming.
Java2D is fine for games if your not expecting to do a lot of alpha effects or particles. Otherwise I suggest using a library that supports such things.

This tut should help you:http://fivedots.coe.psu.ac.th/~ad/jg/

For items in my game I have a list of Item objects in the player class, so removing a item would just simply remove that item from the list.

1  
HashTable<Item,String> items = new HashTable< Item, String >();

So you could make a method like:

1  
2  
3  
4  
void RemoveItemByID(String ID){
items.remove(ID);

}

Current project - Rename and Sort
Offline gouessej
« Reply #2 - Posted 2009-03-30 05:20:16 »

Hi!

Yes, I do realize Java is already not the best option for creating games, let alone RPGs. I simply enjoy the challenge; and yes, I do have knowledge of the Java (and many other) language(s).
It is only your opinion based on prejudices. Some people here (at least me) think that Java is the best fitted language for cross-platform game programming.

Look at JCRPG, it is really impressive:
http://jcrpg.blogspot.com/

Games published by our own members! Check 'em out!
Legends of Yore - The Casual Retro Roguelike
Offline cylab

JGO Ninja


Medals: 52



« Reply #3 - Posted 2009-03-30 09:39:02 »

1  
HashTable<Item,String> items = new HashTable< Item, String >();

So you could make a method like:

1  
2  
3  
4  
void RemoveItemByID(String ID){
items.remove(ID);

}

I think the entry types are swapped here!?

If you need to have multiple items under a specific name/id (like in some RPG inventories), you could just store a List in the HashMap:

1  
2  
3  
4  
5  
6  
7  
8  
9  
10  
11  
12  
13  
14  
15  
16  
17  
18  
19  
20  
21  
22  
23  
24  
25  
public class Inventory
{
  HashMap<String,ArrayList<Item>> items = new HashMap<String,ArrayList<Item>>();

  public void add(Item item)
  {
     String name = item.getName();
     ArrayList<Item> itemGroup = items.get(name);
     if(itemGroup==null) // add a new group, if none exist, yet
     {
       itemGroup = new ArrayList<Items>();
       items.put(name,itemGroup);
     }
     itemGroup.add(item);
  }

  public void remove(String name){
     String name = item.getName();
     ArrayList<Item> itemGroup = items.get(name);
     if(itemGroup==null) return;
     itemGroup.remove(name);
     // remove an empty group
     if(itemGroup.size()) == 0;
     items.remove(name);
  }


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

JGO Coder


Medals: 4



« Reply #4 - Posted 2009-03-30 10:47:53 »

Woops yes your right there swapped, my browser is missing an inbuilt Java IDE Roll Eyes
I have the quantity value in the Item object itself which removes the need for that ArrayList, unless there's another reason to have the same ID?

Current project - Rename and Sort
Offline cylab

JGO Ninja


Medals: 52



« Reply #5 - Posted 2009-03-30 11:17:15 »

Woops yes your right there swapped, my browser is missing an inbuilt Java IDE Roll Eyes
I have the quantity value in the Item object itself which removes the need for that ArrayList, unless there's another reason to have the same ID?
Nope. It's just that if you have a stack of herbs or something you usually only remove one item from this stack, if you use it. Your removeByID would remove the whole stack.

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

JGO Coder


Medals: 4



« Reply #6 - Posted 2009-03-30 11:55:32 »

Oh yes in my removeByID method would remove them all, I wasn't thinking about quantities when I posted that.
So yes that would need to be modified.

Current project - Rename and Sort
Offline Keteri

Junior Newbie




pwning n00bs in high heels since 1986


« Reply #7 - Posted 2009-03-30 16:06:48 »

Wow, thank you all so much for the tips and linkies~!
I wasn't expecting so much help so quickly, much appreciated

BTW
Welcome to Java game programming.
Java2D is fine for games if your not expecting to do a lot of alpha effects or particles. Otherwise I suggest using a library that supports such things.

This tut should help you:http://fivedots.coe.psu.ac.th/~ad/jg/
Thanks for the warm welcome~! Have a /cupcake Cheesy

I'm actually familiar with this website (and have purchased the book, void of CD, so I have to refer to this site continuously, considering the author doesn't have a single page in the book that "reviews" the code for that chapter - goodness me)

The website is currently experiencing a page load error (perhaps they're simply doing maintenance) so if any curious readers hop into this thread and click the link - it isn't broken.

tyvm 4 all ur help!

Hi!
It is only your opinion based on prejudices. Some people here (at least me) think that Java is the best fitted language for cross-platform game programming.

Look at JCRPG, it is really impressive:
http://jcrpg.blogspot.com/
I concur with you, I've also previously seen JCRPG (and I've been told Runescape was created in Java by a 15 year old boy). The only problem is, of the 8709816-238162-93861-9236-1` million websites devoted to game programming, only ~200 are written in java, and only 3 are RPGs (previously exaggerated example).
So, yes, I suppose my opinion is based on prejudices and Chapter 1 of Killer Game Programming in Java. :\

I do think Java has AMAZING capabilities (that's for sure) - after all my online bank allows me to scan my paycheck straight into my checking account (that's pretty awesomesauce imo)
And I would LOVE to learn more about the language and how I can apply it to creating games (regardless if i currently think I could create a game much easier in C++ or visual basic, or the XNA studio, or any of the microsoft visual studio programs... but probably best to stick with C++)

Also Many Thanks to
cylab & zammbi for collaborating on the best way for me to go about creating an inventory. many /cupcakes to you two~!
Offline cylab

JGO Ninja


Medals: 52



« Reply #8 - Posted 2009-03-30 16:20:55 »

The website is currently experiencing a page load error (perhaps they're simply doing maintenance) so if any curious readers hop into this thread and click the link - it isn't broken.

Hmm, worked a couple of minutes before - maybe you broke it Wink Ahh, works again...

Quote
Also Many Thanks to
cylab & zammbi for collaborating on the best way for me to go about creating an inventory. many /cupcakes to you two~!

You are welcome. Usually java-gaming.org is an extremly friendly and helpfull community.

Btw. interesting video blog Wink


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

Junior Newbie




pwning n00bs in high heels since 1986


« Reply #9 - Posted 2009-03-30 16:50:38 »

Hmm, worked a couple of minutes before - maybe you broke it Wink Ahh, works again...

You are welcome. Usually java-gaming.org is an extremly friendly and helpfull community.

Btw. interesting video blog Wink

HAHAHAHAHAHHAHA yea, im a little cooky Tongue
I should really take that off there >.< but it still makes people laugh, so I suppose it's going to stay up Cheesy
Games published by our own members! Check 'em out!
Legends of Yore - The Casual Retro Roguelike
Offline Eli Delventhal

JGO Kernel


Medals: 42
Projects: 11
Exp: 10 years


Game Engineer


« Reply #10 - Posted 2009-03-30 17:02:49 »

Yeah I'd agree with everyone here that you should be treating your inventory more as a list than as an array. In your original idea over how to do it, you seemed to have been treating it this way by needing to fill in blank spaces when an item was taken away. Instead, just use a list and then print it out however you want in your inventory menu. If you think of it that way then you don't really need to worry about any of the outliers - it's just list access, which is easy.

You could use a HashMap also like zammbi recommended, but I don't necessarily see a reason for this because typically item access will be chosen by the user and therefore can be index-based. If you have a lot of event-stuff happening based on items (like if(playerHasItem("Doggy") { foo(); } ) then it's logical to use a HashMap so you can get that O(1) access. But any time a player is selecting an item, using one from an inventory screen, or even if all the items are being drawn out in the inventory, then a HashMap will be slower and more convoluted to use in terms of accessing elements.

In the end I think it really depends upon the uses you're going to need to get out of it.

See my work:
OTC Software
Offline cylab

JGO Ninja


Medals: 52



« Reply #11 - Posted 2009-03-30 17:54:05 »

I think one should use a HashMap just because of it's simplicity. Speed won't likely be an issue with an inventory...

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

JGO Coder


Medals: 5



« Reply #12 - Posted 2009-03-30 18:11:24 »

I am making an rpg aswell. This is gunn abe helpful when/if I make an inventory.
Offline SunshineKiller

Junior Duke





« Reply #13 - Posted 2009-03-31 05:15:08 »

hi, you may want to check this out
http://roguebasin.roguelikedevelopment.org/index.php?title=Main_Page

rouges arnt necessarily intense rpgs, but are and can be mini-rpgs and can also be dungeon crawlers.

The wiki goes through how random maps and maps can be created, also here is another article for your read,

http://www-cs-students.stanford.edu/~amitp/gameprog.html#tiles
 i find this website very interesting Smiley

<b>Check out my Development Blog:</b> <a href="http://www.scottscreations.com">Scotts Creations</a> | <b>Games in Development: </b> <a href="http://mechwarfare.scottscreations.com">Mech Warfare</a> | Mech Warfare: Facebook Edition | Game Master
Offline kevglass

JGO Kernel


Medals: 186
Projects: 24
Exp: 18 years


Coder, Trainee Pixel Artist, Game Reviewer


« Reply #14 - Posted 2009-03-31 06:36:52 »

If you treat the inventory as a list you'll lose position information. If you imagine your inventory on screen as a grid of boxes then if you treat it as a list, when you remove an item all the other items will slide through the grid to close the now empty space.

There's nothing wrong with using an array for inventory. Something like:

1  
2  
3  
4  
5  
6  
7  
8  
9  
10  
11  
12  
13  
14  
15  
16  
17  
18  
19  
20  
21  
22  
23  
24  
25  
26  
27  
28  
29  
30  
31  
32  
33  
34  
35  
36  
37  
38  
39  
40  
41  
42  
43  
44  
45  
public class Inventory {
     private static final int MAX_INVENTORY_SIZE = 100;

     private Item[] items = new Item[MAX_INVENTORY_SIZE];

     public Inventory() {
     }

     public boolean addItem(Item item) {
           int slot = findFreeSlot();
           if (slot >= 0) {
               items[slot] = item;
           }
     }

     public Item removeItem(Item item) {
           for (int i=0;i<items.length;i++) {
                if (items[i].equals(item)) {
                      Item temp = items[i];
                      items[i] = null;
                      return temp;
                }
            }

            return null;
     }

     public Item getItemAt(int index) {
            return items[index];
     }

     public int getInventorySize() {
             return MAX_INVENTORY_SIZE;
     }

     private int findFreeSlot() {
          for (int i=0;i<items.length;i++) {
               if (items[i] == null) {
                    return i;
               }
          }

          return -1;
     }
}


works fine (just typed, not compiled). The interesting bits come when you want to start stacking quantities of items on top of each other. On the other hand, if you're not expecting the normal behavior from your inventory when you display it then lists might work just fine for you. Best idea would be to wrap it up in an Inventory class and then decide when you have a reason to what needs to be done internally.

Kev

Offline Eli Delventhal

JGO Kernel


Medals: 42
Projects: 11
Exp: 10 years


Game Engineer


« Reply #15 - Posted 2009-03-31 08:31:48 »

Yeah, that's true, Kev. I actually prefer to have the empty spaces fill in. Basically the list is compacted to be as minimally sized as possible. Then you can order it however you want as a player. It sorta depends what you want. If you're going for something like Legend of Zelda where everything has a set slot, then position information is very important. Also if it's like World of Warcraft where you have bag slots, then position also matters. If you are doing something more like Final Fantasy, however, then a list probably makes more sense.

Either way there probably is no best way to do it, and cylab was right when he said speed isn't really important in an inventory. So function is what you should design for.

See my work:
OTC Software
Offline pjt33
« Reply #16 - Posted 2009-03-31 10:42:16 »

I concur with you, I've also previously seen JCRPG (and I've been told Runescape was created in Java by a 15 year old boy). The only problem is, of the 8709816-238162-93861-9236-1` million websites devoted to game programming, only ~200 are written in java, and only 3 are RPGs (previously exaggerated example).
You have been misinformed. RuneScape started out as a university project, so Andrew was probably 20 when he started. He may have written parts of his graphics and audio libraries when he was 15.

As far as tutorials, there are plenty of general tutorials covering things like the collections framework, 3D rendering, etc. Read and understand those. If you do find an RPG tutorial but you don't understand the basics then you'll end up with a lot of code you can't debug. If you understand the basics, including basic OO design, you won't have much need for an RPG tutorial (or, at least, not for a language-specific one).
Pages: [1]
  ignore  |  Print  
 
 
You cannot reply to this message, because it is very, very old.

 

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

The first screenshot will be displayed as a thumbnail.

Longarmx (45 views)
2014-10-17 03:59:02

Norakomi (35 views)
2014-10-16 15:22:06

Norakomi (27 views)
2014-10-16 15:20:20

lcass (31 views)
2014-10-15 16:18:58

TehJavaDev (61 views)
2014-10-14 00:39:48

TehJavaDev (61 views)
2014-10-14 00:35:47

TehJavaDev (51 views)
2014-10-14 00:32:37

BurntPizza (67 views)
2014-10-11 23:24:42

BurntPizza (39 views)
2014-10-11 23:10:45

BurntPizza (81 views)
2014-10-11 22:30:10
Understanding relations between setOrigin, setScale and setPosition in libGdx
by mbabuskov
2014-10-09 22:35:00

Definite guide to supporting multiple device resolutions on Android (2014)
by mbabuskov
2014-10-02 22:36:02

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
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!