How do I make a SIMPLE inventory

Just started using Construct 2? Post your questions here

Post » Mon May 11, 2015 7:41 pm

Hi Guys

Well, i'm trying the different Inventory Array Tutorials that i find but the thing is most of them are much too complex and more in the vein of an RPG than an Adventure Game.

I'm trying to do this:

The player has an Inventory in the Bottom of the Screen
If he picks up an Object, it appears in the Inventory
It should be Keyboard controlled so he can cycle through it (reaching the last active Slot and pressing right again brings the selection back to the first)
Upon overlapping Door and using Key for Example, the Key should be destroyed from the Slot and the Door opened
Here's the tricky part: let's say you got three Inventory Items and the Key has the first Spot. If you use the Key, the slot goes back to zero again so the space is free. The other two Items still available then should move up.

I'm still kind of a noob (although i finished my first game recently in C2) so i'm kinda too stupid to simplify the existing tutorials. Could someone give me a helpful tip? Each object is only going to be available once, it's not like in Zelda or Diablo where you can collect 30 Arrows and 3 Apples etc. It's much more Adventure Game oriented.

Thanks for your continuing support! :)
B
18
S
7
G
2
Posts: 113
Reputation: 2,210


Post » Sun May 17, 2015 3:09 pm

Thanks a lot but again, these are only examples with much more complex Inventorys than i would ever need :(
B
18
S
7
G
2
Posts: 113
Reputation: 2,210

Post » Sun May 17, 2015 6:58 pm

joel87 it's not hard to understand. Just study the examples and use logic how you want to implement in your work
B
257
S
42
G
15
Posts: 1,991
Reputation: 41,411

Post » Mon May 25, 2015 11:17 am

@joel87

Hey Joel, here are some of the key things that helped me build an efficient inventory with an array.

Create only one sprite for the inventory items, this sprite should have a different animation for every item in the inventory with an appropriate name. Let's say that you have 8 visible slots in your inventory. At the start of each scene create 8 instances of the inventory item, even if you're inventory is bigger than 8 slots, it's only about the visible slots. Position each of these items in the appropriate place, I create an image point in the inventory bar animation and set each sprite to that position e.g: create object 'invitem' on layer GUI at 'InventoryBar.ImagePointX"item1" etc. etc. Then run a function that fills the items to correspond with the array. Here's mine:

Image

So this first sets a variable stored in my inventory array to 0, then for each instance of my inventory item sprite it sets the animation to whatever is in the array at the value of this variable. For example if the value saved into my array's X at 0 is "Hat" it will set the animation to "Hat". Just make sure you've create an animation called "Hat" and it will work. Then it adds 1 to the variable, so the second time it runs this loop it will set the animation to whatever is in the array's X at 1. If the value is 0 this means the slot is empty and it sets the animation to default, i.e. an empty picture.

Moving the inventory left and right isn't too difficult. You create a variable, lets call it invScroll, and you add 1 to this variable when the left key is pressed and subtract 1 when the right key is pressed. You also run a function after changing the value of invScroll, i.e. every time you press left or right. This function is almost identical to the one above for arranging the inventory, except instead of: set animation to Inventory.At(Inventory.item) it will be set animation to Inventory.At(Inventory.item+invScroll)

Hopefully that makes sense and is enough to get you started!
B
11
S
3
Posts: 135
Reputation: 1,309

Post » Mon May 25, 2015 11:30 am

For showing the items, you can make Item sprite, then add animations like id_0;id_1 then call them by the id of the item in the array like set animation "id_" &invarray.at(1,1) or something like this.
Image
B
23
S
5
G
5
Posts: 146
Reputation: 3,773

Post » Tue Jan 05, 2016 6:59 am

It would be handy if all those capx files were zipped up into a single file. i'm just sayin...:)
B
8
S
1
Posts: 27
Reputation: 515

Post » Wed Jun 21, 2017 6:34 am

i assume youve figured this out, but for just a few items you could use global variables, after all, an array just stores variables on an axis. a numerical global variable is a 2 axis array. so you can say, { Player - on collision - item - { Destroy item, add 1 to itemVariable}}
for this, i suggest you call your item variables '0','1','2','3', etc. you can create a note in your code to remind you what is what
to show what item is currently selected, create a numerical global variable called 'Selected". create a sprite with all item possibilities. then, you would want your code to read along the lines of {every tick - sprite.uid# - set animation frame - Selected} . to cycle back and forth, you would only have to say, {On left press - -1 from selected} {On right press - +1 to selected}
from here, you would want a safeguard in place, that would say, if selected < max, set selected to 0. this would create a loop, so once you got to the end you could continue cycling.
how do i know whether i want to cycle left or right?
you can make 2 copies of your item sprite, one on the left, one on the right.
{every tick - sprite.uid# (sprite on left) - set animation frame - selected -1}
and {every tick - sprite.uid#(on right) - set animation frame - selected +1}
again, you have that bit of safeguard, to keep from max+1 or 0-1.
Max +1 is an easy fix, as you just add a copy of your sprite in the 0 slot of your animation into your last frame, and set your max(return to 0) to 1 number less than your actual max frame count(or animation count).
the 0 is a bit trickier, however, still simple. {If Selected =0,Sprite.uid#(onleft) -Set animation frame - Max

this shouldnt be too hard to implement, and should only take up 2 or 3 events.
i almost forgot,
as far as knowing how many of each item you have
If Selected = x, set text.uid to x. what this would do, is if Item displayed in middle box is item "1" , the text becomes the value of variable "1" which would be the amount of that item you have, while setting the animation frame in the middle box to match (assuming your frames match your item number variables)

because you named your items '0','1','2','3, etc, you can use the same variable to call on all instances of the item, from your inventory count, to the frame displayed in your selection sprites. if you were to name your items "hammer" etc in your variables, you would have to code out each one instead of using math, such as selected+1. if you dont like having raw numbers as variable names, id suggest item0 item1 item2, and then call on them as "item"&selected


its 230 am, so im not my sharpest, i may have overlooked a thing or two, but that should assemble most of a working basic inventory.
B
25
S
14
G
1
Posts: 138
Reputation: 3,111


Return to Beginner's Questions

Who is online

Users browsing this forum: No registered users and 0 guests