[SOLVED] ever decreasing array

0 favourites
  • 9 posts
From the Asset Store
Supports 1D, 2D, 3D arrays. Import and export arrays in JSON format
  • Ok, so i have been going through this every which way, so if i there is a source of info i should be looking at, please direct.

    Basically, I think that array delete removes the index but then effectively just pops the value off the back.

    I am using an array of numbers to generate a random number that will not be repeated.

    I populate my array (numbersShown) with 1 to 10 at x positions 0 to 9.

    Each time i want a number i call numbersShown.At(floor(random(numbersShown.Width)))

    This works well.

    I am displaying that number of sprites.

    The user clicks on a number button that has an instance variable for a number (1 to 10).

    numbersShown - Delete index int(Button.clickedValue -1) from X axis.

    The length of the array is getting shorter, but I see repeat values when i call numbersShown.At(floor(random(numbersShown.Width))) as though the last value on the array is dissappearing, not the value at the index.

    Alternatively, the shorter array is not picked up until the next tick but that is not something i am sure about.

    Any suggestions?


  • [SOLVED]

    So the short answer to this is use Dictionary, not an array. Then cast your int to a str and delete the key and value from the dictionary.

    Using an Array means that the index will be deleted but the shuffling of the rest of the contents to new index positions detaches them from being found by any link to their index, this increases the more indexes you delete.

    eg. array index 9 has a value of 10 at the start of the game, but when a lower key is deleted the index of 10 shifts to 8 and then 7 etc as more indexes are removed. So when you have 10 buttons, each to delete that value from the array, you cannot rely on an instance variable on the button (eg 10) being used to find index of 10 in the array, except for the first delete.

    To then still find a random item, you need to copy the dictionary to an array after each deletion, then use the random(array.width) approach to select a random one (thanks to this for that end part->)

    It seems obvious now and was only made possible by using the debugger, for some reason i didn't think this was available in the free version - it is.

    I hope this helps someone and shows that I really do trawl through the forums, manual and examples before posting seemingly obvious questions on here.


  • Try Construct 3

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

    Try Now Construct 3 users don't see these ads
  • Array delete works perfectly well.

    Here's how.

    myArray = "1,2,3,4,5,6,7,8,9,10"
    myArray.delete(3) would give you
    myArray = "1,2,3,5,6,7,8,9,10"
    If you, then, did a myArray.delete(6), it would give you
    myArray = "1,2,3,5,6,7,9,10"
    It doesn't "just pops the value off the back". It removes the value at the specific index like in any other programming language. 
    The action array.pop is the one that pops a value off the back (or the front) of an array. Which is, basicaly, the inverse of what array.push does, adding a  value at the back or front of an array.
    The problem with your first code, I'm guessing, since you didn't share a capx, is that you are picking a number and deleting another. You must delete the index you picked, or else you will see repetition...
  • And here's a capx...

    https://www.dropbox.com/s/m4lscjllmh33h ... .capx?dl=1


  • in this case, I like a 2D array.



    so when you populate it would look like this: [[12131,1].[09843,2]...] and then just sort it. Boom, you now have it random. Simply pop off the first value and you are good to go. Less complicated than the route you are trying. Store it to some variable for continuous use until you are done with it.

  • , how does that avoid repetition?

  • , how does that avoid repetition?

    Because the number is only ever used once.

  • So, it's basicaly the same thing we said before. You use an index and delete it, right?

  • brunopalermo I need to keep the keys / indexes tied to the value since I want to create eg. 7 sprites and then press the button with an instance var of 7

    Yes array delete does work, I saw the array getting smaller.

    The reason why my results were unexpected was that the remaining values would move up the index and no longer be anywhere close to the instance var. What I was looking for was delete by value but I didn't find something to do this until i had moved to a dictionary approach.

    You are right, randomising the array and using pop would have worked quite well I think, but I ended up making it work through copying the dictionary to an array and random selection from there. Ultimately, I needed to get at the keys or indexes in a predictable way so it works for me.

    I have now completed this first (very simple) game in just 3 days since even hearing of construct 2 so although it is difficult to adapt to the approach I must take, I have made good progress...a reflection of construct 2's ease.

    For visual tools like construct, game salad etc it probably helps to never have done a code approach like unity or unreal.

    It feels like you are a little annoyed at my ignorant questions so I thank you for taking the time to guide me !

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