Sound callback (text and sound sync) (SOLVED)

Hi!

I have dialog in my game and I would like to have text synced with the sound.

I need a basic callback from a Sound-object when it has finished playing.
Looking at http://www.defold.com/ref/sound I can’t find anything.

My emergency solution would be to hard code the length of each sound file, but that shouldn’t be necessary?

How would you solve this?

1 Like

No, you are right, there is no way to know when a sound is finished. I’ll see if this feature is in the backlog and will add a request if it’s not there.

DEF-1549

1 Like

Workaround: If you have many sounds you can perhaps write a script that emits a lua source file with length data into the project.

1 Like

This isn’t an exact solution but I’ve built a little sound queue. You could modify it to send a done message back to the sender. To use it just create a new game object (sound.go) and attach all the wav files and the following script. Then to play it just:

msg.post("sounds", "play", { wav='boom'})

local head = nil
local tail = nil

function init(self)
	self.timer = 1
end

function update(self, dt)
	self.timer = self.timer + dt
	if self.timer < 1 then
		return
	end
	self.timer = 0

	if ( not (sound.is_music_playing()) and not( head == nil ) ) then
		msg.post( "#"..head.wav , "play_sound" , { gain = 1.0 } )
		print("Playing  ",head.wav)
		head = head.next
		if ( head == nil ) then
			tail = nil
		end
		pprint( head )
	end
end

function on_message(self, message_id, message, sender)
    print("sounds.script",message_id)
    
    if ( message_id == hash("play") ) then
    	local next = {}
    	next.wav = message.wav
    	
    	if ( tail == nil ) then
    		head = next
    		tail = next
    	else
    		tail.next = next
                    tail = next
    	end
    	
    	pprint( head )
    end
end
3 Likes

Thank you for sharing!

+1 for having a sound callback.

I’m trying to figure out how to only have a dynamic object impact sound play once even though there may be multiple tiny contact impacts (until the object comes to rest) that trigger my sound…

@jweaver911

there is a simple workaround: have a boolean called “sound_triggered” and set it to false.

Then on a collision, do “if not sound_triggered” then play sound and set sound_triggered to true. Then, after about 1 second (or however long it is to complete the collision), set it to false again

edit: in fact I just saw there’s a useful article here that explains exactly how to do that in detail: http://www.defold.com/manuals/sound/

3 Likes

Thanks for the link! I’ll have to incorporate this technique. :thumbsup:

The code for that “gate” is pretty straightforward. But what the doc doesn’t tell you is where to put this. Is it best to create a new game object with this gate script? Or do I use the gate script in my game object that is calling the sound?

It’s details like these that, while they may be obvious to regular Defold users, make or break the information for new users like myself.

While it doesn’t directly say it, the last code example does show it as its own game object. I’m doing it that way in my game and it works.

@jweaver911

The article on gating does imply that you should make a new game object and send messages to it to play sounds. You would need to be familiar with message passing to understand that.

Re: beginner users. I started with defold two or three months ago and I think the documentation is pretty good (and the help that has been provided by this forum has been exceptional)… however, the fact is that making even simple games is a complicated undertaking. Many game engines (via marketing or simply via your own imagination and naivety) make you think that in no-time-at-all you will have made your first game. But the people who can make games very quickly have a large amount of experience behind them. I believe that whatever engine you use, you need to gain an understanding of a number of concepts that are generally logically consistent, but often unintuitive.

It’s also true that many people do not realise how long it can take before these concepts become obvious. The same thing happens with language learning, or learning to drive a car: once you have learned, it becomes bewildering to see other people struggle to change gear or steer while reversing.

3 Likes

@88.josh I agree… All that is true. And while I don’t find message passing to be that obtuse, I do find the path and url structure to be more elusive at times. That part is the confusing part for me. Editor 2.0 will help this as it displays the object urls in a readable and obvious nature.

@codecloak So the object just sits out in the level somewhere. Where do you have your sounds then? Do you recommend placing them In the game objects they’re playing for? Or would it be better to store them all somewhere else?

If I’m gating a sound, I put them on the sound gate object otherwise I put them where I need to play them.

1 Like

Interesting. Ok. :thumbsup:

@jweaver911 yes, I use print(msg.url()) all the time which prints an object’s URL to the console. Otherwise I can never figure it out.

I use a collection (which is always loaded) called “loader” to deal with all the loading and unloading of the levels (there are many in my game) and that is where I’d put the sound game object.

1 Like

Yeah… I’ve used msg.url() before but it doesn’t always seem to work. Like right now I have it in my init callback of my player and it’s outputting nothing.

EDIT: Ahh… Turns out my atom editor changes were not being updated in the game. Not sure how that happened.

@jweaver911 yeah, i’m always doing stuff like that (writing and rewriting a script only to discover that it was perfect the first time but I had failed to attach it to an object, etc). I’m curious to see how your game is looking!

@88.josh :slight_smile: Yeah, I’m not really sure what I’m doing with it yet. I intended to use this prototype as a tool for learning Defold. I wanted to make 2 players that can run around and chuck rocks at each other. Like a primitive Towerfall game :wink: Most of it’s all “thrown together” art.
I’m just about at the point where I can add second controller support for testing.

I have some other questions about how to do just that but they would be off-topic in this thread… as I’ve probably diverged enough already.

2 Likes

local two player is the best!!! I’ve always thought about making a compilation of two-games that could be done on mobile/tablets, because I think there aren’t many of those. Good luck!

Hey there.

Is there any solution to find out if a sound / music is finished???