Array inventory system has items overwriting themselves

0 favourites
  • 5 posts
From the Asset Store
Inventory Aid
$4.90 USD
Create grid-based game inventories and items easily.
  • I initialized the array and put together this system for a simple inventory. When I pick up any item, everything works as intended. I can use the item, the sprite gets destroyed, etc.

    The issue appears when I pick up a second item, as it overwrites the first. Does anyone know what might be happening? Here are some screenshots.

    The checkSlot loop was meant to bypass indexOf to see if that was the issue, but nothing changed. Running this with or without indexOf vs running it without the loop doesn't change anything.

    firstEmptySlot is my global variable.

    In the debug example above, when hitting E on either of these items, it's added to the correct first spot in inventoryArray.

    tempWatchValue (watching inventoryArray.IndexOf("")), also changes to 1 as it should. Picking up the other item doesn't change it anymore though, and just replaces the first item. Thank you for any help!

  • I might be losing my mind here haha. I added some logs to track what was happening in the console, and it does show the item getting overwritten:

    FInding Slot: 0

    runtime.js:14 Inventory {"c2array":true,"size":[10,1,1],"data":[[[""]],[[""]],[[""]],[[""]],[[""]],[[""]],[[""]],[[""]],[[""]],[[""]]]}

    runtime.js:14 Inventory {"c2array":true,"size":[10,1,1],"data":[[["key"]],[[""]],[[""]],[[""]],[[""]],[[""]],[[""]],[[""]],[[""]],[[""]]]}

    runtime.js:14 FInding Slot: 1

    runtime.js:14 Inventory {"c2array":true,"size":[10,1,1],"data":[[["key"]],[[""]],[[""]],[[""]],[[""]],[[""]],[[""]],[[""]],[[""]],[[""]]]}

    runtime.js:14 Inventory {"c2array":true,"size":[10,1,1],"data":[[["ball"]],[[""]],[[""]],[[""]],[[""]],[[""]],[[""]],[[""]],[[""]],[[""]]]}

    But it also shows that it's working properly outside of that? It does everything right and then the second item overwrites the first every time. Here is updated code that I put in an isolated event sheet, testing on an isolated layout.

  • It's kinda odd, I'm looking and your code and it looks like it should work? Maybe... is "firstEmptySlot" a number variable? Maybe it's set to string and that messes it up?

    Either way, I've set it up slightly different (with a function, highly recommend) and tested it. This works 100%.

  • Try Construct 3

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

    Try Now Construct 3 users don't see these ads
  • It's kinda odd, I'm looking and your code and it looks like it should work? Maybe... is "firstEmptySlot" a number variable? Maybe it's set to string and that messes it up?

    Either way, I've set it up slightly different (with a function, highly recommend) and tested it. This works 100%.

    Yeah it’s a number variable. Thank you I’ll try this!

  • It's kinda odd, I'm looking and your code and it looks like it should work? Maybe... is "firstEmptySlot" a number variable? Maybe it's set to string and that messes it up?

    Either way, I've set it up slightly different (with a function, highly recommend) and tested it. This works 100%.

    This does work, thank you! Now all I need to do is make each item trigger map to its associated item instead of being random.

    I really wonder what the issue was with my last code. It had me stumped the entire day, because it really looks like there aren't any issues with it.

    EDIT: I changed the function choose to the trigger I'm using and its associated item and it works flawlessly. HUGE thank you for your help with this!

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