Piecewise Random

0 favourites
  • 14 posts
From the Asset Store
Easily generate many levels from a set of pre-built scenes (Construct 3 template)
  • Hello! I want to add a random that keeps my CuiserWingmen from overlapping the Cruiser itself.

    I added this:

    I am not sure if "|" works in this case as an "or". I just read somewhere that it is what construct uses for "or".

    Any help much appreciated.


  • I am not a 100% sure, but that seems to want to return either true or false - not one of the values you want.

    I would use

    Set GotoX to choose(Cruiser.X+random(20,40),Cruiser.X-random(20,40))

    Set GotoY to choose(Cruiser.Y+random(20,40),Cruiser.Y-random(20,40))

  • It worked!

    Thank you for responding so quickly!

  • I think you might have been misunderstanding the "logical or" operator.

    It evaluates the left hand side and checks if that side it "truthy". If it is truthy it will return the left hand value. If it is "falsy" it will evaluate the right hand side and return that value instead. For numbers "0" is the only "falsy" value.

    Looking at your expression it would have always been returning the left hand side. AllanR's suggestion of using the "choose" expression is what your expected the "or" operator to do I expect? Which is to randomly return one of the values.

  • Hello! I want to add a random that keeps my CuiserWingmen from overlapping the Cruiser itself.

    I added this:

    I am not sure if "|" works in this case as an "or". I just read somewhere that it is what construct uses for "or".

    Any help much appreciated.

    AllanR already solved this but another way you can do this if you want to be a little more DRY is

    Set GotoX to Cruiser.x + (choose(-1, 1) * random(20,40))

    As for expressions and the " | " operator I used to get really confused too. This is how they work

    Set X to 2 = 1 ? 5 : -5

    This can be translated to: Is 2 equal to 1? If yes, return 5. If no, return -5

  • Ah yes AllanR gave me the solution I needed. I need something extra, though. The distribution I have right now is like this:

    The cruiser is green, and the red is approximately the distribution of the wingmen.

    I need something like this:

    Thank you all for your help!

  • Try Construct 3

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

    Try Now Construct 3 users don't see these ads
  • was there supposed to be another picture?

  • whoops yes

    This is what I needed

  • If you don't mind the distribution being more of a circle than a square you can do:


    Set AngleVariable to random(360)

    set X to cruiser.X + 40*cos(AngleVariable)

    Set Y to cruiser.y + 40*sin(AngleVariable)

    This will position the wingmen randomly around the ship in a circle with a distance of 40pixels

  • how many wingmen can there be? is there a minimum distance you want between wingmen?

    and the wingmen have to be anywhere on the red line?

    (it would be a lot easier to evenly distribute the wingmen on a circle around the cruiser)

    first I would have a variable named WingmanOffset to define how far out the box is from the cruiser.

    WingmanOffset = 40 (each side of the square will be 2 x the offset)

    then choose which line of the box the wingman is on (Top, Bottom, Left, Right), and pick a random point on that line.

    if Top then Y = -WingmanOffset, X=random(2 x WingmanOffset) - WingmanOffset

    if Bottom then Y = WingmanOffset, X=random(2 x WingmanOffset) - WingmanOffset

    if Left then X = -WingmanOffset, Y=random(2 x WingmanOffset) - WingmanOffset

    if Right then X = WingmanOffset, Y=random(2 x WingmanOffset) - WingmanOffset

    EDIT: I should have refreshed! :) NOAH is right, the other advantage of a circle is that you can space the wingmen an equal number of degrees apart.

  • I made a quick test...

    the square button doesn't try to evenly distribute the wingmen at all - just randomly creates them.

    the circle button evenly spreads them apart.

    there are textboxes to enter the number of wingmen you want, and the offset from the cruiser...


  • First, noah, that idea sounds great! I would definitely prefer the wingmen to be in a circle, but if it wasn't possible, I would go with the alternative.

    Also, AllanR, your solution is also something I wanted to add (if I am reading this right). If the wingman is on the left side of the ship, then tween only to another point on the left side. If it is in a corner, pick the sides it is on. This keeps the wingmen from traveling across the center in an x shaped pattern.

    Furthermore, the project you posted I am unable to run. Construct crashes every time I try to run it (is it possible it is because the project was made in a beta version?)

  • I saved it in the last stable... so try this:


  • yep works now

    If I added tweening, would the ships stay on the same side, or would i need to add that?

    Thank you again for your help!

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