Random Number Without Repeat Problem

0 favourites
  • 9 posts
From the Asset Store
Easily generate many levels from a set of pre-built scenes (Construct 3 template)
  • New to construct, first time posting on the forums:

    I have a game, which On Collision with "DoorWarp"(an invisible sprite object) the game sets a variable RoomRandom from 1-10, this part is working great. Depending on the value it retrieves, it loads a new layout based on the value. Level 1-1 for 1, 1-2, for 2, etc.

    I'm making a rogue-like-esque-lite game where level progression should be random.

    Each 'level' is on it's own layout, and is associated with a RoomRandom number as explained before.

    How do i make it so RoomRandom doesn't repeat numbers when you collide with DoorWarp.

    For example, i can be on 1-1 and hit the DoorWarp, loading 1-5... Perfect.

    I beat 1-5 and RoomRandom chooses a new number, if this number is 1, it loads 1-1.... Undesired.

    Sometimes, even, i beat 1-5 and RoomRandom lands on 5 again and reloads 1-5.... Undesired.

    How do i make it so once RoomRandom picks a number, that number can no longer be chosen again until game over.

  • Try Construct 3

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

    Try Now Construct 3 users don't see these ads
  • Easy Peasy and classic coding. There are a few ways to do this. Personally Like strings because of the small object it represents. but we will go with arrays.

    You have an array with size = NumRooms

    Repeat NumOfRooms

    Array.Set(0) to loopindex

    boom you have your list of rooms. Next step and here is the magic.

    // get a random index from remaining rooms

    var index = floor( Random( 0, Array.Size ) )

    // get the room number from the array at index

    var roomToLoad = Array.Get( index )

    // remove the value at index position so it doesn't happen again.

    Array.RemoveAt( index )

    You have now mastered Construct 2

    As a warning. This is Psuedo Contrsuct 2 terms. I could have the terms wrong. Such as RemoveAt could in fact could be PopAt, DeleteAt... or something else. Also i think think Array.Size should probably be Array.Width. C2 naming is kinda wierd.

  • I was going to suggest something else, but jayderyu example is more elegant and graceful. What he is suggesting to do is perfect.

  • oh by the way. You can also put entire Layout names as "strings" in side the array instead of just numbers. Works really well.

  • thanks guys! you're the best

  • actually would you perhaps be able to link a capx example, because i'm having trouble understanding whats really going on here, i can't even find the stuff in c2 editor to do any of this

  • like how would i implement this into loading a layout, and what is this loopindex, i'm just really confused.

  • Nevermind, i figured it out! thanks a ton.

  • Can someone explain how? I have 4 layouts. And they go to each layout randomly by code "Layout " & ceil(random(3) + 1). And it's working. But the problem is, I want each layout to appear once.

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