MZ Running code in parallel at all times

Is there a way to run code in parallel at all times while making a plugin? so far I've just added my code to a plugin command and written in the instructions that a parallel common event needs to be made that runs this plugin command, but it's not as graceful of a solution as I would have liked, I'd prefer for this to be done automatically so the user doesn't need to set up a common event for this purpose.
nothing in javascript is ever truly parallel.

what there is is a main game loop that contains all the objects that are processed once per frame one after the other, and that includes the objects for parallel events, for move routes and so on.

you can add your own objects to that main game loop, but if you do it in the wrong way your game will lag like hell.
I suggest you give a better description of what that plugin does and how it works, and then wait for someone with more experience to tell you which method to use to integrate it into the engine.
Is there a way to run code in parallel at all times while making a plugin? so far I've just added my code to a plugin command and written in the instructions that a parallel common event needs to be made that runs this plugin command, but it's not as graceful of a solution as I would have liked, I'd prefer for this to be done automatically so the user doesn't need to set up a common event for this purpose.
If you're wanting to create a plugin that constantly checks for something in the background, I'd take a look at Casper Gaming's achievements plugin for inspiration.

That plugin includes tracking of achievement conditions automatically, so maybe you can glean something from that?
If you're wanting to create a plugin that constantly checks for something in the background, I'd take a look at Casper Gaming's achievements plugin for inspiration.

That plugin includes tracking of achievement conditions automatically, so maybe you can glean something from that?
I checked out how it was done in that plugin and that is exactly what I needed, thank you! I'll be sure to use this code but one question: would I need to credit casper gaming for referencing this code? I have nothing against it, just want to make sure I don't do it wrong.

nothing in javascript is ever truly parallel.

what there is is a main game loop that contains all the objects that are processed once per frame one after the other, and that includes the objects for parallel events, for move routes and so on.

you can add your own objects to that main game loop, but if you do it in the wrong way your game will lag like hell.
I suggest you give a better description of what that plugin does and how it works, and then wait for someone with more experience to tell you which method to use to integrate it into the engine.
it was for my day and night cycle plugin, but I wanted to know as I'll probably be using that kind of code a lot, I tried implementing a custom scene but it was a bit too difficult for me, so I'll hold off on that. I've only made 1 plugin so I didn't expect to understand scenes anyway, and all of the ideas for things I want to do is just making my workflow easier so I don't have to remake common event systems in many projects, so instead I can just create a plugin and use that one.
What was the solution?

I am also using in-game's event running in parallel in order to give me "ticks" every frame.

I frankly thought, that was it and as good as it gets.

Or does Casper hook into the mechanism for the parallel events and adds his code onto the parallel event chain to be called, just as any other parallel event does?

I checked out how it was done in that plugin and that is exactly what I needed, thank you! I'll be sure to use this code but one question: would I need to credit casper gaming for referencing this code? I have nothing against it, just want to make sure I don't do it wrong.


it was for my day and night cycle plugin, but I wanted to know as I'll probably be using that kind of code a lot, I tried implementing a custom scene but it was a bit too difficult for me, so I'll hold off on that. I've only made 1 plugin so I didn't expect to understand scenes anyway, and all of the ideas for things I want to do is just making my workflow easier so I don't have to remake common event systems in many projects, so instead I can just create a plugin and use that one.
would I need to credit casper gaming for referencing this code?
If you use it directly copied instead of creating your own based on a similar technique, then yes, absolutely. Consider your plugin an edit of his... I created an in-game "challenge" tracker plugin based on the same code, and I made sure to put him in the credits as follows:

Casper Gaming (https://www.caspergaming.com/)
You can find his full terms here:
If you're wanting to create a plugin that constantly checks for something in the background, I'd take a look at Casper Gaming's achievements plugin for inspiration.
That plugin includes tracking of achievement conditions automatically, so maybe you can glean something from that?

I looked at the plugin, thank you for suggestion!

Is the following the code that creates the stream of update ticks? (Starts at line #2041) Thank you.

JavaScript:
//=============================================================================
// Scene_Map
//-----------------------------------------------------------------------------
// Automatic tracking for playtime (Using Scene Map so playtime achievements do
// not update in battle or mid-scene
//=============================================================================
//-----------------------------------------------------------------------------
// Alias: Check achievements that have playtime criteria
//-----------------------------------------------------------------------------
const alias_CGMZ_Achievements_SceneMap_update = Scene_Map.prototype.update;
Scene_Map.prototype.update = function() {
    alias_CGMZ_Achievements_SceneMap_update.call(this);
    if(Graphics.frameCount % 60 == 0) $cgmz.checkAchievementPlaytimeCriteria();
};
If you're wanting to create a plugin that constantly checks for something in the background, I'd take a look at Casper Gaming's achievements plugin for inspiration.


I looked at the plugin, thank you for suggestion!

Is the following the code that creates the stream of update ticks? (Starts at line #2041) Thank you.

JavaScript:
//=============================================================================
// Scene_Map
//-----------------------------------------------------------------------------
// Automatic tracking for playtime (Using Scene Map so playtime achievements do
// not update in battle or mid-scene
//=============================================================================
//-----------------------------------------------------------------------------
// Alias: Check achievements that have playtime criteria
//-----------------------------------------------------------------------------
const alias_CGMZ_Achievements_SceneMap_update = Scene_Map.prototype.update;
Scene_Map.prototype.update = function() {
    alias_CGMZ_Achievements_SceneMap_update.call(this);
    if(Graphics.frameCount % 60 == 0) $cgmz.checkAchievementPlaytimeCriteria();
};
I would ask @casper667 just to be sure.
Hi all, seeing as I was pinged here, I will try my best to answer questions.

First, you only really need to credit me if you are directly copying some code of mine, but you can always credit me anyways if you used my plugins for inspiration or whatever else 😍 To credit me just list "Casper Gaming (https://www.caspergaming.com/)" in your credits somewhere.

Second, the achievements plugin is not what I would think of as my best work, it's quite messy 😅

Third, the main game loop updates once per frame, or 60 times a second assuming 60fps. Each class that needs to be updated automatically (most of them) also updates once per frame since the main game loop calls their "update" function in one way or another. However, updating so frequently can cause lag especially if you are trying to do expensive operations in the update. Plus, the more code running every 60th of a second, the more computing resources will be required to run at a smooth 60fps. To see this yourself, just stick a few console.log statements in an update function.

While it would probably be fine to run that function to check achievement playtime criteria every frame, it's not needed. Not all code needs to run every 60th of a second. By dividing the framecount (what determines playtime in rpg maker) by 60 and checking if the remainder is equal to 0, the achievements plugin only runs the checkAchievementPlaytimeCriteria function once every 60 frames aka once per second. Players won't notice that their achievement popped a fraction of a second later than it should have, and it helps reduce lag however slightly.

In other instances, there are better ways of knowing if the achievement plugin should check criteria. For example, it doesn't check the switch criteria every frame or even every 60 frames, instead it only checks when a switch is changed. Basically, you want to run your code as infrequently as possible while still achieving the desired effect.

There are also other ways you can optimize your code for performance. The achievement plugin for example checks playtime criteria first, and only then when the playtime criteria is satisfied will it check if all of its other criteria is also satisfied. For further optimization, you could look into async functions which can run asynchronously and not cause everything else to wait for them to complete: https://developer.mozilla.org/en-US/docs/Learn/JavaScript/Asynchronous/Introducing

See also time and space complexity analysis: https://www.geeksforgeeks.org/time-complexity-and-space-complexity/
If you're wanting to create a plugin that constantly checks for something in the background, I'd take a look at Casper Gaming's achievements plugin for inspiration.


I looked at the plugin, thank you for suggestion!

Is the following the code that creates the stream of update ticks? (Starts at line #2041) Thank you.

JavaScript:
//=============================================================================
// Scene_Map
//-----------------------------------------------------------------------------
// Automatic tracking for playtime (Using Scene Map so playtime achievements do
// not update in battle or mid-scene
//=============================================================================
//-----------------------------------------------------------------------------
// Alias: Check achievements that have playtime criteria
//-----------------------------------------------------------------------------
const alias_CGMZ_Achievements_SceneMap_update = Scene_Map.prototype.update;
Scene_Map.prototype.update = function() {
    alias_CGMZ_Achievements_SceneMap_update.call(this);
    if(Graphics.frameCount % 60 == 0) $cgmz.checkAchievementPlaytimeCriteria();
};

So, indeed, I tested this thing above and it is the solution.

Here, try for yourself - start up a project (Crtl-R), go into DevTools (F8), and copy-paste two statements:

JavaScript:
const alias_CGMZ_Achievements_SceneMap_update = Scene_Map.prototype.update;

Second statement, this will keep printing "Update" once a second, although the statement will be executed every frame:

JavaScript:
Scene_Map.prototype.update = function() {
    alias_CGMZ_Achievements_SceneMap_update.call(this);
    if(Graphics.frameCount % 60 == 0) console.log('Update');
};

The statement keeps on printing, even if you change the locations/maps.

I trust this closes the case, thanks to @C64_Mat and @casper667
Hi all, seeing as I was pinged here, I will try my best to answer questions.

First, you only really need to credit me if you are directly copying some code of mine, but you can always credit me anyways if you used my plugins for inspiration or whatever else 😍 To credit me just list "Casper Gaming (https://www.caspergaming.com/)" in your credits somewhere.

Second, the achievements plugin is not what I would think of as my best work, it's quite messy 😅

Third, the main game loop updates once per frame, or 60 times a second assuming 60fps. Each class that needs to be updated automatically (most of them) also updates once per frame since the main game loop calls their "update" function in one way or another. However, updating so frequently can cause lag especially if you are trying to do expensive operations in the update. Plus, the more code running every 60th of a second, the more computing resources will be required to run at a smooth 60fps. To see this yourself, just stick a few console.log statements in an update function.

While it would probably be fine to run that function to check achievement playtime criteria every frame, it's not needed. Not all code needs to run every 60th of a second. By dividing the framecount (what determines playtime in rpg maker) by 60 and checking if the remainder is equal to 0, the achievements plugin only runs the checkAchievementPlaytimeCriteria function once every 60 frames aka once per second. Players won't notice that their achievement popped a fraction of a second later than it should have, and it helps reduce lag however slightly.

In other instances, there are better ways of knowing if the achievement plugin should check criteria. For example, it doesn't check the switch criteria every frame or even every 60 frames, instead it only checks when a switch is changed. Basically, you want to run your code as infrequently as possible while still achieving the desired effect.

There are also other ways you can optimize your code for performance. The achievement plugin for example checks playtime criteria first, and only then when the playtime criteria is satisfied will it check if all of its other criteria is also satisfied. For further optimization, you could look into async functions which can run asynchronously and not cause everything else to wait for them to complete: https://developer.mozilla.org/en-US/docs/Learn/JavaScript/Asynchronous/Introducing

See also time and space complexity analysis: https://www.geeksforgeeks.org/time-complexity-and-space-complexity/
I need to ask, would it be fine with making a comment in the code where I used the 5 lines to update every second detailing that it was borrowed from your plugin, that way the people using my plugin don't have to credit you, I'll also write in my plugin help section that you have helped out with it and that the users can credit you if they want, or should this be required?
Adding a new line of code to be executed in the main game loop is not a technique you should start crediting someone when you use it. It is pretty much common practice.

But you can always put the name of whoever told you the technique as a token of gratitude in your plugin description.
So, indeed, I tested this thing above and it is the solution.

Here, try for yourself - start up a project (Crtl-R), go into DevTools (F8), and copy-paste two statements:

JavaScript:
const alias_CGMZ_Achievements_SceneMap_update = Scene_Map.prototype.update;

Second statement, this will keep printing "Update" once a second, although the statement will be executed every frame:

JavaScript:
Scene_Map.prototype.update = function() {
    alias_CGMZ_Achievements_SceneMap_update.call(this);
    if(Graphics.frameCount % 60 == 0) console.log('Update');
};

The statement keeps on printing, even if you change the locations/maps.

I trust this closes the case, thanks to @C64_Mat and @casper667
You're very welcome!

Original thread: https://forums.rpgmakerweb.com/threads/running-code-in-parallel-at-all-times.146581/