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

Just started using Construct 2? Post your questions here

Post » Sat Aug 06, 2016 4:34 am

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.

http://www.darksunpictures.com/public/array2.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.
Last edited by AmpedRobot on Sun Aug 21, 2016 4:48 am, edited 1 time in total.
B
37
S
20
G
18
Posts: 259
Reputation: 12,922

Post » Sat Aug 06, 2016 5:32 am

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.
ImageImageImage
B
71
S
22
G
241
Posts: 3,734
Reputation: 134,494

Post » Sat Aug 06, 2016 8:56 am

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.
B
37
S
20
G
18
Posts: 259
Reputation: 12,922

Post » Sat Aug 06, 2016 2:59 pm

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
ImageImageImage
B
71
S
22
G
241
Posts: 3,734
Reputation: 134,494



Return to Beginner's Questions

Who is online

Users browsing this forum: No registered users and 4 guests