Music (Sequence) Callback Question


For my example: new_song is a Song object, which contains a sequence member variable. I have songs organized into playlists (also objects), and next_song() is a member function of Playlist objects.

When running this code, I end up running next_song() in this line, which is an issue as I intend to run this function only at the end of the sequence. I realize why this is happening, but I'm unsure of the remedy.

There must be something obvious that I'm just too tired to see, but I'm unsure of how to pass a member function as a callback to the :play() function.

PS - I do see the obvious idea of making next_song() a stand-alone function and passing the playlist. That might be where I go with it, but I'm still curious about the answer to this question. Member function as a callback solution would be preferred.

PPSS - I found another interesting solution. In Playlist:init(self) I can declare the function as such:

  self.next_song = function ()
    print("do next song stuff")

and then set the callback like this:


Is this inefficient? Maybe. Is this working? Yes.

The argument to sequence:play() is a callback function that gets called when the sequence is finished, but we aren't doing any validation on that like we should be so that we can throw an error if you pass something else. When it's time to call the callback the system skips it because it's not a function there. It looks like you got there eventually, and I don't think it's particularly inefficient to do it the way you're doing. If you want to go back to your original form, you'd do this instead:

new_song.sequence:play(function() current_playlist:next_song() end)

I'll file an issue to add a check there to err if you pass something other than a function. Thanks for catching this!

1 Like

Oh, of course. Run the member function as the only line of code in an in-line function. I think I prefer this solution as it avoids a messy initialization function. This exactly what I meant when I said I'm probably missing something obvious.

Thank you! :pray:

1 Like

@dave Hey followup related question: say I've got a song playing and the default behavior is for the finishedCallback to run a function called next_song.

Say I want to hard-stop the music, so I stop mid-sequence. In this case, I would not want the next song to play. But currently for my code, I can't figure out a simple way of stopping this once the finishedCallback is set.

Any suggestion on how I could go about also halting the next_song (finishedCallback) function?

I mean I can imagine basically putting next_song in an if statement and having a variable I track called, like, "should_continue_to_next_song" but that just feels wonky. It does not seem that I can just set my_sequence.finishedCallback = nil

PS - I just made a variable in my playlist objects that tracks paused status just as I said above. It works for now and doesn't add too much complexity.