How do I Set up inventory using Array

Not favoritedFavorited Favorited 0 favourites
  • 13 posts
From the Asset Store
Inventory Aid
$4.90 USD
Create grid-based game inventories and items easily.
  • Hi everyone, I am completely new to construct and I'm trying to create a main menu/key item system for my game. Its been working fine so far. I'm trying to make it so when a new key item is added it goes to the next avaliable empty slot. Originally I had lots of bugs like the text not displaying at the bottom instead showing 0 and the image being off centre and the image scrolling with the cursor. I managed to fix all these problems but I'm still left with the problem that items are stacking and not moving to the next empty slot.

    I'm pretty sure the issue is either with how the key items are added initiallyand being stacked or overwriten on the first slot or that the image being created/destroyed (so only one image exists per key item) is interfering and stopping other slots being filled or destroying the next image/item.

    At this point I'm starting to lose the plot and nothing makes sense anymore lol. I've spent hours upon hours on this and all the loopindex.at and numbers has completely confused me lol. I tried asking for help but no-one was sure and the suggestions they made may have made my code worse? Or interfere? I then asked chatgpt if it could spot the problem and it gave a few suggestions but again did not fix the problem and may have led to furthur conflicts??? And now I don't really know what to do.

    It's still 100% better than when the text wasn't displaying and all the other bugs but this one bug I cannot figure out.

    Also I just did my own thing and did not follow any guides or tutorials and have 0 knowledge and expertise lol. I'm pretty sure it can be fixed or reworked without having to start all over again.

    Can anyone tell me what's wrong and either give me some super simple steps or pointers to fix it or suggest another option?

    I've dedicated about 2 days of my spare time into this so far and managed to fix so many bugs and such I really don't want to have to give up or start again now. I have a few back ups I can revert to if needed. But ones before the text was fixed and one is after but with this same issue. But before adding the suggestions and chatgpt suggestions.

    Thanks.

  • Just create a system loop for 0 to array.width, then a sub condition system compare 2 values if array.at(loopindex,0) = 0 (is empty), then stop loop and add the item there with actions.

  • Thanks for your response. Thanks for your advice. Is that not what I am doing on line 34? (Not sure if you can see my screenshots) I thought line 34 was structured to loop through the inventory and find an empty slot. Again, I'm a bit clueless when it comes to construct.

  • Here you can see my test key items are stacking on top of my keyitemicon sprite (which updates the image for the collected key item, which works because it has the 2 key item images on top of it, so it is updating the new image for the new collected item just not in the right place) it should be picking the next empty slot.

    If I do not collect any items it shows no sprite and no items. (Proving that its only when i collect items they are being updated and added into the slot)

    Everything to me seems like it should be working?? I just don't get it. I really don't want to start all over again because I built this to my needs. I just don't understand why it cannot find the next empty slot and place the image and text there.

    Everything seems to be working correctly apart from it going to the next empty slot. The image is updating the text is updating.

    The way I can see this is by removing the destroy action under line 36 of the create keyitemicon. By removing this I can see the items stacking. If I have it activated it only shows the latest item which again is right and what I want but because the items are stacking and not moving along its not working.

    Again I'm confused and unsure if there's an issue when adding the new item and it's not being recorded properly into a new empty slot (line34). Or if the issue is when I'm creating the keyitemicon for each new keyitem when collected and its somehow destroying or recreating the image in the same location preventing it going to the next empty slot. (Line36)

    It's probably something so simple but I've been tinkering for ages now and nothing lol. Also took advice from others (not from construct) and not sure what effect that has had. My code has slightly updated now but anytime any issues arrived i backtracked. I do have some updated code but it didn't seem to make a difference good or bad. Still have my back ups to revert back to if needed.

    Absolutely clueless. Lol.

  • The reason I believe it could be something to do with line 34 where I search for an empty space is because items do display text and stack on top of eachother. So all the code for updating items and text seems to be working? Just not the cycling the next item to the next empty slot. Because I can see its not moving along.

    However I assume (not 100%) that if line 36 is constantly recreating icons in the same location or destroying the last one it could be recycling to that spot thinking its the next empty one because its being destroyed. Making it empty.

    Or I'm just overthrowing this at this point and should just delete the 6 lines of code and start over with a simply system or a suggested replacement??

    Basically my system is designed so I can call an imageicon that holds all my keyitem sprites. When I get round to working out where and placing a new key item in my game I call the function, fill in the info making sure the sprite number is the next in line, go to the sprite and change the corrosponding frame default to the new key item I just placed. This makes it so it automatically should update the image and text and push it along the grid. It does all that apart from finding the next empty space lol.

    I need it to be like this because I'm not 100% on where everything is going until I get furthur into development. I'm just setting up the menus and logic right now.

  • 'for each array' is not looping through the array, this is to pick each instance of invarray of which you have 1 so it is not useful here. You are probably looking for 'for each element' which is a loop on the array object, but the system one I mentioned can also work well. You were never looping the array that's why all your objects are set to 0 position.

    "Or I'm just overthrowing this at this point and should just delete the 6 lines of code and start over with a simply system or a suggested replacement??"

    - Yes, keep it simple like I described above. You were never looping the array so first do this and see where your game is at, then you can come with questions from there when you are able to pick up an item and add it to the next empty slot.

  • Tried that still the same thing. Think I'm just going to cancel the subscription and give up.

  • Lets see the new events?

  • Hi. Thanks again for taking an interest and offering advice.

    I tried to incorporate your suggestion into my set up (setting array.width) (I also set up height, but tried just width as well.) So I did not have to delete it all and syaty again.

    I've moved things around a bit here and there as well at suggestion. I don't know if my set up is just wrong, to confusing or interfering somewhere.

    At this point im thinking of reverting to an earlier point before I made so many changes and trying to incorporate your suggestion there. I believe I was still using for each inventoryarray at that point.

    My current set up still uses inventoryarray at call keyitemicon but I did change that to width/height but no change so I changed it back for now because i wasn't sure if that should be on that line or not.

    If that don't work I will just have to delete the 6 lines of code and start again from the on call function (where the item gets added) and try to structure it better from the start. Maybe following a tutorial or guide, But I'm not sure if I can incorporate my text descriptions and such updating each item as well in some of the simpler set ups they don't have text descriptions and such negating the need where as I need mine to update the text description item name and sprite image each time a new object is added and moved along to the next empty slot.

  • What I need it to do is upon adding a new key item, 3 parameters ItemName ItemDesciptoon ItemSprite and then have the correct size inventoryarray and set it so it looks for an empty slot, saves the itemname item description and itemfram to the empty slot then it should recycle that same loop upon call key function each time a new key item is collected. Back to looking for an empty slot when it finds one save the name description and sprite and then set up the recycle.

    For all 3 name, description, sprite.

    It's probably really simple? And I've wasted 3 days lol.

  • Try Construct 3

    Develop games in your browser. Powerful, performant & highly capable.

    Try Now Construct 3 users don't see these ads
  • Well again it needs to be much simpler than what you have, you don't need so many loops and you don't need variables to track next available x and y. Also be careful using loopindex("x") as the x is for the name of the loop which you don't have.

    You need a simple system 'for' loop, for from 0 to array.width. This runs down all the rows. Then with this the condition array.at(loopindex,0) = 0. Then on the right side actions, you have Stop loop, and set the other columns to what you want so for example set array.at(loopindex,1) to an item name, array.at(loopindex,2) to an item description.

    So yeah that's all you need to set up what you want I dunno how I can make it clearer than this :D

    Also you never posted what the array looks like but make sure the empty slots are actually 0 and not just lacking any value so it would be "" instead of 0 if that's the case.

  • Here is a screenshot of what I mean

Jump to:
Active Users
There are 1 visitors browsing this topic (0 users and 1 guests)