music changing with time

Ask for help about creating mods and scripts for Grimrock 2 or share your tips, scripts, tools and assets with other modders here. Warning: forum contains spoilers!
Post Reply
bongobeat
Posts: 1076
Joined: Thu May 16, 2013 5:58 pm
Location: France

music changing with time

Post by bongobeat »

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
My asset pack: viewtopic.php?f=22&t=9320

Log1 mod : Toorum Manor: viewtopic.php?f=14&t=5505
User avatar
Skuggasveinn
Posts: 562
Joined: Wed Sep 26, 2012 5:28 pm

Re: music changing with time

Post by Skuggasveinn »

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

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
Link to all my LoG 2 assets on Nexus.
Link to all my LoG 1 assets on Nexus.
bongobeat
Posts: 1076
Joined: Thu May 16, 2013 5:58 pm
Location: France

Re: music changing with time

Post by bongobeat »

thank you
---> trying!
My asset pack: viewtopic.php?f=22&t=9320

Log1 mod : Toorum Manor: viewtopic.php?f=14&t=5505
bongobeat
Posts: 1076
Joined: Thu May 16, 2013 5:58 pm
Location: France

Re: music changing with time

Post by bongobeat »

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?
My asset pack: viewtopic.php?f=22&t=9320

Log1 mod : Toorum Manor: viewtopic.php?f=14&t=5505
bongobeat
Posts: 1076
Joined: Thu May 16, 2013 5:58 pm
Location: France

Re: music changing with time

Post by bongobeat »

Another issue

the music change everywhere. It starts by playing forest day ambient, then the night ambient. This complicate things :o

any help?
My asset pack: viewtopic.php?f=22&t=9320

Log1 mod : Toorum Manor: viewtopic.php?f=14&t=5505
minmay
Posts: 2790
Joined: Mon Sep 23, 2013 2:24 am

Re: music changing with time

Post by minmay »

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():

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
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.
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.
bongobeat
Posts: 1076
Joined: Thu May 16, 2013 5:58 pm
Location: France

Re: music changing with time

Post by bongobeat »

that's great! thanks :)
My asset pack: viewtopic.php?f=22&t=9320

Log1 mod : Toorum Manor: viewtopic.php?f=14&t=5505
Post Reply