How to populate inventory slots from a dictionary?

0 favourites
  • 11 posts
From the Asset Store
Inventory Aid
$4.90 USD
Create grid-based game inventories and items easily.
  • Hey everyone,

    I am working on an inventory system and I got stuck in a tiny part. My inventory system is a "render" only system -- basically it doesn't hold any information aside from what to display and even then that is temporary. I want to know how can I loop over my dictionary keys and over all current slots of type "ingredients" for example and then change the slot animations accordingly.

    Elaborating on this, here is what I have.

    I created a sprite called "InventorySlot". It has one instance variable called "category". This identifies what kind of slot is it; weapons, potions, armor, etc...

    InventorySlot has a few animations, these animations are the icons for each of the items in the game. On the other hand I have a Dictionary called "Inventory" it has a key for every item in the game with a value referring to the amount the player has.

    What I want to basically do is this:

    For every key in the dictionary, find all InventorySlots in the layout. Set the first InventorySlot to the first key and the second InventorySlot to the second key and so forth...

    I thought this would be easy and what I tried is setup an Event with "For Each Key in Dictionary" with a Sub Event "InventorySlot > category is equal to "Weapons" and then an action "Set InventorySlot animation to Inventory.CurrentKey.

    This didn't work even though the keys and animations have the same exact names. What I got is all slots are set to the last key since the loop picks ALL InventorySlot in each iteration. So I am not sure how to tell the loop to pick one by one and set their animation and once it picks one, it skips (breaks) this iteration and moves on to the next iteration.

    Any ideas?

  • Hard to deduct from the text actually, but I would try the following:

    Change the slot to add an instance variable to indicate it is used. for example "occupied". 0 or 1. Initialise them to 0

    within the Dictionary loop,

    pick by evaluate with expression slot.category = dictionary.currentKey (or whatever holds the category)

    In a subevent. Pick 0th instance of slot. So within the first picking, you've got the first one. So you're left with only 1 slot with the correct category.

    Do your stuff with animations

    Set Occupied to 1.

    If you're interested you can check out my tuts on my youtube channel where I do stuff like that:

    For example in Solitaire: First pick by evaluate, and then within that pick top.

    Many many tutorials have "pick by evaluate" by the way, and also dictionary loops.

    You can check them out if you want.

    youtube.com/watch

  • Hard to deduct from the text actually, but I would try the following:

    Change the slot to add an instance variable to indicate it is used. for example "occupied". 0 or 1. Initialise them to 0

    within the Dictionary loop,

    pick by evaluate with expression slot.category = dictionary.currentKey (or whatever holds the category)

    In a subevent. Pick 0th instance of slot. So within the first picking, you've got the first one. So you're left with only 1 slot with the correct category.

    Do your stuff with animations

    Set Occupied to 1.

    Thanks for your quick reply. I followed your instructions 1:1 and it didn't work at all. Here are my events:

    https://i.imgur.com/Ker8pqr.png

    The result is that they are all displayed as empty. I checked the dictionary, checked the animations, checked the categories they all match (strings and spelling wise). What am I missing?

  • maybe you can post the c3p file if that's not a problem

  • maybe you can post the c3p file if that's not a problem

    My project is a bit big with multiple event sheets and what not. So I extracted the logic in a separate project and tried again. Still not working properly as it changes the first slot ONLY and ignores all of the others.

    Here is a download link:

    https://www.dropbox.com/s/lnwmp66j052ir1j/Dictionary%20Not%20Working.c3p?dl=0

  • I added an "id" instance variable to the slots and filled the 4 slots with number 0 to 3

  • I added an "id" instance variable to the slots and filled the 4 slots with number 0 to 3

    I tried something like that but instead of picking the nth instance, I set the "n" in there to loopindex but that didn't work either.

    Thank you very much for fixing it for me. I am going to use that.

    If you don't mind a side question and this purely for curiosity and nothing else -- is there a way to set it up without using the IDs or any kind of identifier?

    Reason by hind the question is that I want to understand if one can create generic systems within C3 without having to resort to IDs and what not -- to reduce the margin for human error when creating systems. If that makes any sense.

  • Try Construct 3

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

    Try Now Construct 3 users don't see these ads
  • Good question, I haven't given that a lot of thought really...

    My first hunch would be to create the slots dynamically as well, making a grid of slots, and filling in the id's programatically, and looping as you do now. Something like that.

    Shouldn't be too hard to do, if you just set up a number of rows and columns, decide on the height and width of the slots, an offset between the slots, left margin, top margin, and you're good to go really.

    I'm a pretty big fan of placeholders, as you might have noticed on some of my video's on my youtube channel, and granted that has been prone to errors, but the need for generic systems is rarely the case. Certainly not in C3 as you don't (yet) have options to integrate predefined blocks of logic.

  • Good question, I haven't given that a lot of thought really...

    My first hunch would be to create the slots dynamically as well, making a grid of slots, and filling in the id's programatically, and looping as you do now. Something like that.

    Shouldn't be too hard to do, if you just set up a number of rows and columns, decide on the height and width of the slots, an offset between the slots, left margin, top margin, and you're good to go really.

    Would something like say having some placeholder sprite denote locations and then creating the slots work? So have a sprite that is invisible in each slot's location and just then spawn the slots, add their IDs and then populate the dictionary?

    I'm a pretty big fan of placeholders, as you might have noticed on some of my video's on my youtube channel, and granted that has been prone to errors, but the need for generic systems is rarely the case. Certainly not in C3 as you don't (yet) have options to integrate predefined blocks of logic.

    In my current project (or maybe this is how I am used to thinking about implementation) there are quite a few cases where generic systems would work well. For example I have two inventories; one for a shop and one for the player. Both are populated in the same exact method we've been talking about (from a predefined dictionary). So setting up a generic system here that I don't have to manually create for both ends would save a lot of hassle overall. That doesn't mean it can't be done otherwise of course (after all that is what I am doing now).

  • Create placeholders at design time, create the slots at runtime is indeed how I would set it up, and depending on player/shop change the configuration and the way it looks

  • Create placeholders at design time, create the slots at runtime is indeed how I would set it up, and depending on player/shop change the configuration and the way it looks

    Nice, thank you for confirming this :) makes things a lot easier this way!

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