._. How to access a function once in IFFE

Status
Not open for further replies.

Riazey

Master of None
Veteran
Joined
Feb 27, 2014
Messages
160
Reaction score
201
First Language
English
Primarily Uses
RMMV
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. :kaoswt2:

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");
 
  }; 
 

 
}());
 
Last edited:

Eliaquim

Hakuen Studio
Veteran
Joined
May 22, 2018
Messages
1,778
Reaction score
1,206
First Language
Portuguese - Br
Primarily Uses
RMMZ
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'])
    };
 

OcRam

Servant of the Universe
Veteran
Joined
Aug 2, 2016
Messages
623
Reaction score
756
First Language
Finnish
Primarily Uses
RMMZ
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 :)
 

Riazey

Master of None
Veteran
Joined
Feb 27, 2014
Messages
160
Reaction score
201
First Language
English
Primarily Uses
RMMV
@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.
 

OcRam

Servant of the Universe
Veteran
Joined
Aug 2, 2016
Messages
623
Reaction score
756
First Language
Finnish
Primarily Uses
RMMZ
@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.
 

MushroomCake28

KAMO Studio
Global Mod
Joined
Nov 18, 2015
Messages
3,783
Reaction score
4,726
First Language
English
Primarily Uses
RMMZ

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.

 
Status
Not open for further replies.

Latest Threads

Latest Posts

Latest Profile Posts


I ended up needing to do a chapter one world map remake. It's still WIP but coming along nicely. =D
Stream will be live shortly! I will be beginning a play-through of Resident Evil 4! Feel free to drop by!

1st iteration on castle interior in Tiled.
I've started up Pillars of Eternity. It's interesting how the game does a "select choice" cutscene using still image art for dramatic flair. I wonder if that might work for my RPGMaker project.
The self-righteous indignation of family when you expect them to pay back money you've leant to them never ceases to amaze me.

Forum statistics

Threads
107,748
Messages
1,031,857
Members
139,888
Latest member
KuramaPrime
Top