Return random X,Y where X,Y = A

0 favourites
  • 4 posts
From the Asset Store
Easily generate many levels from a set of pre-built scenes (Construct 3 template)
  • I need to pick a random x,y where the value is equal to 0. But, I need to return the x,y values, not the item value. (This is for spawning a sprite based on a tilemap where the tilemap frame is = 0)

    I have a solution already, but I'm imagining there is a loop I could do that would be a lot cleaner.

    My solution:

  • Try Construct 3

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

    Try Now Construct 3 users don't see these ads
  • I never use waits when I can.

    On function getXY
    — set myX to int(random(300))
    — set myY to int(random(400))
    — array: at (myX,myY) =0
    — — stop loop

    The only issue is it will loop forever if there are no 0s in the array. It also could take a while if there only a few 0s. Depending on how robust you want it you could have it try random places first, then make a list of all the locations with zero and pick one at random, and lastly if there are no zeros you could set the variables to -1 or nan or something.

  • Got that going. Appreciate the feedback. I've gotten a lot better with arrays and loops over the years, but they always make my head hurt. (As for the waits, I try not to use them. This is on a load screen so I'll probably give it a random time or trigger off a logo animation.)

    Thanks again!

  • 113000 out of 114600 and the random works in under 1 second still. Just an fyi (computers are really amazing).

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