Page 1 of 1

Play random audio file out of some folder

Posted: Sat Apr 09, 2022 2:34 pm
by boernsen2
Hi everyone,

I am wondering if it is at all possible to play a random audio file out of some folder or if you have any workarounds in mind?
I would like to be able to put any *.ogg files in there and have a random choice of these files played upon a certain event.

Thank you for any ideas!

Re: Play random audio file out of some folder

Posted: Sat Apr 09, 2022 2:59 pm
by DaveMcW
You have to list all the sounds in data.lua, but they can be random at runtime.

data.lua:

Code: Select all

data:extend{
  {
    type = "sound",
    name = "my-random-sound",
    variations = {
      {filename = "__my-mod__/sound/sound1.ogg"},
      {filename = "__my-mod__/sound/sound2.ogg"},
    },
    allow_random_repeat = true,
  }
}
script:

Code: Select all

/c game.player.surface.play_sound{
  path = "my-random-sound",
  position = game.player.position,
  volume_modifier = 0.5,
}

Re: Play random audio file out of some folder

Posted: Sat Apr 09, 2022 4:09 pm
by boernsen2
Thank you very much!!! :)