How do I choose randomly from a set of strings and not get the same one twice in a row?

0 favourites
  • 6 posts
From the Asset Store
2D fighting template based in the game that defined the fighting games genre.
  • Hello,

    I've been struggling but largely ignoring this problem in my game for a while. I'm developing a metroidvania that involves a lot of situations where this crops up. Most of my bosses are developed using a finite state machine, so most of the time there in an event saying something along the lines of: "set state [choose("state1", "state2", "state3")]. When the state is chosen, there are some times the same one will play over and over due to random chance. I have also had issues recently when adding sound effects. I have 4 footstep audio files but when I choose randomly, it often plays the same sound multiple times in a row, which sounds off-putting.

    The main question is: How can I choose randomly from a set of STRINGS and not get the same one twice in a row?

    I have looked into advanced random features such as permutation tables but they mostly include numerical systems. I would assume there's a way to set strings equal to the numbers in the permutation tables or something but I am not familiar enough with advanced random to think of something.

    Are there any simple solutions I'm missing? Or what is the most efficient way of implementing something to regulate this?

    Any ideas would be great.

    Thanks!

  • if all you want to do is not repeat the previous one, then you could save the previous choice and make a While Loop keep generating a new one until its not equal to the previous.

    just make sure to save the one you pick as the "new" previous one

    something like this

  • Put your strings into an array.

    Set a variable to a random number - floor(random(array.width)). Use this number to read the string in the array at that index array.at(variable), then array - delete that index.

    You can do the same thing with a permutation table - each number in sequence will be randomized, and refer to an index in the array where you have your string.

  • Try Construct 3

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

    Try Now Construct 3 users don't see these ads
  • if all you want to do is not repeat the previous one, then you could save the previous choice and make a While Loop keep generating a new one until its not equal to the previous.

    just make sure to save the one you pick as the "new" previous one

    something like this

    This looks like what I need it to do! This will definitely work for the finite state machine enemies, but I'm wondering if you have any ideas on how to log the audio file that is chosen as the new previously played?

    Because it chooses from a list of sound files, I don't know how to track which one it chooses.

    Either way, your suggestion is very helpful. Thanks!

  • just make a string and have the choose function populate the string, then in Audio play just use the string (instead of the whole choose function), that way you will always know which one you chose.

  • I think permutation table is the easiest way to do this.

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