How do I put a value in a unknow index of an array?

0 favourites
From the Asset Store
Supports 1D, 2D, 3D arrays. Import and export arrays in JSON format
  • I've already read all the tutorial on the array and inventory sistem but i can't figured out this: when the player grab an object, this should be put inside in the invetory firts slot avaible but this happen;

    The object fills all the slots in invetory. This is the code i use:

    The logic i still looking for is: grab the item, is the first slot avaible? yes put in that, else check for the next one.

    Please help!

  • I don't understand. Do you stack items? Then you are missing the amount of each item in the slot. Do you not stack items? Then why not just add the new item at the end of the array, because obviously all previous slots will be filled.

  • The for each command is doing that. That is why it is filling it up your commanding it to put it in each slot. You should check for the first empty slot and place it there.

    So 'for each' then to a check for if the current slot is empty if so break out the loop and place it there.

  • Try Construct 3

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

    Try Now Construct 3 users don't see these ads
  • see.... if this helps you

    Example.capx

  • see.... if this helps you

    Example.capx

    Thank you for the example, it's self-explanatory. It's still not helping me becouse, in your example the intial array width is 0 and you use push back to create a "slot" and the expression"Set value at (ArrayPicked.Width-1, 1) to 1" to put the value, whic is great but, in my case, i already know how many slot i have in my invetory. Any way, it's a great idea.

    The for each command is doing that. That is why it is filling it up your commanding it to put it in each slot. You should check for the first empty slot and place it there.

    So 'for each' then to a check for if the current slot is empty if so break out the loop and place it there.

    How can i break the loop?

  • .. in your example the intial array width is 0 and you use push back to create a "slot" and the expression"Set value at (ArrayPicked.Width-1, 1) to 1" to put the value, whic is great but, in my case, i already know how many slot i have in my invetory...

    I use " o width" and "push" because I dont know how meny slots you have in inventory...

  • Try this:

  • They System have a Stop Loop action. But it appears to not being doing what it says and stopping the loop. I wonder if its a beta thing.

  • > .. in your example the intial array width is 0 and you use push back to create a "slot" and the expression"Set value at (ArrayPicked.Width-1, 1) to 1" to put the value, whic is great but, in my case, i already know how many slot i have in my invetory...

    >

    I use " o width" and "push" because I dont know how meny slots you have in inventory... <img src="{SMILIES_PATH}/icon_e_biggrin.gif" alt=":D" title="Very Happy">

    You're right indeed but, the inventory could have any width, e.g.: a bag contain 10 spaces but i wanna make it bigger, than i buy a new one with 20 spaces. Your solution is pretty cool (i will quote you if my game will see the light!).

    Try this:

    http://www.blackhornettechnologies.com/ ... pdate.capx

    I've try this kind of code early but i think that the "stop loop" doesn't do anything, just as [quote:g5qfubfc]PhoenixNightly

    say.

    Thank you guys for the answers. If i discover something, i will share in the tutorial section <img src="{SMILIES_PATH}/icon_cool.gif" alt="8-)" title="Cool">

  • You can try this

    You a variable to make when it set within the loop so it won't keep setting.

  • "Stop" works perfectly with the System For events. It doesn't work with the Array For events.

  • Thanks for that note blackhornet

  • .. the inventory could have any width, e.g.: a bag contain 10 spaces but i wanna make it bigger, than i buy a new one with 20 spaces..

    I think ....you know how to solve this..but just in case...

    ..Add Variable "Bag(max_slots)"=10

    ................and then compare.....if Variable<Array.Width....then.....Push item in the bag

    ..and when you "buy a new bigger bag"...set Variable to 20...

    EDIT:

    Sorry guys...my mistake (lapsus clavis )

    obviously ..should be:

    ................and then compare.....if Variable>Array.Width....then.....Push item in the bag

  • You can try this

    You a variable to make when it set within the loop so it won't keep setting.

    This is a loop breaker! Thank you for the tip! It's actually working but the "insert", as name suggest, still add more rooms in the array. It's possible to delete the last index but is not was i looking for. I think i gonna keep working on your break loop solution! Thanks!

    > .. the inventory could have any width, e.g.: a bag contain 10 spaces but i wanna make it bigger, than i buy a new one with 20 spaces..

    >

    I think ....you know how to solve this..but just in case...

    ..Add Variable "Bag(max_slots)"=10

    ................and then compare.....if Variable<Array.Width....then.....Push item in the bag

    ..and when you "buy a new bigger bag"...set Variable to 20...

    I'm afraid you're wrong becouse, i don't really know how to solve this. Anyway, thanks for the tip, i'll give a shoot!

  • Hmm for me it didn't. If you don't mind could I see your eventsheet snapshot for that section? Because it is suppose to only push the value at the current location

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