For Each (random)

Get help using Construct 2

Post » Sat Feb 09, 2013 11:34 am

Hi guys. How to implement For Each (random)?

Example:

List: 1,2,3,4,5,6,7,8,9

For Each (random) : 3,7,2,1,9,5,8,4,6
B
9
S
3
G
1
Posts: 29
Reputation: 2,207

Post » Sat Feb 09, 2013 7:22 pm

Can you give this some context; do you wish to random bunch of object IIDs, random numbers or what are you trying to achieve?
B
24
S
8
G
7
Posts: 756
Reputation: 7,192

Post » Sat Feb 09, 2013 7:52 pm

i think he wants to do a foreach event ordered by random. if this is the case, you can use foreach (ordered) and in the expression use sprite.someinstancevariable ascending and on creation of the sprite just set its someinstancevariable to floor(random(999999)) for example. very pseudo random but as good as it can be im afraid.ranma2013-02-09 19:56:54
B
81
S
50
G
10
Posts: 555
Reputation: 13,009

Post » Sat Feb 09, 2013 7:53 pm

Random For Each all objects.

wizsg2013-02-09 19:55:00
B
9
S
3
G
1
Posts: 29
Reputation: 2,207

Post » Sat Feb 09, 2013 7:56 pm

why you used "for" in the first event when you can make a "repeat 10 times" event :)

also check my answer above, it should be fine.ranma2013-02-09 19:59:01
B
81
S
50
G
10
Posts: 555
Reputation: 13,009

Post » Sat Feb 09, 2013 7:56 pm

I believe "Choose" is what you are looking for:

choose(a, b [, c...])
Choose one of the given parameters at random. E.g. choose(1, 3, 9, 20) randomly picks one of the four numbers and returns that. This also works with strings, e.g. choose("Hello", "Hi") returns either Hello or Hi. Any number of parameters can be used as long as there are at least two


https://www.scirra.com/manual/126/system-expressions
B
13
S
4
G
3
Posts: 75
Reputation: 3,425

Post » Sat Feb 09, 2013 8:14 pm

@ranma
[QUOTE=ranma] why you used "for" in the first event when you can make a "repeat 10 times" event :)
[/QUOTE]

I just started learning to Construct 2, but I have development experience C #, PHP, JS.
Not quite accustomed to the approach to the development of Construct 2.


You suggest a good solution. I just do not know how to set sprite.someinstancevariable to create a sprite. Show how to do it.

B
9
S
3
G
1
Posts: 29
Reputation: 2,207

Post » Sun Feb 10, 2013 11:13 pm

when you create a sprite, select it and on the left pane (properties) you should have the ability to add a new instance variable to it, add it, call it "whatever" :)

then in the event sheet when you create the sprite just set its "whatever" instance variable to the floor(random(9999)) and in the next event make a foreach ordered, choose the sprite, ordered by expression sprite.whatever ascending
B
81
S
50
G
10
Posts: 555
Reputation: 13,009

Post » Mon Feb 11, 2013 5:12 am

Thank you friend! I did not know that you can sort by instance variable. Construct 2 pleases more and more :)
B
9
S
3
G
1
Posts: 29
Reputation: 2,207

Post » Mon Feb 11, 2013 6:15 am

I'd like to add that you can do ranma's method without an instance variable. Just use random(1) as the expression in for each ordered.
B
79
S
24
G
54
Posts: 4,738
Reputation: 40,739

Next

Return to How do I....?

Who is online

Users browsing this forum: Artpunk, Tombas and 17 guests