MV How do I make a plugin run without plugin command?

I am making a plugin (for MV and MZ), and right now everything works fine! But to make it run, the user needs to run a plugin command. However, this plugin should run multiple times per second to get the best result.

1710248859247.png

I tried running a parallel event to run the plugin every 3 frames, and this works just fine with no lag what so ever. Yet I would love to get that common event out of the routine.

So is there a way that everything inside the plugin command would run in a loop in the background instead?
depends how the code looks like and what code it needs to run.

code that needs to run constantly can be done in gameSystem or in
the Interpreter which I think are constant running or use the code
as scriptcall.

I dont know what your code is supposed to do.
So is there a way that everything inside the plugin command would run in a loop in the background instead?
Sure, the simplest way is usually to alias the scene's update method and add your plugin's logic there.

Though it also depends on what it does. If it's just waiting for something specific to happen, then it's better to alias the code that makes that thing happen rather than keep checking constantly.
If you want it to still do its thing outside Scene_Map, setInterval or aliasing SceneManager.update itself might be needed.
Hmmm I hoped to not have to tell what I am working on, because I want it a surprise.

Without spoiling, it mainly does a bunch of math formulas. In the end it writes the results in a few variables.

Those variables need to be up-to-date at all times, that is why I want it looping in the background.
you can also do a "while loop"

so while (update) {
//..do stuff
} do ... {
//..whatever needs to be done to break it
}

but there are ways, but hard to tell what approach is best for you.
and where.

you can also make simple functions like:

mathCheck = function() {
// your code
}

and call it in an event, and loop the event through this.
depends what you feel most comfortable with.
I hoped to not have to tell what I am working on, because I want it a surprise.
Then let's give you a more general answer

To have anything happen without giving specific commands, you can select a function of the engine and link your code into it (usually by aliasing or replacement).
If you do this, the code will be executed whenever the engine runs that function.

The msin problem now is to decide which function to use - because that determines not only when the code is called but also in which context and with which data the code is run.

And that is why the question of what the code does is important here.
If you do not want to share that infor, then you have to look through the entire engine for yourself to find the function best used to integrate your plugin.
only if you share what you're doing someone esle might answer "yes, I remember doing something silmilar and the best function to use is xyz".

Now it's your choice, you can either keep the surprise and search for the best function yourself or reveal part of what you need to get advice on which function to use.
Those variables need to be up-to-date at all times
If this means every frame, then aliasing the map's update function would probably be best. You can do it like so:
JavaScript:
var _Scene_Map_update = Scene_Map.prototype.update;
Scene_Map.prototype.update = function () {
    _Scene_Map_update.apply(this);
    //Add your custom logic here
}
Or alias whenever is responsible for changing the input of those formulas if that's possible.
you can also do a "while loop"

so while (update) {
//..do stuff
} do ... {
//..whatever needs to be done to break it
}
okay, I will look what that does. 😛 I am a serious noob when it comes to javascript, so this is going to be a challenge 😛

but there are ways, but hard to tell what approach is best for you.
and where.

you can also make simple functions like:

mathCheck = function() {
// your code
}

and call it in an event, and loop the event through this.
depends what you feel most comfortable with.
Well, I do now call it in a looping common event, and that works just fine. But that is what I would like to avoid. Not to make it run better, just to make it easier on the user. 😛

Now, to use the plugin, the user needs to run that plugin command constantly. This means they need a (common) event running all the time. I would like to make it so that the same formula's run in the background the whole time, but without the use of that event.

Another thing I tried was to have it run with every step the player takes. This too works perfectly fine, but the variables won't be updated if the player doesn't move. So therefor the looping event has a better result.

---------

Then let's give you a more general answer

To have anything happen without giving specific commands, you can select a function of the engine and link your code into it (usually by aliasing or replacement).
If you do this, the code will be executed whenever the engine runs that function.

The msin problem now is to decide which function to use - because that determines not only when the code is called but also in which context and with which data the code is run.
Yes, this would be great, but like you said: Which function? I tried updating with each step, which I thought was going to lag, but it didnt. However, this won't update when the player is standing still. 😛

And that is why the question of what the code does is important here.
If you do not want to share that infor, then you have to look through the entire engine for yourself to find the function best used to integrate your plugin.
only if you share what you're doing someone esle might answer "yes, I remember doing something silmilar and the best function to use is xyz".
I told what it does, but without the reason why it is doing it.

