Picking X Non-Repeating Values Out of An Array [SOLVED]

0 favourites
  • 5 posts
From the Asset Store
Supports 1D, 2D, 3D arrays. Import and export arrays in JSON format
  • This is a corollary to my post about filling a 1D array with an incremented variable.

    I then wanted to pick 4 unique non-repeating values out of that array, which wasn't difficult to code by using another array, but the non-repeating part has proven a bit of a challenge. Basically I have a roller with Choose, but to make sure the values don't repeat I delete the rolled value out of the original array.

    The problem then is that rarely, depending on the odds, I get a 0 value out of the 4 chosen when it rolls into one of the deleted cells.

    There is an easy solution, don't spawn want I want to spawn if it's the 0 value, but this screws up my odds a little.

    I tried coding this a dozen different ways using the system repeat and various sub-event structures/flags with no success.

    Basically nothing works, but there should be a clean solution to this.


    I tried as a flag, for example, compare current value in the original array, if it does not equal 0, do your thing, but if it equals 0, reroll to hopefully hit another cell.

    I'm not sure this works properly within the Construct logic or if there's another solution for this.

    Is there something like Roll A Random Number As Many Times As Needed Until A Specified Condition Is Met?

    And how would one integrate that into a typical finite system repeat block (not once per tick)?

    P.S. I thought I solved this until I discovered the rare 0 values on rolls and realized what was happening.


    P.S. Here's the capx.


    Also, is there a cleaner way of stopping the infinite loop of system repeat w/o having to increment the flag? I tried doing it with a flip between 0/1, but it didn't work.

    It's doable doing it in this weird sequence, but gets a little annoying having to increment the flag on every copy of the block.

    Just wondering if somebody figured out a cleaner solution.

  • Try Construct 3

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

    Try Now Construct 3 users don't see these ads
  • Change: round(random(0,HBlockLineRepeatCount-1))

    to: round(random(0,HBlockLinePositions.Width-1))

    Since the array keeps getting smaller, you need to account for the changing size, which your global does not.

    C2 continually runs your script. You need to alter your events to only run when required. You can use functions to do this. Fill would then get called on startup, and Random whenever you needed it.

  • Wow, blackhornet.

    Thank you.

    I owe ya.

    Such a simple change.

    I didn't even know you could access that (not sure I would've thought of it, even if i did; well maybe after some brainstorming, that's assuming I knew it existed as I've surprised myself with the arrays).

    I would like to learn functions, but they look fancy to me.

    I don't know the coding process or the logic.

    Sort of like arrays. They look scary, a little bit like driving off the cliff, but you're glad once you did it.

    Any suggestions as far as resources to learn how to use functions (a kindergartener's manual for Construct 2)?

    I don't really understand the logic or the coding from the capx examples I've been able to download.

    <----AmpedRobot----> throws virtual items at the blackhornet from games not yet made.

    Must code to finish something!

    Wait, I don't must anything.

    I actually enjoy it, ha ha.

  • Functions just let you call the same code whenever you want. I cleaned things up. You can call Pick4Random() whenever you need.

    http://www.blackhornettechnologies.com/ ... 2_BHT.capx

  • Wow, thank you.

    Much obliged.

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