I’m pretty much brand new to any sort of game development. The only experience I had was copy and pasting some scripts into garry’s mod to make a plane on a sandbox server, so apologies if this question is dumb or poorly formatted.
I’ve mostly found GDevelop to be pretty intuitive, but I have run into a wall. Effectively what I want to do is create a playlist of music tracks which can be changed by the player. I have currently set up the music to play randomly from a random range of 0-3, but I want to allow players to disable tracks in the settings. I can’t figure out how to randomly select from a list of numbers that has gaps i.e. track 1, track 3, track 4, track 5. Is there a way to randomly select a number like this or perhaps a workaround?
Thanks!
1 Like
First you need a condition to check if the random number should play or has been deactivated by the player. For example if the player deactivates the track 2, create a child variable “2” in a structure variable “disabled_tracks”, if the player deactivate the tracks 2 and 6 the structure will be:
disabled_tracks
2 0
6 0
(the values doesn’t matter, just the child names)
There’s a condition to check if a child exist in a variable, so you can check if the random number is disabled or not:
Child ToString(Variable(random_track)) exists in variable "disabled_tracks"
The condition will be true if the random number is disabled.
Then you need a while loop, the idea is: pick a random track, and while the picked track is disabled pick a new random track, the while loop will end when a picked track is available so you can use it:
[code]Conditions: Your conditions to change the track
Actions: Do = Random(6) to the variable “random_track”
// sub-event
While Child ToString(Variable(random_track)) exists in variable "disabled_tracks":
//sub-sub-event
Conditions: No conditions
Actions: Do = Random(6) to the variable "random_track"
// sub-event
Conditions: Variable "random_track" is = 0
Actions: Play "My_Music_0.ogg"
// sub-event
Conditions: Variable "random_track" is = 1
Actions: Play "My_Music_1.ogg"
// sub-event
Conditions: Variable "random_track" is = 2
Actions: Play "My_Music_2.ogg"
…[/code]
Warning: If the player disables all the tracks (i.e. the structure variable will have all the children from 0 to 6) the while loop won’t be able to generate a random number that doesn’t exist in the structure. In other words it will generate an infinite loop and your game will freeze!
1 Like
Thanks! You saved my sanity here. I’ll figure out a way to prevent the infinite loop scenario.