How to use arrays in inventory?

0 favourites
From the Asset Store
Template for maintaining an inventory with crafting possibilities. Completely documented in text and video.
  • Hi guys, first, I did search about arrays used in inventory systems, but I tried and I didn't find a way to do what I need. So I apologize in advance if this question has been asked often.

    This is my .c3p file:

    So basically, I need to add an item to an inventory slot, and then add it into an array as well.

    Which for now seems to work. Now the problem arises when I *use* an item (for now I tried destroying the item). So you end up with an empty slot anywhere in the inventory, which subsequently should be filled by the next item you find.

    How can this be accomplished?

    Thank you.

  • You should be making use of slot_ID to relate to the array X. This makes things easier. When you select the item you look at the slot_ID and delete the item from the array X that is the same.

  • Thanks for the reply.

    I compared both values:

    array_inv.CurX & invItem.slot_ID

    But I can only destroy the first item. And subsequently, I cannot get a new item in the empty slot.

  • not curx. if you are going to use slot IDs then you need to relate the slot ID to array X. so slot ID 1 is 1 in the array also. Then when you select the slot object which is ID 1, you take the slot_Id and then compare array x where array.x = slot.ID, then delete the contents. You should also get rid of item ID that's not needed. Think of the inventory more as the array and not the visual side.

  • Try Construct 3

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

    Try Now Construct 3 users don't see these ads
  • Thanks, that helped, but I'm still facing the problem in which once I delete an item, the next added item wont go to the empty slot, but rather continue.

    I know why this is caused, but I cant figure out how to do it otherwise.

    Should I do a For each in the array to check for empty slots?

  • Yep you answered your own question, you'll need to create some new logic that checks for empty slots, you can't really just push to the back if there are empty ones in between. You would just use a for loop and loopindex compared with the array X until you find one which is empty "". You can either do this or have an inventory where deleting the item moves everything down, you would do this by deleting the actual row rather than its contents.

  • After fiddling more with it, I've been able to understand a bit more. But I still fail to understand how I could jump from one empty slot to another.

    *Edit: Didn't see your post, sorry about it. I'll take a look at that.

  • I'm missing something...

    Basically, if I understood, you do a:

    for each X element -> Current Value = Loopindex( array.CurX )

    But on a debug text, CurX is always the latest X value from my array.

    How can I stop the loop whenever it finds the first empty value inside an array?


  • There's a system way if you understand it :

    for "" from 0 to array.width,

    - if = "", then stop loop, set variable to loopindex (stores the x)

  • You mean

    System for "" Start Index: 0 End Index: array.width, loopindex ) ?

    But the expression in End Index is not valid.

  • I edited above so you can see the structure

  • Thank you

    So basically, whenever we delete an element we start a loop and stop it and save this value into a variable. Which afterwards I use to return to that point.


    But the variable is always showing zero as value. So is not being triggered for some reason.

    Sorry, I'm completely lost lol

  • You don't put = "i", the "" is to represent the empty array cell you are searching for. Whatever the data is for an empty cell if its "" or 0, you use this. Then on setting the variable it's the loopindex only. You can then use this to add an item to that array X.

  • I set them to "", but I always end up with zero stored in the var.

    I also realised I'm having an issue which is visible in the debug window: something it's constantly loopinmg.

    I assume it is actually how I set the function to AddToInventory().

    Since it's a Array -> For, the loop doesn't seem to stop. So I'm thinking this might not be the best solution when adding a new element to the inventory.

  • Ah ok it's because you're using an x,y array so you need to say at (loopindex,1) I guess to check there is no item. When you use (loopindex) that first cell is always 0, im not sure what the numbers are for tbh.

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