Is there a way to change the level music, depending of the time?
I have forest level with forest (day) ambient track, but I want that the ambient track is set to "forest night" when the time comes at 10h pm to 6h am
music changing with time
- Skuggasveinn
- Posts: 562
- Joined: Wed Sep 26, 2012 5:28 pm
Re: music changing with time
Not fully tested
Bug I've not resolved is if the party enters the area for the first time during the night, then the cycle will not be correct until morning.
but this will hopefully get you in the right direction.
Place a counter in you map that has the name daycounter, set the value to 1
Have a timer check the following script
Bug I've not resolved is if the party enters the area for the first time during the night, then the cycle will not be correct until morning.
but this will hopefully get you in the right direction.
Place a counter in you map that has the name daycounter, set the value to 1
Have a timer check the following script
Code: Select all
GameMode.getHours = function() -- by Jkos, converts the time to 0 - 23 hour format
local hour = math.floor(GameMode.getTimeOfDay()/(2/24))+6 -- add 6 hours because 0.0 = 6:00
return hour%24
end
function setMusicbyTime()
local isday = false -- is true during the day, false during the night
wtii = GameMode.getHours() -- for debug, gets the time
--hudPrint(""..wtii.."") -- for debug, prints out the time
if GameMode.getHours() >= 6 and GameMode.getHours() <= 22 then -- change this to reflect your daytime
isday = true
--hudPrint("its day") -- for debug, prints out "its day" of the clock is between 6 and 22
else -- if its not the timeframe above will default to night
isday = false
--hudPrint("its night") -- for debug, prints out "its night" if the clock is between 23 and 5
end
if not isday and daycounter.counter:getValue() == 0 then -- checks if isday is false and if the daycounter is 0
GameMode.playStream("forest_night") -- if the checks above are OK starts playing forest_night ambient
daycounter.counter:setValue(1)
elseif isday and daycounter.counter:getValue() == 1 then -- checks if isday is true and daycounter is 1
GameMode.playStream("forest") -- if the checks above are OK starts playing forest ambient
daycounter.counter:setValue(0) -- failsafe so the checks above will not return true again to prevent the ambient track from resetting
else
end
end
Re: music changing with time
thank you
---> trying!
---> trying!
Re: music changing with time
works fine!
I have waited with the rest function, the music has changed.
I suppose this works only for the level where the script is placed? I need to copy and paste, the counter / time and the script on the other levels ?
By the way, in the same time I have go to another level, it as play the defaut music of this level (of course), but when I came back to the other level where the script is, the music was no more night_ambient, but was reseted to defaut. could this be avoided?
I have waited with the rest function, the music has changed.
I suppose this works only for the level where the script is placed? I need to copy and paste, the counter / time and the script on the other levels ?
By the way, in the same time I have go to another level, it as play the defaut music of this level (of course), but when I came back to the other level where the script is, the music was no more night_ambient, but was reseted to defaut. could this be avoided?
Re: music changing with time
Another issue
the music change everywhere. It starts by playing forest day ambient, then the night ambient. This complicate things
any help?
the music change everywhere. It starts by playing forest day ambient, then the night ambient. This complicate things

any help?
Re: music changing with time
Here is a simpler script that will do what you want. Just connect a timer with a very short duration (I recommend 0.001) to updateMusic():
You should be able to use it for as many levels as you want. The music will only change on the same level as the script, and it will be properly adjusted when the player first enters the level, too.
Code: Select all
prevLevel = self.go.level
function updateMusic()
local night = GameMode.getTimeOfDay() > 1.0
if party.level == self.go.level then
if (prevLevel ~= party.level) or
(night and not isNight) or
(isNight and not night) then
isNight = night
updateTrack()
else
isNight = night
end
end
prevLevel = party.level
end
function updateTrack()
if isNight then
GameMode.playStream("forest_night")
else
GameMode.playStream("forest")
end
end
Grimrock 1 dungeon
Grimrock 2 resources
I no longer answer scripting questions in private messages. Please ask in a forum topic or this Discord server.
Grimrock 2 resources
I no longer answer scripting questions in private messages. Please ask in a forum topic or this Discord server.
Re: music changing with time
that's great! thanks 
