Varying randomness

Get help using Construct 2

Post » Wed Aug 29, 2012 8:20 pm

Hi everyone,

I'm currently stumped again, I know what the problem is, I just don't know how to fix it. Basically I have a turret which currently has 4 different types of ammo in it and if any of the ammo is greater than zero it would fire based on it being random. My first solution is like this.



But the problem is because it chooses the ammo first, there are times when there is a gap between firings because it is choosing a random number with no ammo in it. So the way to fix this was to choose what ammo was available first, then choose a random number from that seed. For example if ammo1 and ammo2 was greater than 0 then the random seed would be 2, if all 4 ammo types is greater than 0 then the random seed would be 4 but I don't know how to link up the random chosen number with the ammo types. Fixed numbers wouldn't work because the random seed can change.

Here is the capx
http://dl.dropbox.com/u/57137706/variable%20random.capx
Just disable group 2 and enable group 3 if you want to see the old method.

Thank you for your time everyone.

Kind Regards.RockmanZero2012-08-29 20:24:28
B
25
S
161
G
3
Posts: 51
Reputation: 18,702

Post » Wed Aug 29, 2012 11:15 pm

I'd like to know a good answer to this also.

You could use 'Repeat' to keep generating a new number if that ammo type is at 0, but that would be a sh*tty solution.
B
28
S
8
G
1
Posts: 469
Reputation: 4,683

Post » Thu Aug 30, 2012 2:58 pm

Sorry keepee, but I don't understand how that would fix the problem could you elaborate please?RockmanZero2012-08-30 14:59:07
B
25
S
161
G
3
Posts: 51
Reputation: 18,702

Post » Sat Sep 01, 2012 8:11 pm

Sorry all, just giving this a bump incase anyone that can help missed it.
B
25
S
161
G
3
Posts: 51
Reputation: 18,702

Post » Sat Sep 01, 2012 8:49 pm

What you want, then, is a random shuffle of certain elements.

Keepee's solution would work however he is right in the fact that it is not the most efficient solution.

What I think you might need to do is fill out a 1-dimensional array beforehand, either manually or procedurally, then shuffle it.

Shuffling the array is not simple, but here's a nice example of it being done.

I might have missed a better way of doing it, but I think this will work if done right.
B
90
S
30
G
24
Posts: 3,189
Reputation: 32,400

Post » Sat Sep 01, 2012 9:19 pm

Many thanks Sqiddster, I will give it a shot. I tried to avoid arrays for my first project but I guess there's no helping it.
B
25
S
161
G
3
Posts: 51
Reputation: 18,702

Post » Sat Sep 01, 2012 9:33 pm

Here's what I came up with. It looks unnecessarily complicated haha.

[code]
Local text ammoString = ""

System: Set ammoString to Sprite.ammo1 > 0 ? ammoString & "1" : ammoString
System: Set ammoString to Sprite.ammo2 > 0 ? ammoString & "2" : ammoString
System: Set ammoString to Sprite.ammo3 > 0 ? ammoString & "3" : ammoString
System: Set ammoString to Sprite.ammo4 > 0 ? ammoString & "4" : ammoString

System: Set randomNumber to int(mid(ammoString, floor(random(len(ammoString))), 1))
[/code]

Basically create a string with the available ammos eg. "134" and then choose a random character from that string.

randomAmmo.capx (r101)
B
54
S
28
G
18
Posts: 1,520
Reputation: 25,000

Post » Sun Sep 02, 2012 12:50 am

Once again thank you ramones, that's a pretty clever way to solve this problem. I've just been using number values, didn't know you could use strings like that.
B
25
S
161
G
3
Posts: 51
Reputation: 18,702

Post » Mon Oct 29, 2012 9:28 am

Pattern gen plugin might help you to get shuffling result in some cases.rexrainbow2012-10-29 09:39:03
B
97
S
22
G
178
Posts: 4,118
Reputation: 104,043


Return to How do I....?

Who is online

Users browsing this forum: 99Instances2Go, Matthew de and 2 guests