Variable music.

Started by Minefran12, October 31, 2017, 08:18:32 PM

Previous topic - Next topic
October 31, 2017, 08:18:32 PM Last Edit: November 01, 2017, 12:18:03 PM by Minefran12
Hi, Is posible to program a place to have more than 1 music? I want to do a specific place that when you enter the music will variate, one time the song great_fairy, other time sanctuary,...

Yes, you can use the random Lua function to choose randomly the music that you will play on a map. You can do this from the map script.
"If you make people think they're thinking, they'll love you. But if you really make them think, they'll hate you."

November 01, 2017, 12:04:00 PM #2 Last Edit: November 01, 2017, 12:18:25 PM by Minefran12
Thank you, but how i use it.

Quote from: Minefran12 on November 01, 2017, 12:04:00 PM
Thank you, but how i use it.

The "math.random()" function of Lua is explained here:
http://lua-users.org/wiki/MathLibraryTutorial

And the function to start a chosen music is in the Solarus Lua API, here:
http://www.solarus-games.org/doc/1.5/lua_api_audio.html#lua_api_audio_play_music
"If you make people think they're thinking, they'll love you. But if you really make them think, they'll hate you."

November 01, 2017, 11:55:42 PM #4 Last Edit: November 02, 2017, 12:02:59 AM by MetalZelda
I should go like this

In your music directory, name your music like so 'music_name_0, music_name_1', etc, etc

In your code

Code (lua) Select

local music = math.random(0, x) -- Where X = number of music with as name "music_name_"
local function play_music()
  sol.audio.play_music("music_name_" .. music, function()
    -- Re-random the music
    -- Get the old number
    local old = music

    -- Set a new random value
    music = math.random(0, x)  -- Where X = number of music with as name "music_name_"

    -- Avoid repetition
    if music == old then
      -- Re-random the music number
      music = math.random(0, x)  -- Where X = number of music with as name "music_name_"
    end

    -- Call the function to play another music
    -- Remark, if you use "LOOPSTART" in the music metadata, the sol.audio.play_music callback (this code essentially) will never be called.
    play_music()
  end)
end

-- Start the music. The only way to cancel the phase is to use sol.audio.stop_music()
play_music()


That's basic stuff but that's the way to go essentially.

If you want to play specific tracks rather than random one, you can do the same thing, just replace music = math.random(0, x) by music = (music + 1) % x (replace x by the number you want, this is the number of tracks you wanna play)

There is no need to rename files, and that is a bad idea (it would be a loss of time and you wouldn't be able to guess from the filenames which one is each song). It is much better to define a list of strings with all the music names:
Code (Lua) Select
music_list = { ...blablabla... } --  <--- Put here the list of music names (strings).
and then just generate a random number between 1 and #music_list to choose the music. Something like this:
Code (Lua) Select
local index = math.random(1, #music_list)
local music_id = music_list[index]
sol.audio.play_music(music_id)
"If you make people think they're thinking, they'll love you. But if you really make them think, they'll hate you."

Thanks, i was doing it with an if to each music.