Now it's your choice, you can either keep the surprise and search for the best function yourself or reveal part of what you need to get advice on which function to use.
you're probably right... This puts me kind-off in a tough spot. I think the reveal of the plugin will kick-start the popularity, like it did with the project this whole idea is based on. 😛

I want to announce the plugin during a stream, and on all platforms at the same time, and as shortly before release as possible.

I think I'll experiment a bit with the suggestions I got so far. If that won't fix it, I could still decide to give a quick preview. 😛 (I know, I am my own problem here... I am fully aware. Ha ha.)

-------------------------------------------------

If this means every frame, then aliasing the map's update function would probably be best. You can do it like so:
JavaScript:
var _Scene_Map_update = Scene_Map.prototype.update;
Scene_Map.prototype.update = function () {
    _Scene_Map_update.apply(this);
    //Add your custom logic here
}
Or alias whenever is responsible for changing the input of those formulas if that's possible.
oh, no every frame is not required. If it runs about a few times per second, should be enough. I tried up to 20 times per second, which surprisingly didn't lag at all. It actually makes the variables update super fast and smoothly, with absolute precision. It is overkill though, but... doesn't mean I'm not going to try this! Hahaha!

-------------------------------------------------

Guys, thank you for your quick replies. 👍:LZYsmile:👍 this will get me experimenting some more, to find the right methods.
Revo_XXX_Scene_Base_prototype_update = Scene_Base.prototype.update;
Scene_Base.prototype.update = function() {
//My Script Call
Revo_XXX_Scene_Base_prototype_update.call(this);
};

This should do what you want, if it lags the game maybe trying to put a Script call in the function that makes the plugin do what it does.
Hmm, I am interested in this too as I have a script that logs the player's coordinates then makes an event follow those coordinates. I should probably update it.
there are multiple ways, but I think @Mac15001900 has a point, you can alias
the update, than make a "let interval = 20;" and a for...loop to count the interval
for the variable you want to be updated if it's possible.

but we can only guess at the moment as there are many mathematic as well,
and I seen long once going way shorter with the same result.

for now, I look forward to the surprise you build and what it does in the end.
oh, no every frame is not required. If it runs about a few times per second, should be enough. I tried up to 20 times per second, which surprisingly didn't lag at all. It actually makes the variables update super fast and smoothly, with absolute precision. It is overkill though, but... doesn't mean I'm not going to try this! Hahaha!
If it's just a bit of math then the runtime might be negligible enough to not matter when you're doing it every frame. Though you could also make it only run if it hasn't done that in some time (use Date.now() to get the current time in milliseconds).

Hmm, I am interested in this too as I have a script that logs the player's coordinates then makes an event follow those coordinates. I should probably update it.
That sounds like it would be better done inside whichever function is responsible for player movement, since there's presumably no need for it when the player is stationary.
I am walking home right now and it is raining, so I will read all the replies more xarefully when I get home.

i did want to reply to this quickly, though:
That sounds like it would be better done inside whichever function is responsible for player movement, since there's presumably no need for it when the player is stationary.

Yes there is need for it when the player is stationary. I was able to bind it to steps, and that works, but then everything stops when the player stands still.

It is might be a good solution, if I cant get it to run all the time. That would be the ideal situation.
Yes there is need for it when the player is stationary. I was able to bind it to steps, and that works, but then everything stops when the player stands still.
That remark was addressing AquaEcho's post, yours does look it probably belongs in map's update.
If this means every frame, then aliasing the map's update function would probably be best. You can do it like so:
JavaScript:
var _Scene_Map_update = Scene_Map.prototype.update;
Scene_Map.prototype.update = function () {
    _Scene_Map_update.apply(this);
    //Add your custom logic here
}
Or alias whenever is responsible for changing the input of those formulas if that's possible.
Okay, so this works. No lag at all, as far as I can tell. 🤘😎🤘

there are multiple ways, but I think @Mac15001900 has a point, you can alias
the update, than make a "let interval = 20;" and a for...loop to count the interval
for the variable you want to be updated if it's possible.

but we can only guess at the moment as there are many mathematic as well,
and I seen long once going way shorter with the same result.

for now, I look forward to the surprise you build and what it does in the end.
This means it would only run every 20 frames? Great. How do I put that in mac's code? i just add that as a new line before "}"? 👍😀👍
not sure, but you need a for...loop, increase it, and it its 20, re-run the code.
I can give an example later, 2-3 hours probably.

