I've been making a top-down 2D rpg game with java and I've made some good progress, but I'm not sure how I should make an inventory system? I've tried using things from my own knowledge and I can't seem to find any tutorials. I have it so that you always see the inventory on the right of the window. Can somebody please help?
I just can't seem to figure out how to make an inventory. Something similar to minecraft's is fine, but instead if pressing a button for the inventory window to pop up. To the right of the actual game is where a whole bunch of tiles are, and that's where you place your items.
Well at the moment I don't have any code for the inventory yet and I haven't gotten far enough to acquire items. I was hoping that once I got the inventory figured out I could just go from there. Lemme go make an item
Eh, I've never created a java game... Would like to look at the src. xD
But anyway, you need a way to detect whether the 2 sprites have touched. Try getting the size of the images and their locations on the screen. If they are touching, remove from the screen, and have it print out like, 1 for example.
Hmm, what would you use to check for collisions? Because what I normally use needs an array and I think that's kind of redundant for a stick. Or should I just make the array?
Use an array
You could create a getWidth and getHeight (or getItemWidth and getItemHeight) to get the individual items with and height, then create a checker thread that checks whether your character's pixels have intersected or not.
I've been making a top-down 2D rpg game with java and I've made some good progress, but I'm not sure how I should make an inventory system? I've tried using things from my own knowledge and I can't seem to find any tutorials. I have it so that you always see the inventory on the right of the window. Can somebody please help?
It's rather vague what you're asking.. what part don't you understand?
Well most of the inventory system really. At the moment I really have no idea how to pick the item up, and how to place the item into inventory slots.
Make an item class, make a player class, make the player class contain an array of inventory slots or something. Create methods that add/remove/sort those slots, things like that. Make the visual inventory read each item existing in the array and display it accordingly. It really depends on a lot of factors about how the gameplay works and how your inventory is.
Make an item class, make a player class, make the player class contain an array of inventory slots or something. Create methods that add/remove/sort those slots, things like that. Make the visual inventory read each item existing in the array and display it accordingly. It really depends on a lot of factors about how the gameplay works and how your inventory is.
Thanks for that overview!
Do you think that you could give me an example please? I know enough java to get by, but for something like this I'm not really sure how I would go by it.
Here's an example for the player class, allowing you to remove and add items to the inventory, as well as allowing you to get the item belonging to a certain position (index) in the inventory. It's kind of hastily written, lacks proper error management and it's probably kind of shabby in general, but it should illustrate how the inventory works.
public class Player {
private Item[] inventory = new Item[game.getInventorySize()];
public void pickup(Item item){
//Find empty spot in array, then place the item in it.
for(int i = 0; i < inventory.length; i++){
if(inventory[i] != null) next;
inventory[i] = item;
return;
}
}
//True if item exists in inventory, false if it doesn't.
public boolean remove(Item item){
//Find the to-be-removed item in the array, then remove it.
for(int i = 0; i < inventory.length; i++){
if(item.equals(inventory[i]){
inventory[i] = null;
return true;
}
}
return false;
}
//Return the item at the index position. Returns null if no item exists at that position, and also returns null if you attempt to read an item outside the inventory.
public Item[] getItemAtIndex(int index){
if(index >= inventory.length || index <0) return null;
return inventory[index];
}
}
Now you would, in addition, have to write an Item class, or perhaps an Item interface, with different items inheriting from this interface or class.
I've been making a top-down 2D rpg game with java and I've made some good progress, but I'm not sure how I should make an inventory system? I've tried using things from my own knowledge and I can't seem to find any tutorials. I have it so that you always see the inventory on the right of the window. Can somebody please help?
It'd help if it was on github.
You get lots of help there.
What I meant was this:
I just can't seem to figure out how to make an inventory. Something similar to minecraft's is fine, but instead if pressing a button for the inventory window to pop up. To the right of the actual game is where a whole bunch of tiles are, and that's where you place your items.
Does that help?
What code do you have right now?
How do you currently acquire items?
(Pardon my noobiness, I'm relatively new to all this.)
But anyway, you need a way to detect whether the 2 sprites have touched. Try getting the size of the images and their locations on the screen. If they are touching, remove from the screen, and have it print out like, 1 for example.
I'll pm you the src.
Thanks, try that, and the statements above.
Use an array
You could create a getWidth and getHeight (or getItemWidth and getItemHeight) to get the individual items with and height, then create a checker thread that checks whether your character's pixels have intersected or not.
Player.java
I tried System.out.println but it didn't work either, so I just replaced it with JOptionPane
GameFrame.java
ItemStick.java
That's just the code that applies, there's more stuff in between.
Did the print ever print?
It's rather vague what you're asking.. what part don't you understand?
Well most of the inventory system really. At the moment I really have no idea how to pick the item up, and how to place the item into inventory slots.
Make an item class, make a player class, make the player class contain an array of inventory slots or something. Create methods that add/remove/sort those slots, things like that. Make the visual inventory read each item existing in the array and display it accordingly. It really depends on a lot of factors about how the gameplay works and how your inventory is.
Thanks for that overview!
Do you think that you could give me an example please? I know enough java to get by, but for something like this I'm not really sure how I would go by it.
Now you would, in addition, have to write an Item class, or perhaps an Item interface, with different items inheriting from this interface or class.
I'm not exactly sure what to put in the Item class though. Could I have another example please? =)
Also, is the getInventorySize method the size of one slot or the whole thing?