Questions about plugin making

Hello, I'm trying to make some plugins plivately for my project and I have 2 questions
1. When I try to use $game, $data or any other command that starts with "$" in my js file it throws the "cannot read property ... of null". But if I put it inside a Game_Interpreter.prototype.pluginCommand it works perfect, so I assume I can only use "$" data inside of original rpg maker function. But how can I make it so I could use it in my js file in any place I want?
2. How do I use functions from one of my js file in another? For example: I created 1 js plugin with an array and 2nd with a script that summons a window on a button. How do I make it that way so I could summon a window from 2nd js that shows data from an array from 1st js?
I would imagine you would need to make $Gamedata a function in your plugin. Don't quote me on that, sense i'm not a scripter. I say, come through the game interpreter and other scripts and see how they do it. But don't edit anything unless your sure of what your doing. Like changing the crit multiplyer from *3. to *2 or *1.5
  1. main.js loads after the base code files and is responsible for launching the game proper:
    Code:
    PluginManager.setup($plugins);
    window.onload = function() {
        SceneManager.run(Scene_Boot);
    };
    I.e. the base code files are loaded, then the plugins are added, then the boot scene starts. The database is loaded during the boot scene: $dataActors etc aren't initialised until then, so attempting to reference them when your plugin loads will result in an error. Similarly, $gameActors etc are instanced on a per-playthrough basis.

    You can alias an appropriate method and insert your code like that, e.g.
    Spoiler: Example aliases (cf. rpg_managers.js)
    Code:
    var _DataManager_loadDatabase = DataManager.loadDatabase;   // Remember original routine
    DataManager.loadDatabase = function() {   // loadDatabase runs once on game boot
      _DataManager_loadDatabase.call(this);   // call original routine
      // your code here
    };
    
    var _DataManager_createGameObjects = DataManager.createGameObjects;   // Remember original routine
    DataManager.createGameObjects = function() {   // createGameObjects runs on new game
        _DataManager_createGameObjects.call(this);   // call original routine
        // your code here
    };

  2. All active plugins are loaded and merged into one page, so this is a question of Scope. A common approach is to set aside a global "namespace" variable for the plugin (often as a property of an author namespace to help avoid naming conflicts) and define the plugin's public methods on that. A quick example of this:
    Code:
    // global variables to organise plugin code
    var author = author || {};   // author namespace, i.e. everything by "author" (rename as appropriate)
    author.pluginName = author.pluginName || {};   // plugin namespace, i.e. everything from this plugin
    
    (function($) {   // IIFE (optional)
      $.cake = 'cake';   // define a string property
      $.myFunc = function(a) {   // define a function
        return a + 1;
      };
    })(author.pluginName);
    Both cake and myFunc are defined on author.pluginName, which is global, so they are also global. E.g. you can use these expressions in script calls or in other plugins:
    Code:
    author.pluginName.cake
    author.pluginName.myFunc(5)
  1. main.js loads after the base code files and is responsible for launching the game proper:
    Code:
    PluginManager.setup($plugins);
    window.onload = function() {
        SceneManager.run(Scene_Boot);
    };
    I.e. the base code files are loaded, then the plugins are added, then the boot scene starts. The database is loaded during the boot scene: $dataActors etc aren't initialised until then, so attempting to reference them when your plugin loads will result in an error. Similarly, $gameActors etc are instanced on a per-playthrough basis.

    You can alias an appropriate method and insert your code like that, e.g.
    Spoiler: Example aliases (cf. rpg_managers.js)
    Code:
    var _DataManager_loadDatabase = DataManager.loadDatabase;   // Remember original routine
    DataManager.loadDatabase = function() {   // loadDatabase runs once on game boot
      _DataManager_loadDatabase.call(this);   // call original routine
      // your code here
    };
    
    var _DataManager_createGameObjects = DataManager.createGameObjects;   // Remember original routine
    DataManager.createGameObjects = function() {   // createGameObjects runs on new game
        _DataManager_createGameObjects.call(this);   // call original routine
        // your code here
    };

  2. All active plugins are loaded and merged into one page, so this is a question of Scope. A common approach is to set aside a global "namespace" variable for the plugin (often as a property of an author namespace to help avoid naming conflicts) and define the plugin's public methods on that. A quick example of this:
    Code:
    // global variables to organise plugin code
    var author = author || {};   // author namespace, i.e. everything by "author" (rename as appropriate)
    author.pluginName = author.pluginName || {};   // plugin namespace, i.e. everything from this plugin
    
    (function($) {   // IIFE (optional)
      $.cake = 'cake';   // define a string property
      $.myFunc = function(a) {   // define a function
        return a + 1;
      };
    })(author.pluginName);
    Both cake and myFunc are defined on author.pluginName, which is global, so they are also global. E.g. you can use these expressions in script calls or in other plugins:
    Code:
    author.pluginName.cake
    author.pluginName.myFunc(5)
Thanks dude
One last question
In the function($) you used "$"
What exactly does this dollar sign do?
Thanks dude
One last question
In the function($) you used "$"
What exactly does this dollar sign do?
It's the first argument of the IIFE; you can name it whatever you like but $ is a common name or prefix meaning "global variable". The brackets after the function contain the values passed to the function. So basically, inside the IIFE in my example, $ = author.pluginName. 🙂

I.e. this:
Code:
(function($) {
  // stuff
})(input);
Works similarly to this:
Code:
var myFunc = function($) {
  // stuff
};
myFunc(input)
Except that in the first one, the function isn't available to call later on. 🙏
It's the first argument of the IIFE; you can name it whatever you like but $ is a common name or prefix meaning "global variable". The brackets after the function contain the values passed to the function. So basically, inside the IIFE in my example, $ = author.pluginName. 🙂

I.e. this:
Code:
(function($) {
  // stuff
})(input);
Works similarly to this:
Code:
var myFunc = function($) {
  // stuff
};
myFunc(input)
Except that in the first one, the function isn't available to call later on. 🙏
Thanks again dude✌
Now I've got answers to all the important questions I had the whole time

Original thread: https://forums.rpgmakerweb.com/threads/questions-about-plugin-making.114410/