This site requires JavaScript, please enable it in your browser!
Greenfoot back
Freekywill
Freekywill wrote ...

2016/8/13

Inventory

Freekywill Freekywill

2016/8/13

#
I am currently making a survival/exploration game, and I am having a small setback. Making an inventory! Can anybody give me some tips, or an example of an inventory that allows you to click to move items, and a hotbar to move items to.
lordhershey lordhershey

2016/8/13

#
Could imagine lots of different schemes. Is there a limited amount of space, does item weight count? What does moving an item to the hotbar do?
Freekywill Freekywill

2016/8/14

#
There would be a limited number of inventory slots, and each slot could hold a set number of that item, for example you could have 1000 wood in slot of inventory, and as soon as you hit 1000 it would start using the next slot to store wood. The weight of items would not matter. The hot bar could be something like 5 slots in the inventory that are always displayed at the bottom of the screen, allowing you to actively use whatever item was on the slot you had selected. For example if you moved a weapon to slot one of the hot bar, closed your inventory, and pressed one to select hot bar one, you would then be wielding the weapon. I hope this clears up any confusions. :)
lordhershey lordhershey

2016/8/14

#
Why have a hot bar when you can have a picture of your avatar with readied equipment? But a hot bar would be just fine.
Super_Hippo Super_Hippo

2016/8/14

#
Create an Actor called Inventory. Save a reference to this object. Each inventory slot will be an Actor, too. The slots can be created through the Inventory object and the Inventory also keeps a reference to all of the slots. Whenever you open the inventory, add the inventory object to the world which then automatically adds the slots to the world, too. The slots need two variables: a name of the object which it contains, so a String (for example "wood") and the number of objects, so an int. When the character collects an item, it checks if any slot already contains this kind of object. If it does and the number variable of this slot is less than 1000, it will add one to it. If not, it will find the next free slot in the inventory, change the name to the collected object and the number to one. The inventory doesn't need to be in the world for this to happen. So you can simply remove the Inventory (and with it all the slots) from the world when closing the inventory.
Freekywill Freekywill

2016/8/14

#
The hotbar could also be used for building, for example I will have craft able towers that are able to be placed, which will shoot enemies. And Hippo, thanks for the tips. I will now attempt to figure out half of what you just said :P, but im sure I'll figure it out eventually. I may be back in the future to ask more questions about it.
Freekywill Freekywill

2016/8/14

#
Here is a question, I don't know much about references, but I think I understand how to do what you're saying, but I dont know if it is the most efficient way.
1
2
3
4
5
private Actor slot1;
 
slot1 = new InventorySlot();
 
getWorld().addObject(slot1, 10, 10);
so I would add all the slots to the world when I open inventory. Would I have to manually add/delete all the slots with 20 lines of code? Or is there some way to write a for loop that can do that for me?
danpost danpost

2016/8/14

#
The code you gave above only creates one slot for the inventory. Also, with the 'slot1' field being declared as type Actor, you would have to typecast it down to an InventorySlot object before accessing any of its members (fields and methods). Below shows how to reference multiple InventorySlot objects and loading the objects.
1
2
3
private InventorySlot[] slots = new InventorySlot[ < max slot count > ];
 
for (int i=0; i<slots.length; i++) slots[i] = new InventorySlot();
From that, the object that has the InventorySlot objects can add them into the world when it is added into the world with:
1
2
3
4
protected void addedToWorld(World world)
{
    for (int i=0; i<slots.length; i++) world.addObject(slot[i], < wherever >);
}
and removed with it using:
1
2
World world = getWorld(); // or '= this;', if in world class
world.removeObject(world.getObjects(InventorySlot.class));
Freekywill Freekywill

2016/8/14

#
Thank you very much dan, I was atleast able to create the inventory, now for the hard part, actually making it work
lordhershey lordhershey

2016/8/23

#
There are a few inventory scenarios floating around that may have shared the source.
You need to login to post a reply.