Play random audio file out of some folder

Place to get help with not working mods / modding interface.
Post Reply
boernsen2
Inserter
Inserter
Posts: 21
Joined: Thu Dec 02, 2021 5:36 pm
Contact:

Play random audio file out of some folder

Post 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!
Last edited by boernsen2 on Sat Apr 09, 2022 3:19 pm, edited 1 time in total.

User avatar
DaveMcW
Smart Inserter
Smart Inserter
Posts: 3700
Joined: Tue May 13, 2014 11:06 am
Contact:

Re: Play random audio file out of some folder

Post 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,
}

boernsen2
Inserter
Inserter
Posts: 21
Joined: Thu Dec 02, 2021 5:36 pm
Contact:

Re: Play random audio file out of some folder

Post by boernsen2 »

Thank you very much!!! :)

Post Reply

Return to “Modding help”