What am I doing wrong? (Inventory System)

0 favourites
  • 15 posts
From the Asset Store
Be quick and choose whether the shown equation is right or wrong.
  • Hi y'all!

    I'm working on an inventory system and for some reason it's not updating the slots. I "pick" a item from other layout, set the key "PickedItem" in a Dictionary as the picked item, then I go back to the inventory and call a while expression to check the next empty slot (the inventory is in an array) and add this picked item there.

    But the problem is that it's not adding the item properly to the first empty slot. Actually it's not adding anywhere, it stays as the "PickedItem", the "PickedItem" slot never empties and the slots remains as 0.

    What is strange is that for some miliseconds, it works as it should: 0 for picked item, 2 in the slot 1 (uncommon medicine herb code), 0 in the slot 2 and slot 3 as well. But after this miliseconds, it gets back to the wrong structure showed bellow on the output image.

    So: what am I doing wrong!?

    Thank you so much for your time and for helping me!!! Really appreciate it :)

    Here is my code:

    Here is my inventory array:

    Here is my item list dictionary: (used to call the picked item code into the slot)

    Here is the Inventory Layout (dummy texts on the bottom to test it):

    Here is the output from the code:

  • What is the uncommon herb medicine code you mention, you didn't share this? This looks a bit convoluted for adding an item to an array, do you definitely need to use dictionaries?

    You could store an item as a global variable string and use that to add in the array. You can run a basic 'for' loop from 0 to array.width to find the next empty X and store that somewhere to make sure it's working and finding the correct row.

    Once that is confirmed you can then try adding the item to that row and confirm that is working. Also since you are using a function, you could find the dictionary item required outside of the function and pass it through the function as a parameter to also check it.

  • Hey Lionz!! Thank you for your response!! :)

    Hm, yes, I did share! It's in the same dictionary as the picked item (3rd image that I shared), and it's "code" is the number 2.

    So, I don't understand why the 'while' loop should not work properly as the 'for' loop, since both of them should find the empty slot.

    Also, ok, I could add the picked item to a global variable, but since it's a mobile app game, any layout suspension will need it to save this global variable. If I start adding global variable for everything, it will be a mess to save all of them, so that's why I use dictionaries and arrays. And I found many posts also recommending it, to use arrays and dictionaries instead of global variables, so I'm confused now! haha

    And sorry, the function thing, I didn't get it, is there any other way I can call the while without using functions? Or should I optimze it somehow?

    thank you again! :))

  • What I mean about the logic is that it only shows you searching for an empty slot and adding one item to the inventory on start of layout. It sounds like you are storing items to later add to an array, why not add to the array at the moment they are picked up?

    There would only be one global var to store an item as you pick it up, but once you have verified it is working you wouldn't even need that. With the function, if you are making an add item function, you can send the item name through the function as a parameter. Read up on functions if you don't know about this.

    What you've done here is combine looking for the empty row and adding the item into one function which is fine but I don't understand the use of dictionaries and list of items.

    I would advise using debugging with the Browser object, you can log things to the console to check how the function is working.

  • Hm, got it. I understand that there is simpler ways to do what I'm trying to do, but still, I don't understand why it's not working the way I structured it! I mean, what's wrong with my code, it should work, doesn't it?

  • I changed a little bit the code to this:

    What is really weird is that it doesn't work at first moment, but if I close the preview, and reopen it, then it works haha that doesn't make any sense to me... I'm obviously missing something here, but what!? I'm stucked into it like 3 weeks haha.

    (Immediately after picking up the item)

    (After closing the preview and reopening it, it works! Zero for the picked item and 2 for the slot 1, which is the uncommon medicine herb item code)

    What's going on here!?

  • What are you loading with ajax? Maybe it's not finished. Also you are getting the dictionary item within the function which may take time and it could be blank or 0. I suggested getting the dictionary item before you call the function then call it with the dict item as a parameter.

  • I don't know why, but the images didn't load properly.

    Here is the new code (I excluded the function):

  • Also, I adjusted the loading code, and the LocalStorageLoading_I variable always reach the 1 value, except when it's the first time running it.

  • It's no use talking about events that we can't see, you should provide all important events. So it's resolved now then, not everything is loading for you.

  • I provided all the important events! It's up here, the loading, the text update, the invntory adding item code! And no, everything is loading properly! As I said, the LocalStorageLoading always get the "1" value... :/

  • I mean initially in the first post you didn't provide the important load events. I can see now you're loading things from ajax on start of layout while at the same time running a function on start of layout before the data has loaded. But also I mentioned this method is unnecessary, you shouldn't be saving and loading to local storage between layouts to add items to an inventory.

  • Ok, but I said this is a mobile app, let's say that the player suspend the app and return to the it's homescreen. I need to save it, regardless the layout.. What do you suggest?

  • On mobile you can use system "save" it will save everything and then when they launch the app again use system "load".

    Check out this tutorial from Ashley : construct.net/en/tutorials/savegames-11

  • Try Construct 3

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

    Try Now Construct 3 users don't see these ads
  • Well, but I use real time based events and time spent offline to update the player progress, so I guess this "save" system for mobile apps would not work properly, right? Also, sorry but it doesn't make much sense that he needs to save the game every time he exits the game to the phone's home screen, like how many times we do that? switching into apps in our phone? I'll for sure forget almost 9 to 10 times to save the game haha.

    I'm still stucked on this and I still don't know what is wrong with my code :/

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