How do I make a playback queue?

Get help using Construct 2

Post » Wed Apr 27, 2016 11:37 am

I have 3 songs for soundtrack, in 3 audiofiles. What I want to do is, when the first finishes, the second plays, and when second finishes, 3rd plays.

But can't find a way to do that. Anyone? :)
Posts: 20
Reputation: 1,169

Post » Wed Apr 27, 2016 5:11 pm

In the event sheet, you can use Audio's "On ended" trigger.

On ended
Triggered when a sound with a given tag finishes playing. This is not triggered for looping sounds.

On a naive level,

When you play any sound, you can give it a tag. So you can create 3 "On ended" triggers for each audio tag. We will name each tag "musictag1", "musictag2" and "musictag3" for each 3 music respectively.

When you play the first audio, give it tag "musictag1".

When the first audio finishes, "On ended" trigger for "musictag1" will fire. In this trigger, you can play the second audio, and give it tag "musictag2".
When the second audio finishes, "On ended" trigger for "musictag2" will fire. In this trigger, you can play the third audio, and give it tag "musictag3".
When the third audio finishes, "On ended" trigger for "musictag3" will fire. In this trigger, if you wish to play the first audio again, you can just play the first audio and give it tag "musictag1".

This naive implementation will have the music keep on playing these 3 musics forever.


A possibly better approach:

The problem with naive implementation above is that if you have 100 songs, doing all those "On ended" trigger charade for each audio is really silly and not feasible for bigger list of song.

The following is one of the better ways to do this, which name all songs to be in sequential indices, uses variables to keep track of which music is being played and one "On Ended" trigger:

1. Name all of your songs like this: "music1.ogg", "music2.ogg", ......., "music10.ogg" (Let's assume you have 10 music this time)

2. Create a global variable named "totalMusic" that keeps track of how many music are there. Since we assume there are 10 music here, we will set the variable "totalMusic" to 10.

3. Create a global variable named "currentMusic" which is used to track current music index.

4. Now, play the first music "music1" and give it tag "musictag1", as well as set currentMusic to 1.

5. Create "On Ended" Trigger which looks for the tag "musictag" & currentMusic. This means "On Ended" trigger will look for "musictag1", as currentMusic is set to 1 in step 4. In "On Ended" Trigger, do the following event and actions:
-- add 1 to the variable currentMusic
-- if currentMusic > totalMusic, set currentMusic to 1.
-- play music "music" & currentMusic and give it tag "musictag" & currentMusic.

With the logic in step 5 for "On Ended" trigger, this will play all of your 10 music one after another and also get back to the first music after the last music is finished.

Do you follow this so far? If not, feel free to ask.
I got a game that you multiply, breath fire with two heads and brawl foes to oblivion with your clones: (use Chrome on Windows for best performance)

My sites:
Posts: 411
Reputation: 8,469

Return to How do I....?

Who is online

Users browsing this forum: Lancifer, Sproadic and 9 guests