"Wait for completion" for BGM?

I know it was already asked, but those topics are many years old, so maybe in one of the MV updates such thing became possible? Basicly i want my event to wait until sound (BMG, BGS, does not matter) is finished playing before continuing the events.

Currently the only way to do this - is adding a lot of "wait" commands, but it's not optimal, because not only it's tiresome to do (since it's in frames instead of seconds, you need a lot of resstarts to guess the right time), but also can bug when something slows the work of the engine - in that case song keeps playing but timings for the event will trigger in wrong moment.

Theoreticly it sounds fairly easy - just have some check when track is finished. But since i were unable to find any working method for that despite not being the first person who asked - it should be a problematic thing to code. But still, perhaps someone solved it?
I know it was already asked, but those topics are many years old, so maybe in one of the MV updates such thing became possible?
It would help if you linked to the other threads you're talking about so we know what you're reading.

It's unlikely an MV update changed the structure of the AudioManager objects.

it's tiresome to do (since it's in frames instead of seconds, you need a lot of resstarts to guess the right time)
I don't understand why you would need any guessing or restarts.

If you know the length of a track, you multiply that many seconds by 60 frames. Seems straightforward.
Once the BGM track is loaded/playing, use this (note: if you don't have audio loaded or playing, it will return a null error) :
JavaScript:
this.wait(AudioManager._bgmBuffer._totalTime * 60);

Since it loops automatically, you have to calculate the wait time.

I mean, you could technically write your own waitMode as well for audio.
Bear in mind that BGMs and BGSs are meant to be constantly going in the background, hence being called "background music" and "background sounds", so no event should ever straight up stop while those are active. Sound effects are generally too snappy to worry about, leaving only the ME to be considered.
It would help if you linked to the other threads you're talking about so we know what you're reading.
This, for example
Since it loops automatically, you have to calculate the wait time.
As SwordofDusk said, i would rather use ME then, if it supports multiple minute files, so i won't have to deal with loops. How do i write it, then?

this.wait(AudioManager._mebuffer._totalTime * 60);

Will this work or i have to remove total time as well? Or maybe just number? Do i still need to calculate anything if it's without loop? Sorry, i am really bad at understanding the code.
This, for example

As SwordofDusk said, i would rather use ME then, if it supports multiple minute files, so i won't have to deal with loops. How do i write it, then?

this.wait(AudioManager._mebuffer._totalTime * 60);

Will this work or i have to remove total time as well? Or maybe just number? Do i still need to calculate anything if it's without loop? Sorry, i am really bad at understanding the code.
As SwordofDusk said, the properties of BGM and BGS are to loop continuously when played during a scene. Using an ME is best here.

I wouldn't use any code or go down this path if the code looks confusing because this is only the waiting part of it. You still need to check the current time of the audio being played to reference against that code. You would then use a parallel common event (run from a switch that turns itself off when completed) to try to time the BGM properly with that wait command and hope that the user doesn't change the audio with a battle or scene change. (Or however you wanted to set it up)

Yup. All this just to stop the BGM when it ends.

ME only plays once. Can't do much else besides stop it and check its properties. You could test that but make sure you are writing out the correct property:
._meBuffer instead of ._mebuffer.

(My mistake, it appears I also write it incorrectly above - changed, hurray for mobile haha)

In the case of the ME, why are you still needing a wait time for it?
As SwordofDusk said, the properties of BGM and BGS are to loop continuously when played during a scene. Using an ME is best here.

I wouldn't use any code or go down this path if the code looks confusing because this is only the waiting part of it. You still need to check the current time of the audio being played to reference against that code. You would then use a parallel common event (run from a switch that turns itself off when completed) to try to time the BGM properly with that wait command and hope that the user doesn't change the audio with a battle or scene change. (Or however you wanted to set it up)

Yup. All this just to stop the BGM when it ends.

ME only plays once. Can't do much else besides stop it and check its properties. You could test that but make sure you are writing out the correct property:
._meBuffer instead of ._mebuffer.

(My mistake, it appears I also write it incorrectly above - changed, hurray for mobile haha)

In the case of the ME, why are you still needing a wait time for it?
It seems like you misunderstood my intentions. I don't want to stop the music from playing. I want to pause an event so player will not be able to move and do stuff while specific track is being played.

My overall event sturcture is:
- Start playing the music. Certain events happen.
- Location change, certain events happen
- Same as above, 3 times more
- Then final location loads, but the dialogue only starts when music is finished, and not a second before or after, because i don't want the VO to interfere with music. I also can't stop the music because i need it to play until the end.
It seems like you misunderstood my intentions. I don't want to stop the music from playing. I want to pause an event so player will not be able to move and do stuff while specific track is being played.

My overall event sturcture is:
- Start playing the music. Certain events happen.
- Location change, certain events happen
- Same as above, 3 times more
- Then final location loads, but the dialogue only starts when music is finished, and not a second before or after, because i don't want the VO to interfere with music. I also can't stop the music because i need it to play until the end.
It appears there is a misunderstanding - is the wait command not enough then?

Wouldn't this be solved with a simple auto-run event for your cutscenes that is timed to the length of the track? So, play the ME and immediately invoke the AudioManager._meBuffer._totalTime to get how long it is and set the wait time to that * 60. event everything to be within that time frame and we are set correct?
It appears there is a misunderstanding - is the wait command not enough then?

Wouldn't this be solved with a simple auto-run event for your cutscenes that is timed to the length of the track? So, play the ME and immediately invoke the AudioManager._meBuffer._totalTime to get how long it is and set the wait time to that * 60. event everything to be within that time frame and we are set correct?
I am not sure that i understand the second part of your message.
But regarding the first one - wait kinda works, and i will use it if i won't find a better way to do this. But i can't use wait right away because in the first part of the song a lot of locations change, and a lot of events happen. Wait would put them on hold as well. So i can use wait only in final location, and to do that i first need to calculate how much of the song will be left to play there.

But even that won't be ideal, because i already had very negative experience with wait command when i created entire video clip as a main menu for one of my games, and it worked trough wait commands - wait, change picture, wait, and so on. I spent a lot of time calculating exact timings for each part of the song so the image change will happen in exact moments of the song, but still a slightest load on pc caused instant destabilisation of timings, and images were disaplayed at wrong time. And i believe that it was impossible at all to get the timing right on mobile version, because it lagged all the time.

If that can't be avoided - i still can use that method. But i just wanted to check if someone knows a better one. Basicly what i want is the same thing as "wait for completion" checkbox in events, but in music instead.
I am not sure that i understand the second part of your message.
But regarding the first one - wait kinda works, and i will use it if i won't find a better way to do this. But i can't use wait right away because in the first part of the song a lot of locations change, and a lot of events happen. Wait would put them on hold as well. So i can use wait only in final location, and to do that i first need to calculate how much of the song will be left to play there.

But even that won't be ideal, because i already had very negative experience with wait command when i created entire video clip as a main menu for one of my games, and it worked trough wait commands - wait, change picture, wait, and so on. I spent a lot of time calculating exact timings for each part of the song so the image change will happen in exact moments of the song, but still a slightest load on pc caused instant destabilisation of timings, and images were disaplayed at wrong time. And i believe that it was impossible at all to get the timing right on mobile version, because it lagged all the time.

If that can't be avoided - i still can use that method. But i just wanted to check if someone knows a better one. Basicly what i want is the same thing as "wait for completion" checkbox in events, but in music instead.
Thanks for the explanation.

Run another code:
AudioManager._meBuffer.seek() to get the current time and then set your wait at that time by subtracting it from the total time of the track (assuming that the track is already playing):
JavaScript:
const timeLeft =  AudioManager._meBuffer._totalTime - AudioManager._meBuffer.seek();
this.wait(timeLeft * 60);

I think tying it directly to the track and allowing the track to orchestrate your timing for these scenes would be best.

If you want a wait for completion, you'll have to dive into the game interpreter to patch in a waitMode for AudioManager. I may consider doing something like that if I have some time later tonight (assuming it is possible and effective after testing).

My current guess is to write something like this along with the cases inside the updateWaitMode function in Game_Interpreter to overwrite the function:
JavaScript:
case 'audiome':
    waiting = AudioManager._meBuffer;
    break;

To simplify, copy this below and save it as a plugin to test out:
JavaScript:
Game_Interpreter.prototype.updateWaitMode = function() {
    var waiting = false;
    switch (this._waitMode) {
    case 'message':
        waiting = $gameMessage.isBusy();
        break;
    case 'transfer':
        waiting = $gamePlayer.isTransferring();
        break;
    case 'scroll':
        waiting = $gameMap.isScrolling();
        break;
    case 'route':
        waiting = this._character.isMoveRouteForcing();
        break;
    case 'animation':
        waiting = this._character.isAnimationPlaying();
        break;
    case 'balloon':
        waiting = this._character.isBalloonPlaying();
        break;
    case 'gather':
        waiting = $gamePlayer.areFollowersGathering();
        break;
    case 'action':
        waiting = BattleManager.isActionForced();
        break;
    case 'video':
        waiting = Graphics.isVideoPlaying();
        break;
    case 'image':
        waiting = !ImageManager.isReady();
        break;
    // here is the audio case
    // Checks if there is anything in the buffer
    // Buffer clears when finished so this case should work for your ME
    case 'audiome':
        waiting = AudioManager._meBuffer;
        break;
    }
    if (!waiting) {
        this._waitMode = '';
    }
    return waiting;
};

That way you can call it with a script call in an event:
JavaScript:
this.setWaitMode('audiome');
// or this if you are not using the Game_Interpreter, aka, you are not in an event running this script
$gameMap._interpreter.setWaitMode('audiome');

I can't access/test anything currently so I can only theorize and work through things in my head at the moment and with github that has the corescripts to MV/MZ. My apologies in advance if it doesn't work.

Edit: I just tested the code and it works. Please let me know if this works for you - I'm not sure if it is exactly what you want currently, but it is doing its job as a wait
mode.
Thanks for the explanation.

Run another code:
AudioManager._meBuffer.seek() to get the current time and then set your wait at that time by subtracting it from the total time of the track (assuming that the track is already playing):
JavaScript:
const timeLeft =  AudioManager._meBuffer._totalTime - AudioManager._meBuffer.seek();
this.wait(timeLeft * 60);

I think tying it directly to the track and allowing the track to orchestrate your timing for these scenes would be best.

If you want a wait for completion, you'll have to dive into the game interpreter to patch in a waitMode for AudioManager. I may consider doing something like that if I have some time later tonight (assuming it is possible and effective after testing).

My current guess is to write something like this along with the cases inside the updateWaitMode function in Game_Interpreter to overwrite the function:
JavaScript:
case 'audiome':
    waiting = AudioManager._meBuffer;
    break;

To simplify, copy this below and save it as a plugin to test out:
JavaScript:
Game_Interpreter.prototype.updateWaitMode = function() {
    var waiting = false;
    switch (this._waitMode) {
    case 'message':
        waiting = $gameMessage.isBusy();
        break;
    case 'transfer':
        waiting = $gamePlayer.isTransferring();
        break;
    case 'scroll':
        waiting = $gameMap.isScrolling();
        break;
    case 'route':
        waiting = this._character.isMoveRouteForcing();
        break;
    case 'animation':
        waiting = this._character.isAnimationPlaying();
        break;
    case 'balloon':
        waiting = this._character.isBalloonPlaying();
        break;
    case 'gather':
        waiting = $gamePlayer.areFollowersGathering();
        break;
    case 'action':
        waiting = BattleManager.isActionForced();
        break;
    case 'video':
        waiting = Graphics.isVideoPlaying();
        break;
    case 'image':
        waiting = !ImageManager.isReady();
        break;
    // here is the audio case
    // Checks if there is anything in the buffer
    // Buffer clears when finished so this case should work for your ME
    case 'audiome':
        waiting = AudioManager._meBuffer;
        break;
    }
    if (!waiting) {
        this._waitMode = '';
    }
    return waiting;
};

That way you can call it with a script call in an event:
JavaScript:
this.setWaitMode('audiome');
// or this if you are not using the Game_Interpreter, aka, you are not in an event running this script
$gameMap._interpreter.setWaitMode('audiome');

I can't access/test anything currently so I can only theorize and work through things in my head at the moment and with github that has the corescripts to MV/MZ. My apologies in advance if it doesn't work.

Edit: I just tested the code and it works. Please let me know if this works for you - I'm not sure if it is exactly what you want currently, but it is doing its job as a wait
mode.
Sadly, either i am doing something wrong, or it's not working. I tried both lines, both as a script call and plugin command. It is supposed to put on hold dialogue, so it would happen after the track ends. But in either case dialogue appears instantly, during the music instead of coming after.

Plugin is added and switched to ON, there are no variables or anything else to do with it, just the switch.

I should not input the track name there, right? It is supposed to interact with currently playing ME?
Sadly, either i am doing something wrong, or it's not working. I tried both lines, both as a script call and plugin command. It is supposed to put on hold dialogue, so it would happen after the track ends. But in either case dialogue appears instantly, during the music instead of coming after.

Plugin is added and switched to ON, there are no variables or anything else to do with it, just the switch.

I should not input the track name there, right? It is supposed to interact with currently playing ME?
Please show me what you are doing with a screenshot and I'll be able to help better. It's tricky since we are adding in stuff and scripting really doesn't allow much room for errors.

There are no plugin commands so you will not need anything there for sure. Just save that script above as a plugin and loading it will be sufficient (we are overwriting a function in the corescripts only).

Screenshots of what you put in the plugin and how you are doing that can help too.

I'll need to see how you are playing the track and how the waitMode command is implemented.

Currently, I have no issues here in terms of the character waiting until the ME is completely finished. You will have to implement this accordingly to your workflow, of course.

Also Edit: Using ShadowDragon's suggestion for SRD's Wait Options would work as well since it is plug and play. You will need to make sure you follow through with what SRD has since I am not familiar with the flow of the plugin's commands/script calls/etc.

Edit:
Here is a video of what I have:


And here is a screenshot of the event:
s1.png
or try SRD_WaitOptions which could do it for you, and you can have the wait
frames to seconds, or minutes, that bypasses 999 frames 🙂
Please show me what you are doing with a screenshot and I'll be able to help better.
I will not show the entire event, since it will be like few dozens of screenshots, just the beginning and end. But it stays the same - character movements, location changes, nothing special.
ex1.jpg
ex2.jpg

or try SRD_WaitOptions which could do it for you, and you can have the wait
frames to seconds, or minutes, that bypasses 999 frames
Thank you, will try to see if that works. But i can't see the command list, or it's inside the plugin?

Update: yes, plugin works, and the commands are as simple as writing "Wait for ME" in plugin commands! Thanks a lot. I think that other types of waiting will come in hadny as well.
I will not show the entire event, since it will be like few dozens of screenshots, just the beginning and end. But it stays the same - character movements, location changes, nothing special.
View attachment 301611View attachment 301612

Thank you, will try to see if that works. But i can't see the command list, or it's inside the plugin?

Update: yes, plugin works, and the commands are as simple as writing "Wait for ME" in plugin commands! Thanks a lot. I think that other types of waiting will come in hadny as well.
Edit: I found the ME at the top of the event.

It's a bit hard to understand the Cyrillic so I am guessing you are running some sort of bgm, tint/flash, and wait command before the waitMode script. Based on this flow, it will have to wait for everything else you put in until the setWaitMode script call so that part of it should be working.

Hoping SRD Wait Options work for you, too, if you are going that route.

Edit: it appears that the plugin is working for you so disregard this post. It is more of a quick fix than anything (and if you want to avoid the overhead of more plugins to deal with, you can just overwrite the code like the examples above I provided).

Original thread: https://forums.rpgmakerweb.com/threads/wait-for-completion-for-bgm.167637/