MV Can't get a function call to work from a plug-in

I'm trying to use a small custom plug-in to hold all the data for small dating sim elements from the game. But for some reason the function will not work when called through the code. I have, scrubbed everything and rewritten it like 5 times and it just will not do anything. There's no error or crash, it just won't do anything and the game plays the rest of the events as if it wasn't there.
var supdan = supdan || {}; supdan.datingSimFunction = supdan.datingSimFunction || {}; (function($) { $.datingSimElements = function() { if (($gameVariables.value(1) == 1) && ($gameSwitch.value(101) == false)) { $gamePlayer.reserveTransfer(Map005, 0, 0, 0, 0); } else if (($gameVariables.value(2) == 1) && ($gameSwitch.value(121) == false)) { $gamePlayer.reserveTransfer(Map006, 0, 0, 0, 0); } else if (($gameVariables.value(3) == 1) && ($gameSwitch.value(141) == false)) { $gamePlayer.reserveTransfer(Map007, 0, 0, 0, 0); } else if (($gameVariables.value(4) == 1) && ($gameSwitch.value(161) == false)) { $gamePlayer.reserveTransfer(Map008, 0, 0, 0, 0); } else if (($gameVariables.value(5) == 1) && ($gameSwitch.value(181) == false)) { $gamePlayer.reserveTransfer(Map009, 0, 0, 0, 0); } else if (($gameVariables.value(6) == 1) && ($gameSwitch.value(201) == false)) { $gamePlayer.reserveTransfer(Map010, 0, 0, 0, 0); } else { // bounces to the saved area once there are no dating scenes left to call to. $gameTemp.reserveCommonEvent(3); } }; }) (supdan.datingSimFunction);

I've called it in both a common event and in normal events as: supdan.datingSimFunction.datingSimElements();
perviously I've called it as datingSimElements(); and supdan.datingSimFunction.datingSimElements() neither of which have worked either. Please tell me if I missed something super obvious, ty.
When using an anonymous function, you need to use () at the very end to invoke the function.

JavaScript:
(function($) {
    // code code code
})();

Also, a few issues with the code: you need $gameSwitches instead of $gameSwitch; use map id numbers without word 'Map' or zero in front (Map009 needs to just be 9); and I would simplify this instead since your code has consistent values:

JavaScript:
function datingSimElements() {
    for (let i = 1; i <= 7; i++) {
        if (i === 7) {
            $gameTemp.reserveCommonEvent(3);
            break;
        };
        if ( $gameVariables.value(i) === 1 && !$gameSwitches.value(81 + (20 * i)) ) {
            $gamePlayer.reserveTransfer(4 + i, 0, 0, 0, 0);
            // use 'break;' here like the above code if you want to stop this for loop
            // when the conditions are met, otherwise it will cycle through until i is 7
        };
    };
};

With this, you will be able to use datingSimElements() in a script call to call your function. I am sure there are more efficient ways to do this as well.

Please double check to make sure it works the way you want. Thanks!
the way it fails is because its $gameSwitches.value(x) which you forgot "es"
in Switches, also Map005 = just a single numeric value "5" (without quotes).

but the attempt of @bass2yang is clean as you could also use a "switch"
to go through, but I also wonder why you use variable == 1 and not check
if it's ON once an ingame variable has reached it's value to complete unless
I miss something what your function suppose to do.

but the clean way of some of the method is given above which I would take.
there are more, but if you want to use it as plugin, also use

Imported = Imported;
Imported["pluginName"] = true;

so the engine reconise that it is imported.
but the clean way of some of the method is given above which I would take.
there are more, but if you want to use it as plugin, also use

Imported = Imported;
Imported["pluginName"] = true;
I think it may have been the issue? That or an issue with calling the anonymous function. As after some auxiliary testing, the new code still wasn't executing, but now its fixed 🙂

Additionally, thank you for the syntax and efficiency tips, I am often told my code "looks gross," and I tend not to do rewrites for quite a while.
Allow me to blow your mind:

JavaScript:
$.datingSimElements = function() {
  for (let i = 1; i <= 6; i++) {
    if ($gameVariables.value(i) == 1 && !$gameSwitches.value(101 + ((i - 1) * 20)) {
      $gamePlayer.reserveTransfer(4 + i, 0, 0, 0, 0);
      return;
    }
  }
  $gameTemp.reserveCommonEvent(3);
}
also use
Imported = Imported;
Imported["pluginName"] = true;
so the engine reconise that it is imported.
I think it may have been the issue?
That doesn't actually do anything for the engine.

It's a convention amongst plugin authors as a way for you to be able to check which other plugins are being used - e.g. if (Imported.YEP_CoreEngine) - but it doesn't mean anything in terms of the code functioning.
Doh I missed that @bass2yang already did the for loop thing.
Doh I missed that @bass2yang already did the for loop thing.
But that 'return' on $ though - this is a rich function with awesome ROI, indeed. Let's go - to the moon! Haha.
That doesn't actually do anything for the engine.
The computer likes it, and now it works, even with the old non-loop code structure. I think it was the fact that I was using an anon function then, cause having switched to normal function it works.
The computer likes it, and now it works, even with the old non-loop code structure. I think it was the fact that I was using an anon function then, cause having switched to normal function it works.
...that is not the advice I was referencing in the quote in my post.

You are correct that attempting to make the function anonymous is why it didn't work.

Original thread: https://forums.rpgmakerweb.com/threads/cant-get-a-function-call-to-work-from-a-plug-in.165205/