._. How to access a function once in IFFE

Okay so, everyone everywhere says that you should work on plugins in an IFFE to prevent a lot of stuff like pollution/namespaces etc.
And that it's good practice to do so when working in rpg maker etc etc.

But I don't understand how to access/run functions or the plugin in general at ALL once they are inside an IFFE. I've been looking at other codes and tried looking up stuff online but online most people like to use really convoluted examples haha. After half a day sunk into trying to figure it out on my own I give up oml.


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


I understand to call the following from a plugin in an rpg maker event script is testieboi();

1589925884482.png



I am pretty sure I have to add something to the plugin, the call, or both to get this to work but I don't understand what the internet is telling me. When I look at other plugins I only see 2 or 4 things outside of the (function() { }()); block. But I have NO idea which is doing the trick. 😅

Can someone straight-up tell me exactly what I need to add to the below or the event script (or both) to run the exact same piece of code once it's placed into an IFFE? *screeches*

1589925943379.png



Code:
(function()  {


var testieboi = function() {
  console.log("Holy I work");
 
  }; 
 

 
}());
Hi there!
You cannot access the way you do it.
You have to create a namespace and wrap your functions with it. So you can access these functions like this:
JavaScript:
var riazey = riazey || {} // your nameSpace
riazey.yourPluginName = riazey.yourPluginName || {} // your plugin nameSpace


(function()  {

riazey.yourPluginName.testieboi = function() {
  console.log("Holy I work");
  }; 
}());
But honestly, when I started to develop plugins I also use the IFFE. But I had some "issues" that made me prefer not to use it. And I advise you too.
If you create proper nameSpaces, you will don't have any problems with global variables conflicts, because I think that is the most common reason for using an IFFE(or just because people see that in plugins and repeat this without knowing whats it is).
Here is my template, that you can use it freely.
JavaScript:
"use strict"

var Imported = Imported || {};
Imported.Eli_Template = true; // In case you want to make extension.

var Eli = Eli || {}; // Your name space
Eli.Template = Eli.Template || {}; //The plugin name space(it is useful for you to not overwrite accidentally your own functions/variables)


Eli.Template.Parameters = PluginManager.parameters('Eli_Template'); // Get the plugin parameters
Eli.Template.Param = Eli.Template.Param || {};

    Eli.Template.Param = { // Here you can "parse" your parameters, evaluate them.
posX: Number(Eli.Template.Parameters['Window X'])
    };
Benefits of "IFFE" are:
- Private scope (my favorite)
- Minimal bloating of global namespace
- Memory usage

But do not use it if you are about to create global classes for example.

And to call functions from it do it like @Eliaquim answered 🙂
@Eliaquim @OcRam

YES! This is EXACTLY what I was confused about!! Aaaa ty so muuuch guuuys <3

I was putting scripts to spawn random monsters from pools + store them in the farms + a ton of other stuff but I realized I had just a ton of 10 line block script codes declaring vars every time was was blegh.

But still, if I was gonna make a plugin I was gonna do it right! IFFE here I come! I am super excited~ The explination was so well written!



If you guys happen to know as well, is it more resource heavy on a pc to read from plugins or to paste the 6 duplicate script calls into every map? I mostly want to use a plugin for organizing my code and put all the per map spawn rates etc into objects, but if its resource heavy to call on plugins I can limit it to the most repetitive pieces.


Adding to that, if I had 5 functions inside my iffe, and I called one, will it read the whole plugin or just that one function?
Basically I want to know if it's worth including my spawning, capturing and the pre-battle functions into 1 plugin or if that will make it noticeably more resource heavy by the end.
@Eliaquim @OcRam

YES! This is EXACTLY what I was confused about!! Aaaa ty so muuuch guuuys <3

I was putting scripts to spawn random monsters from pools + store them in the farms + a ton of other stuff but I realized I had just a ton of 10 line block script codes declaring vars every time was was blegh.

But still, if I was gonna make a plugin I was gonna do it right! IFFE here I come! I am super excited~ The explination was so well written!



If you guys happen to know as well, is it more resource heavy on a pc to read from plugins or to paste the 6 duplicate script calls into every map? I mostly want to use a plugin for organizing my code and put all the per map spawn rates etc into objects, but if its resource heavy to call on plugins I can limit it to the most repetitive pieces.


Adding to that, if I had 5 functions inside my iffe, and I called one, will it read the whole plugin or just that one function?
Basically I want to know if it's worth including my spawning, capturing and the pre-battle functions into 1 plugin or if that will make it noticeably more resource heavy by the end.
Hi,

All plugins are loaded when game is loaded. So you may call all functions freely.
Hi,

All plugins are loaded when game is loaded. So you may call all functions freely.

Glorious thanks so much again guys! <3 😤
Go for it!!
:MV1:

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/_-how-to-access-a-function-once-in-iffe.121786/