MZ Run function when another function runs? (SOLVED)

So let's say that Scene_Base.prototype.update() runs. Can I make it so my function runs at the same time somehow?

Currently I have a function that rewrite whatever core functions I want to modify. And I don't know if that's a good way to do it. But I also feel like there is value in knowing if this option exists.

Best regards!
You can save a reference to the old function in a variable and use that to extend it, like this:
JavaScript:
let SceneBase_update = Scene_Base.prototype.update;
Scene_Base.prototype.update = function() {
    // do something before
    SceneBase_update.call(this);
    // do something after
};
If the function takes parameter you can pass these as additional arguments to call. Alternatively, you can make use of the arguments object to get a list of all parameters and use them like this:
JavaScript:
original_function.call(this, ...arguments);
original_function.apply(this, arguments);
Both of these work, but apply takes an array-like object containing the parameters, while call takes them individually. That's why we use the spread operator here.
running at the same time would be parallel processing on a hardware level.
usually parallel processing is only simulated on a software level for a lot of reasons, including that it is much too difficult for even most professional programmers to handle.

depending on what you want to do, you can only choose where and when to implement your function.
One option is described above, another would be to create a new object in the main game loop.
You can save a reference to the old function in a variable and use that to extend it, like this:
JavaScript:
let SceneBase_update = Scene_Base.prototype.update;
Scene_Base.prototype.update = function() {
    // do something before
    SceneBase_update.call(this);
    // do something after
};
If the function takes parameter you can pass these as additional arguments to call. Alternatively, you can make use of the arguments object to get a list of all parameters and use them like this:
JavaScript:
original_function.call(this, ...arguments);
original_function.apply(this, arguments);
Both of these work, but apply takes an array-like object containing the parameters, while call takes them individually. That's why we use the spread operator here.
Thanks mate, similar to what I do at them moment, just not storing the original function at the moment. Thanks for the second solution as well, I'll look into that. Still unclear on how "apply" and "call" works, haven't really focused on it.

Thanks.

This thread is being closed, due to being solved. If for some reason you would like this thread re-opened, please report this post and leave a message why. Thank you.

Original thread: https://forums.rpgmakerweb.com/threads/run-function-when-another-function-runs-solved.138797/