Problem with "random()" on an instance varaible

0 favourites
  • 7 posts
  • 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 need to add Round so make it Round(Random(0,2))

  • 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!

  • 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).

  • Try Construct 3

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

    Try Now Construct 3 users don't see these ads
  • If you have small values you want randomized, you can also use choose(1,2,3,4,5) etc.

  • 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 )


    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

  • 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!

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