Multi Timers

Awesome! In the next version, I will add an option to reset the timer, so you will just be able to do it as you originally did, with 'reset', and it will add 1 to the variable every second (or whatever your delay is).

That would be good. What would be really cool would be a way to speed up or slow down a timer. I was thinking of doing a "rest for x hours" event, but that seems like a headache, so I'll just do rest until night or morning.
Hey Shaz! Any chance this could be ported to MZ? Would be a huge help for me to use in conjunction with the Encounter Effects plugin. All my enemies are on the map and I've love to have them respawn after a given time.
I thought someone had already done a multi timer plugin for MZ?
Do the timers persist after loading a game?

for example, I have 4 crops (4 timers) and they need 2 hours to grow up, and I save the game when there were 30 minutes remaining, if I quit the game and load the game later, Will the 4 timers continue at 30 minutes remaining?
Do the timers persist after loading a game?
They sure do.
Hey Shaz! Any chance this could be ported to MZ? Would be a huge help for me to use in conjunction with the Encounter Effects plugin. All my enemies are on the map and I've love to have them respawn after a given time.
Hi, is this plugin available for MZ or something equivalent. It would be extremely useful to me also.
Is there a way to set the time remaining/passed into a Variable? So like if I set a timer for 100 seconds and 20 passed, I can set V1 to 20 or 80.
Hi, is this plugin available for MZ or something equivalent. It would be extremely useful to me also.
Dunno if you're still looking or not, but this might help.

Is there a way to set the time remaining/passed into a Variable? So like if I set a timer for 100 seconds and 20 passed, I can set V1 to 20 or 80.
Unless I'm misunderstanding your question, I'm pretty sure that's what this plugin command does:
Code:
TimeLeft key variableId
Unless I'm misunderstanding your question, I'm pretty sure that's what this plugin command does:
Code:
TimeLeft key variableId

Well I'll be, that is exactly what I was asking. Thanks! :LZSjoy:

