Solarus-Games English Forum

Solarus => Development => Topic started by: Minefran12 on October 31, 2017, 08:18:32 PM

Title: Variable music.
Post by: Minefran12 on October 31, 2017, 08:18:32 PM
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,...
Title: Re: Variable music.
Post by: Diarandor on November 01, 2017, 12:49:44 AM
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.
Title: Re: Variable music.
Post by: Minefran12 on November 01, 2017, 12:04:00 PM
Thank you, but how i use it.
Title: Re: Variable music.
Post by: Diarandor on November 01, 2017, 02:17:35 PM
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
Title: Re: Variable music.
Post by: MetalZelda on November 01, 2017, 11:55:42 PM
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)
Title: Re: Variable music.
Post by: Diarandor on November 02, 2017, 01:59:17 AM
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)
Title: Re: Variable music.
Post by: Minefran12 on November 14, 2017, 06:27:00 PM
Thanks, i was doing it with an if to each music.