Problem with "random()" on an instance varaible

Discussion and feedback on Construct 2

Post » Thu Jul 03, 2014 1:35 am

I have an instanced object with a variable called "salvo" that I want to change at random up to the number 3 (possibly even more in the future) every time a new object is spawned. I set up this test to see if the variable is changing but it stays constant when I use the "random(3)". Strangely enough if I hard code it to say >set salvo to 1 on the next object spawned it will work. Is there a reason my "random" code won't work for this? Am I missing something? Thanks in advance for the help! File is attached:
You do not have the required permissions to view the files attached to this post.
B
6
Posts: 28
Reputation: 404

Post » Thu Jul 03, 2014 1:43 am

you need to add Round so make it Round(Random(0,2))
B
42
S
17
G
2
Posts: 850
Reputation: 6,209

Post » Thu Jul 03, 2014 2:08 am

Wow, looks like that worked. Not sure why that would be required, guess I'll dig through the docs later. Thanks for the quick help though, Really appreciate it!
B
6
Posts: 28
Reputation: 404

Post » Thu Jul 03, 2014 2:08 am

@Domino3057 I guess you realise the above works because random creates a decimal value, e.g. 1.23892394 and so will not be equal to 0, 1, or 2.

Also maybe you should have an On Created event for Bad objects and do the random stuff there. Not sure if it's a good idea to operate on a newly created object until at least the next tick. (More experienced C2'ers please jump in).
B
24
S
9
G
4
Posts: 1,646
Reputation: 6,596

Post » Thu Jul 03, 2014 3:04 am

If you have small values you want randomized, you can also use choose(1,2,3,4,5) etc.
B
70
S
24
G
19
Posts: 1,757
Reputation: 17,616

Post » Thu Jul 03, 2014 3:05 am

choose( 0,1,2,3,4,5,6) is also fantastic. Also Choose you create weighted results.

choose( 0, 0, 0, 0, 1,1,1,2, 2, 3 )
or
choose( 0 , 0, 1, 2, 3, 3)

so on etcetera. However Choose sucks if you need to get a dynamic result. Choose needs to be defined. It's too bad Choose can't be used with an array :P
B
90
S
18
G
9
Posts: 2,455
Reputation: 15,038

Post » Thu Jul 03, 2014 3:31 am

@Codah I didn't even consider decimals were being called as well. Thanks for clearing that up.

Both of these options work great. At least for this part in my code the "choose" expression actually runs smoother. Thanks so much for the multiple options to solve my issue.

This is a great community, very helpful!
B
6
Posts: 28
Reputation: 404


Return to Construct 2 General

Who is online

Users browsing this forum: Syazanie Amirin, TheRealDannyyy and 1 guest