Why does plugin order matters?

Hi guys,

Just for educational purposes, why does plugin order matters in a project? If a plugin is essentially a library of functions / classes variables, where does the numbering comes from?
Linux OSs use 1000 of libraries which depend on each other. The OS knows dependencies and conflicts each library has. The package manager automatically handles these issues. Why can’t the plugin manager do that?
Simply out of curiosity🙂
The code is ran in order though. That is why the order matters, because if you have two functions that are the exact same (like say two functions for starting a battle), then the code will use the last one it sees as the valid function, and ignore the other.
@bgillisp
Thanks! That’s good to know.
Lesson learned. So I should make sure that any functions I make have unique names for maximum compatibility? 😉
It has to do with how Javascript works.
Linux is a compiled language, while Javascript is an interpreted language. Meaning, it doesn't have any information regarding plugin dependencies etc. as it doesn't have a compiler to generate this information.
Btw. I think plugin dependencies in linux are more or less programmed, but I can't say for sure, since I don't know Linux source code.

The interpreter takes files one at a time and reads them for functions to define them and save them in memory. And as @bgillisp said, it results in two functions that are the same being overwritten. This issue is being solved through aliasing, but still, if you alias one function twice, the plugin ordering matters in terms of which one of those is ran first and which one is ran second. This can cause compatibility issues between plugins, if the ordering is wrong or the functions use one thing for two completely different outcomes. For example I've seen people make Yanfly's and Victor's battle plugins work together, but only when they placed all VE plugins under YEP plugins. I've never got them to run together though, but it was maybe because I had VE core plugin only under YEP core engine and not under all YEP plugins.
Which brings us to another thing - some plugins expect certain variables to be predefined. For example YEP_AnimatedSVEnemies require something from YEP_BattleEngineCore. Again, because the two plugins are on two separate files. So if you put the plugin (animatedSVEnemies) to the top, the requested variables will not be predefined and it will not function correctly.
It wouldn't be hard to add metadata to plugins to let the plugin manager know that it should postpone loading plugin X until plugins Y and Z were loaded. Sadly, this might actually make it harder to debug plugin loading issues. Why? Here's a good example:
I've seen people make Yanfly's and Victor's battle plugins work together, but only when they placed all VE plugins under YEP plugins.
VE plugins don't depend on YEP. They don't need them to run. But since they both touch the same functionality, they interfere with each other in non-intuitive ways. Now what happens if you put your YEP plugins all the way on the top of the plugin list, but they depend on a plugin at the bottom? The manager will delay loading those plugins until it has loaded all the dependencies, and now they will load after VE, even though they appear first on the list. Can you add a dependency notation to force YEP to load after VE? Yes, you can, but what if you decide not to use VE after all? Do you have to go back and remove that notation?

I guess you can see how this gets very complicated, very fast. It doesn't mean it can't be done, but the entire architecture has to be built from the ground up with extensibility in mind, and plugins have to adhere to very specific guidelines.
@Poryg @sciencewarrior
Thank you both for good explanations with examples 🙂
I guess Linux libraries are very different to JS plugins...
Yup, Linux libs have metadata with dependencies, version requirements, conflicts, etc. Everything is in a file which is part of package, together with an actual library. Different package managers and packages are not compatible with each other. And sometimes you can get into dependency loops if you get unlucky 😛
I see. I was thinking about plugin metadata, now I understand why it can complicate stuff. Everyone would have to follow certain protocol for metafiles. Not really worth it...
So I should make sure that any functions I make have unique names for maximum compatibility? 😉
Yes, I'd say WordPress conventions are a good example of how to keep things manageable even in an ecosystem of thousands of disparate plugins. Adding a developer-specific sufix to every function or using classes to create namespaces is a good idea.

I see. I was thinking about plugin metadata, now I understand why it can complicate stuff. Everyone would have to follow certain protocol for metafiles. Not really worth it...
Again, WordPress is a good example of how this can be done. First you need a ton of hooks in your engine to extend it without breaking other stuff, but this obviously has development and performance costs. Then you need plugins to only use the hooks provided and not overextend their boundaries, putting limits on what can actually be done by plugins. In the end, it's a matter of trade-offs.

Original thread: https://forums.rpgmakerweb.com/threads/why-does-plugin-order-matters.85931/