Jump to content

Using a timer to play a song


Recommended Posts

So I'm making a character who dies every 22 minutes, called the Hatchling. I have a functional script that will instantly kill the Hatchling after a 22 minute timer... but I wanted a warning system.

My plan was that after 20 minutes, (2 minutes before death) a song will start playing. I made a timer script, based on the same timer for the Kill Loop.

Here's the scripts I have. If someone can help me, please do! I labelled them with comments, and even put in print() functions, but the print("EndSong plays") appears in the console before the Hatchling even loads in.

--Kills the Hatchling after a certain time. At release, it should be 22 minutes.
    AddPrefabPostInit("hatchling", function(inst)
			print("AddPrefabPostInit")
    if not inst.components.timer then
  inst:AddComponent("timer")
    end
    inst.components.timer:StartTimer("when the sun explodes",60*3) --should be "when the sun explodes",60*22)
			print("Timer start")
  local function YouGetKilled(inst)
		if inst.components.health and not inst.components.health:IsDead() then
		inst.components.health:Kill()
			print("running YouGetKilled")
	end
 end

  inst:ListenForEvent("timerdone", YouGetKilled)

-- revive yourself here
-- assume you have a home
    if inst.components.homeseeker then
  inst.Transform:SetPosition(inst.components.homeseeker:GetHomePos())
			print("Going Home")
    end
-- revive
  inst:PushEvent("ms_respawnedfromghost")
			print("respawning")

  inst:ListenForEvent("ms_respawnedfromghost", function()
    if inst.components.timer:TimerExists("when the sun explodes") then
  inst.components.timer:StopTimer("when the sun explodes")
			print("stopping timer")
    end
  inst.components.timer:StartTimer("when the sun explodes",60*3) --should be "when the sun explodes",60*22)
			print("restarting timer")
    end)
    end)


--This script should play The End Times when there's 2 minutes left in the Loop
    AddPrefabPostInit("hatchling", function(inst)
			print("AddPrefabPostInit")
    if not inst.components.timer then
  inst:AddComponent("timer")
    end
    inst.components.timer:StartTimer("EndSong",60) --should be "EndSong",60*20)
			print("EndSong Timer start")
			
			  inst:ListenForEvent("timerdone")
				inst.SoundEmitter:PlaySound("sound/customvoice") --adding this causes the game to crash upon loading the Hatchling. It doesn't give me anything useful, though.
				print("playing song")
			
  inst:ListenForEvent("ms_respawnedfromghost", function()
    if inst.components.timer:TimerExists("EndSong") then
  inst.components.timer:StopTimer("EndSong")
			print("stopping EndSong timer")
    end
  inst.components.timer:StartTimer("EndSong",60) --should be "when the sun explodes",60*22)
			print("restarting EndSong timer")
    end)
    end)

As a note: CustomVoice is the name of the .fev and .fsb files I'm using for the song. I'll be renaming it once I get everything working properly.

Link to comment
Share on other sites

21 hours ago, Rickzzs said:

Check your event listeners, you didn't register a second listener at all.

Also you should register one timerdone event listener and see which timer is it if there are more than one event.

I've gotten some help fixing it up now, and I've reached a point where it should be working, but FMOD can't find the "event, event group, event category or event property"

Link to comment
Share on other sites

36 minutes ago, ScrewdriverLad said:

I've gotten some help fixing it up now, and I've reached a point where it should be working, but FMOD can't find the "event, event group, event category or event property"

You need to check your sound file and find out the correct name(bank name, event name, etc.) of it. It is a bit tricky and I recommend you follow some tutorial.

Link to comment
Share on other sites

Create an account or sign in to comment

You need to be a member in order to leave a comment

Create an account

Sign up for a new account in our community. It's easy!

Register a new account

Sign in

Already have an account? Sign in here.

Sign In Now
 Share

×
  • Create New...