Adventure Game Inventory array...

Get help using Construct 2

Post » Wed Dec 21, 2016 5:33 pm

I'm following this idea:
AlexFrancois wrote:@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!

Now, this is okay, but this assumes you'll have every item appear in a fixed place *I THINK*. I'm not sure entirely, and I'm not sure if this is the best practice for building an array for an adventure game inventory system, but this example does work, I'm just not *QUITE* sure how to, say, push the array to the right to add the new item to the start, similarly, a cell would need to be removed if an object was used. Can anyone just clarify these couple of points and if this is the best way of building the inventory?

EDIT: Yes, I've just made sure I INSERT the item in the array:
Image
Now, I'll need to figure out how to grab the correct cell for destroying it from the array when it's used/given awayetc.
B
41
S
12
G
14
Posts: 1,117
Reputation: 11,253

Post » Wed Dec 21, 2016 5:57 pm

Hmm, tried this:
Image

Doesn't work though?
B
41
S
12
G
14
Posts: 1,117
Reputation: 11,253

Post » Wed Dec 21, 2016 6:32 pm

try:
Image
Image
B
65
S
23
G
7
Posts: 1,504
Reputation: 10,894

Post » Wed Dec 21, 2016 6:49 pm

korbaach wrote:try:
Image

Hmm, afraid that doesn't work either, I'm re-running the loop afterwards, but the item is still present in the invetory after clicking.
B
41
S
12
G
14
Posts: 1,117
Reputation: 11,253

Post » Wed Dec 21, 2016 7:23 pm

I got it working, but seems to leave duplicates of other items in the array? Does delete remove the entire cell, and not just the value?
B
41
S
12
G
14
Posts: 1,117
Reputation: 11,253

Post » Wed Dec 21, 2016 7:34 pm

if you want to delete all "Badge" cells from inventory use

Image

indexOf() delete only first "Badge" cell
..I assumed that there is only one "Badge" cell on unknown index
Image
B
65
S
23
G
7
Posts: 1,504
Reputation: 10,894

Post » Wed Dec 21, 2016 7:48 pm

korbaach wrote:if you want to delete all "Badge" cells from inventory use

Image

indexOf() delete only first "Badge" cell
..I assumed that there is only one "Badge" cell on unknown index

Yes, there should only be one of each, but I'll keep this in mind if there's duplicates, thank you!
B
41
S
12
G
14
Posts: 1,117
Reputation: 11,253

Post » Thu Dec 22, 2016 10:56 am

Having a bit of trouble determining if I should have a second page with the scroll or not, not quite sure how to effectively test if theres more items than can fit on each page.
B
41
S
12
G
14
Posts: 1,117
Reputation: 11,253

Post » Thu Dec 22, 2016 9:43 pm

Use a system > 'for', .... from array.width - 1 .... to ...... 0.
Now you walk trough the array from the end to the start.

Sub event .... Compare 2 values ... Array.at(loopindex) ... = .... "badge"
Action ... Array > delte ... x-axes ... index = loopindex
B
33
S
18
G
28
Posts: 2,493
Reputation: 20,950

Post » Mon Dec 26, 2016 8:36 pm

B
41
S
12
G
14
Posts: 1,117
Reputation: 11,253


Return to How do I....?

Who is online

Users browsing this forum: ajcoppersmith, blue visionary and 5 guests