Changing the size of an array while in a loop

0 favourites
  • 3 posts
From the Asset Store
For Adventure, Action, Heroes, Sci-fi and Action-RPG games.
  • I know I probably have to rethink my approach, but here's my problem:

    I have a 1D array, let's call it Inventory, with 10 cells, each containing a different value.

    I'm traversing this array, and I check each value to another 1D array, which contains all the unique values.

    The Unique Item array starts off empty and with a width of 1. Each time I find a value in Inventory that doesn't exist in Unique Items, I add this item to Unique Items.

    However if I try to change the width of Unique Items (either by set size, or by push back), so it can fit a second item (and then a third) the program crashes.

    This is the barest minimum of code, and it freezes completely.

    It's probably causing an infinite loop, because "for each element" starts iterating once (since the width is 1) and then I try to mess with the width.

    Any ideas? Thank you!

  • When you access value from indexes out of range then it always returns 0. Pushing 0 probably doesn't help with this.

    Anyways, Here's another idea to approach the idea. Check all values in uniqueArray before adding the new value.

    +---------------------------+
    |array: for each x |
    +---------------------------+
     local number match=0
     +-----------------------------------+
     |uniqueArray: for each x |set match to 1
     |array.curValue=uniqueArray.curValue|
     +-----------------------------------+
     +---------------------------+
     |match=0 |uniqueArray: push array.curValue to back
     +---------------------------+
  • Try Construct 3

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

    Try Now Construct 3 users don't see these ads
  • Thank you so much R0J0hound, this was exactly it. Set match=0 at the start of each loop, and then

    1. if match found, set match =1 and do any actions

    2. if match wasn't found, match remains 0, so do any actions related to no match found.

    <3

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