here is an example, but there might be better ways though.
JavaScript:
var _Scene_Map_update = Scene_Map.prototype.update;
    Scene_Map.prototype.update = function () {
        _Scene_Map_update.apply(this);
            let int = 20;
            for (let val = 0; val < int; val++) {
                if (val > int) {
                    //you code here
                    val = 0; // repeat 0 to 20 before reset.
                }
            }
    }
there might be better ways, as like the while loop or val-- to decrease
the value which is also slightly faster than ++ but same result.

ES6 for advanced code are far cleaner and mostly a bit faster and sometimes
the older way is better, depends which you are comfortable with.
Well. I used macs solution for now, without the interval. This seems to work fine, so I guess Ill just leave it like that for now.
interval is when you want to check every x frames or seconds.
for every 5 seconds, you can do 60 * 5 or 300 frames.

otherwise, it checks every frames, see what suits your needs and go for that one.
as long the final results works the way you want it, that counts.
@ShadowDragon
Okay... I hope this will shine a little light on HOW I try to do things, even without spoiling WHAT I want to do. 😛

Do, as you can see, I have a large row with a lot of formula's. Each var is used in 2 or more formula's.
I don't need help with the formula's, so I left those out.

in the end, I have a few conditions, that might result in an action. Those actions are simply a "$gameVariables.setValue(x,y)" command.

JavaScript:
(function() {
    var [Name]     = [Value/Formula]
    var [Name]     = [Value/Formula]
    var [Name]     = [Value/Formula]
    var [Name]     = [Value/Formula]
    var [Name]     = [Value/Formula]
    var [Name]     = [Value/Formula]
    var [Name]     = [Value/Formula]
    var [Name]     = [Value/Formula]
    var [Name]     = [Value/Formula]
    var [Name]     = [Value/Formula]
    var [Name]     = [Value/Formula]
    var [Name]     = [Value/Formula]
      
    var _Scene_Map_update = Scene_Map.prototype.update;
    Scene_Map.prototype.update = function () {
        _Scene_Map_update.apply(this);
        //Add your custom logic here
        var [Name]     = [Value/Formula]
        var [Name]     = [Value/Formula]
        var [Name]     = [Value/Formula]
        var [Name]     = [Value/Formula]
        var [Name]     = [Value/Formula]
        var [Name]     = [Value/Formula]
        var [Name]     = [Value/Formula]
        var [Name]     = [Value/Formula]
        var [Name]     = [Value/Formula]
        var [Name]     = [Value/Formula]
        var [Name]     = [Value/Formula]
        var [Name]     = [Value/Formula]
        var [Name]     = [Value/Formula]
        var [Name]     = [Value/Formula]
        if(Condition) {Action}
        if(Condition) {Action}
        if(Condition) {Action}
        if(Condition) {Action}
        if(Condition) {Action}

        if(Condition) {Action}
        if(Condition) {Action}
        if(Condition) {Action}

        if (Condition) {
            if(Condition) {
                if(Condition) {
                    if(Condition) {
                        Action
                    } else if(Condition) {
                        Action
                    } else if(Condition) {
                        Action
                    } else if(Condition) {
                        Action
                    } else if(Condition) {
                        Action
                    } else if(Condition) {
                        Action
                    } else if(Condition) {
                        Action
                    } else if(Condition) {
                        Action
                    }
            } else {
                    if(Condition) {
                        Action
                    } else {
                        if(Condition) {
                            Action
                        } else if(Condition) {
                            Action
                        } else if(Condition) {
                            Action
                        } else if(Condition) {
                            Action
                        } else if(Condition) {
                            Action
                        } else if(Condition) {
                            Action
                        } else if(Condition) {
                            Action
                        } else if(Condition) {
                            Action
                        }
                    }
                }
            }
        } else {
            Action
        }
    }
})();

So, where would I put that interval?
Nevermind that last question... I think I got it!
good question, specially if a condition is met, will it continue or break
before it check the rest, but it need to be on each condition on the "else if"
and on the end, so there need to be a cleaner way to do it by than.

this going to be a bit more complicated than I though it will be.
as the way I placed wont work quite well.

I dont know the variables, but the structure is visible though.
maybe @Mac15001900 might have an idea, but I dont know if it can
be cleaner as well, depending on condition, and it could be easier and
shorter if its an array instead for those value, so you can use 1 or 2 variables,
and use the array for the values.

maybe the object to do it for you which could work as well.

Original thread: https://forums.rpgmakerweb.com/threads/how-do-i-make-a-plugin-run-without-plugin-command.166725/