MZ Subroutines within script

So basically, I have a huge amount of if/else processing to do, and I'm referencing arrays of objects so I have no choice but to use scripts to do so. The processing could be broken into several broad categories, and I'd rather make a separate common event to handle each broad category instead of making a single, massive, confusing (for me) if/else block handling each and every condition and sub-condition.

But unfortunately, I'm not seeing a way to call a common event through script that works the same way as when you use RPG Maker's (MZ) built-in command. I need the call to act like a subroutine, processing within the first script, then returning to that script and letting it continue from the jumping-off point.

As an alternative, I tried using the script for "jump to label." I checked the script call database, but I'm not understanding how to "set" the label (for MZ), only to jump to it. And I can't get it to jump to a label defined outside of the script. The instructions aren't as clear as they are for MV, or more likely my newb-brain just isn't getting it yet.

Anyway , I'd be fine if anyone could offer any solution that would work the way I've described. Thanks in advance.
This is the function the editor runs when calling a common event:
Game_Interpreter.prototype.command117 = function(params) {
const commonEvent = $dataCommonEvents[params[0]];
if (commonEvent) {
const eventId = this.isOnCurrentMap() ? this._eventId : 0;
this.setupChild(commonEvent.list, eventId);
}
return true;
};
rmmz_objects.js:10071 (on 1.0.2)

It picks the common event from the list, then makes sure it's not empty, and if the player is not transitioning maps for example, and then it adds it to the list of executing events in the map. You basically need to make sure to run it under the same conditions for it to work the same.

Then, for the other question, the labeling function is part of the event itself, so we only get a "true" value from running it as a script. There seems to be some kind of per event page array of labels, each defined as
{
"code": 118,
"indent": 0,
"parameters": [
"This is a label"
]
}

So you'd have to dynamically edit your $dataMap and reload your $gameMap every single time you add one. That, or simply sort and group the code in functions and run them in proper order.

Oh I just saw your post after I posted my own. lol
What a coincidence.

I'm using mv, but I guess they are more or less the same.
It seems (without seeing samples of your work) that what you're doing might benefit from outsourcing some of the script calls (or even events) to functions in a plugin file.

That way, you can call whatever you need and continue with the next event command.

Alternatively, you can de-modularize and instead of calling other events, just paste their commands into the appropriate places in your first event.
Thanks all for your responses!

Yes, it seems function calls to a plugin would be the way to go. Unfortunately, it's been 20+ years since I had a programming class, and in my few weeks with RPG Maker I'm just now learning how to import and use plugins without breaking things lol, much less creating them myself.

But it'd be worth it to learn, and I'm sure the process is easier than I make it out to be. I learned Java (not script) back in the day, and I remember it being fairly easy to define a class and functions. Just gotta learn the syntax for Javascript.

In the meantime, my current massive if/else block-in-progress isn't quite as bad to work with as I'd thought, so long as I go out of my way to format it so even a dunce like me can follow along lol. Anyway, appreciate the input!

If I could ask one more quick question... I'm not too sure how plugins interact in RPG Maker. As in, if I import a plugin, then write a script referencing it... does my script just recognize the plugin is 'there'? Or do I need to include a header or some such to import it directly into the script?
There's general concensus on a global variable, "Imported" to set if a plugin has been already read or not by the editor. It's used for plugin extensions.

var Imported = Imported || {};
Imported.myPlugin = true;

There are many other ways to find it, but it's the easiest to do it that way.

If there are more global variables set or modified in the first plugin, you can use them after checking they are there.
For example...
if (Imported.myPlugin)
{
//Do stuff
}

or
if ($gameParty.parameterAddedOnAPluginSetAboveThis)
{
//Do stuff
}
I'm not too sure how plugins interact in RPG Maker. As in, if I import a plugin, then write a script referencing it... does my script just recognize the plugin is 'there'? Or do I need to include a header or some such to import it directly into the script?
I think this question is different from what @myenemy answered.

The answer is that no, you don't need to do anything. All js files included in the project can use code from each other; you can just call functions and reference objects from the core files within your plugin. And vice versa between two plugins.

There's no #include or anything like that.

The answer above is giving a way to check whether a specific other plugin has been installed, but it's not necessary to reference its code.
Ok, that helps. Thanks to all who answered!

Original thread: https://forums.rpgmakerweb.com/threads/subroutines-within-script.169828/