Edit: So it is, but it isn't. I'm trying to set a Self Variable ( http://www.yanfly.moe/wiki/Self_Switches_&_Variables_(YEP) ) to the remaining time. I thought I saw it on the list but doesn't seem so. Unless I'm missing it again. Any idea on how to do that?
Well I'll be, that is exactly what I was asking. Thanks! :LZSjoy:

Edit: So it is, but it isn't. I'm trying to set a Self Variable ( http://www.yanfly.moe/wiki/Self_Switches_&_Variables_(YEP) ) to the remaining time. I thought I saw it on the list but doesn't seem so. Unless I'm missing it again. Any idea on how to do that?

Ah, yeah, a self variable would be a bit different. I didn't have time to test it, since I'm on my way to bed, but give this a try. Save this into a new .js file and add it to your project through the plugin manager:
JavaScript:
/*:
 * @plugindesc Shaz Multi_Timers + YEP_SelfSwVar compatibility patch
 * @author Arthran
 *
 * @help
 * Allows you to save the time left in a timer into a self variable,
 * using this plugin command:
 *   TimeLeftSV key variableId
 *
 * Free for use in any kind of project. No credit necessary
 */

(() => {
    const _Game_Interpreter_pluginCommand = Game_Interpreter.prototype.pluginCommand;
    Game_Interpreter.prototype.pluginCommand = function(command, args) {
        if (command.toUpperCase() == 'TIMELEFTSV') {
            args = this.subst(args);
            let key = args.shift();
            let varId = parseInt(args.shift());
            let timeLeft = $gameTimer.hasTimer(key);
            $gameTemp.setSelfSwVarEvent(this._mapId, this._eventId);
            $gameVariables.setValue(varId, timeLeft ? timeLeft : 0);
            $gameTemp.clearSelfSwVarEvent();
        } else {
            _Game_Interpreter_pluginCommand.call(this, command, args);
        }
    };
})();

And try this plugin command:
Code:
TimeLeftSV key variableId
Ah, yeah, a self variable would be a bit different. I didn't have time to test it, since I'm on my way to bed, but give this a try. Save this into a new .js file and add it to your project through the plugin manager:
JavaScript:
/*:
 * @plugindesc Shaz Multi_Timers + YEP_SelfSwVar compatibility patch
 * @author Arthran
 *
 * @help
 * Allows you to save the time left in a timer into a self variable,
 * using this plugin command:
 *   TimeLeftSV key variableId
 *
 * Free for use in any kind of project. No credit necessary
 */

(() => {
    const _Game_Interpreter_pluginCommand = Game_Interpreter.prototype.pluginCommand;
    Game_Interpreter.prototype.pluginCommand = function(command, args) {
        if (command.toUpperCase() == 'TIMELEFTSV') {
            args = this.subst(args);
            let key = args.shift();
            let varId = parseInt(args.shift());
            let timeLeft = $gameTimer.hasTimer(key);
            $gameTemp.setSelfSwVarEvent(this._mapId, this._eventId);
            $gameVariables.setValue(varId, timeLeft ? timeLeft : 0);
            $gameTemp.clearSelfSwVarEvent();
        } else {
            _Game_Interpreter_pluginCommand.call(this, command, args);
        }
    };
})();

And try this plugin command:
Code:
TimeLeftSV key variableId

Thanks! I tested and it appears to be working 🙂. Even did it on multiple Events and they each showed their own number.
This is a very cool plugin. The only thing that seems to be missing is that you cannot use a variable to define the number of seconds the timer will run for.

e.g.
addTimer randomMonster \v[170] \sets(110, true) instead of

addTimer randomMonster 60 \sets(110, true)

Would it be hard to add this or is it already there and I'm doing it wrong.
This is a very cool plugin. The only thing that seems to be missing is that you cannot use a variable to define the number of seconds the timer will run for.

e.g.
addTimer randomMonster \v[170] \sets(110, true) instead of

addTimer randomMonster 60 \sets(110, true)

Would it be hard to add this or is it already there and I'm doing it wrong.
If you have the Yanfly Plugins, "Plugin Commands - Switches & Variables Access (YEP)" will do exactly that.
I have been using this plugin for a while and its a lifesaver :3

Was just wondering if there's a way to individually pause timers?
For example when there's dialogue going on, I wanted to pause certain things, and then resume after the dialogue is over.

I have tried using $gameTimer.pause(); and $gameTimer.resume(); and they work fine but those affect ALL timers in the game. 😢
I'm trying to delete all timers running on a map before I leave a map.

I set up a quick event that just runs this Plugin Command:

Code:
deleteTimer <thismap>

But I don't think I'm using this command correctly because it's not deleting the timer. Can someone show me what I'm doing wrong?
I'm trying to delete all timers running on a map before I leave a map.

I set up a quick event that just runs this Plugin Command:

Code:
deleteTimer <thismap>

But I don't think I'm using this command correctly because it's not deleting the timer.

Yeah, that doesn't look right. From the documentation:
DeleteTimer key
...
key is any text/number string without spaces, that uniquely identifies the timer
bolded emphasis mine.

So those commands can never support you deleting multiple timers at once, because each key had to be unique. What you typed would delete a timer you created with AddTimer <thismap> but no others.

Shaz would certainly know better, but with a quick poke at the code it doesn't look the timers have any reference to being "owned" by or "contained" in a map - you can just use the map's ID as a part of your unique key.

So there would be no built-in way to just iterate through all of the timers contained by a map, you have to just delete them individually.

My two cents.
So there would be no built-in way to just iterate through all of the timers contained by a map, you have to just delete them individually
Would it be possible to add another line at their creation to add the key to an array and then at transfer do "For Each in [Array] do DeleteTimer [key]" and then clear it? I'm sure that's not the correct command but hopefully you know what I'm getting at 🙂
Yeah, that doesn't look right. From the documentation:

bolded emphasis mine.

So those commands can never support you deleting multiple timers at once, because each key had to be unique. What you typed would delete a timer you created with AddTimer <thismap> but no others.

Shaz would certainly know better, but with a quick poke at the code it doesn't look the timers have any reference to being "owned" by or "contained" in a map - you can just use the map's ID as a part of your unique key.

So there would be no built-in way to just iterate through all of the timers contained by a map, you have to just delete them individually.

My two cents.
That makes sense, especially since I am using the plugin command:

Code:
AddTimer <thismap>.<thisevent> 20 \setSS([<thismap>,<thisevent>,'B'],true)

I did try:

Code:
deleteTimer <thismap>.<thisevent>

just to see what would happen, but nothing did - which was expected.

The contributing factor I'm probably also running up against is that I'm using the timer in conjunction with Galv's Event Spawner to create random nodes on the map that gives the player an item when clicked, but disappears when the timer runs out.

And it does work perfectly - I can spawn and despawn as many events as I want all on different timers. And I can also despawn all the events on the map when I leave the map with Galv's plugin, but I just can't get the timers to go away. So even though the event technically despawned, the timer is still there running in the background and then when the timer runs out (in my case after 20) it runs the event on self switch 'B' anyway (which is a quick animation and a variable reset.

And since I'm duplicating the same event multiple times (right now I have about 15 per map) I haven't been able to figure out how to give each event a unique timer key that I can directly call and turn off since they're all technically the same event.
Would it be possible to add another line at their creation to add the key to an array and then at transfer do "For Each in [Array] do DeleteTimer [key]" and then clear it?
Yes, but that would be a request to Shaz to edit the plugin for that functionality.

One could do it manually, but then you'd be doing the whole thing through JavaScript and bypassing the functionality of the keywords provided by the plugin commands.
That makes sense, especially since I am using the plugin command:

Code:
AddTimer <thismap>.<thisevent> 20 \setSS([<thismap>,<thisevent>,'B'],true)

I did try:

Code:
deleteTimer <thismap>.<thisevent>

just to see what would happen, but nothing did - which was expected.

The contributing factor I'm probably also running up against is that I'm using the timer in conjunction with Galv's Event Spawner to create random nodes on the map that gives the player an item when clicked, but disappears when the timer runs out.

And it does work perfectly - I can spawn and despawn as many events as I want all on different timers. And I can also despawn all the events on the map when I leave the map with Galv's plugin, but I just can't get the timers to go away. So even though the event technically despawned, the timer is still there running in the background and then when the timer runs out (in my case after 20) it runs the event on self switch 'B' anyway (which is a quick animation and a variable reset.

And since I'm duplicating the same event multiple times (right now I have about 15 per map) I haven't been able to figure out how to give each event a unique timer key that I can directly call and turn off since they're all technically the same event.
Provided that you are adding all of your timers with your key in that exact format, you can delete all timers from a the current map using this script call:

JavaScript:
const mapId = this._mapId;
const regex = new RegExp(`${mapId}\\.\\d+`);
Object.keys($gameTimer._timers).forEach(key => {
    if (regex.test(key)) {
        $gameTimer.deleteTimer(key);
    }
});

That is assuming that you are running the script call from the same map that is in question.

Original thread: https://forums.rpgmakerweb.com/threads/multi-timers.50474/