MV Reference Error in my plugin

I have a plugin that I wrote. It contains a single function. Nothing else.
Code:
ASCheckAppointment = function() {

  switch($gameVariables.value(175)) {
  case 'MM':    // Monday Morning
      $gameVariables.setValue(177) = 1;
      $gameVariables.setValue(178) = 1;
    break;
  case 'MA':  // Monday Afternoon
      $gameVariables.setValue(177) = 1;
      $gameVariables.setValue(178) = 2;
    break;
  case 'ME': // Monday Evening
      $gameVariables.setValue(177) = 1;
      $gameVariables.setValue(178) = 3;
    break;
// Several other entries similar to the above removed for sake of brevity.
  default:
    // code block
  }
}();


I call it from an event with a script call

Script : ASCheckAppointment();

But when it runs I get
Reference Error
ASCheckAppointment not defined


What could be causing it? Am I supposed to define it some other way or add something else to the .js file?
Remember to save your project to apply Plugin Manager changes before testing.

Press F8 (or F12) to show the dev tools window: any load errors (e.g. syntax error) will show on the Console tab: if your plugin errors out like that, its code will have no effect in-game.

Otherwise I imagine you've defined the function on something other than the global scope:

I think there are also problems within the function itself, e.g.
  • "Set game variable ID 177 equal to 1" should look like this:
    JavaScript:
    $gameVariables.setValue(177, 1);

  • You use the pattern function() { ... }(), which defines a function and then immdiately invokes it. But plugins load before $gameVariables is initialised.

Edit: when posting code, consider using code tags, either via BBCode ([code]code[/code]) or the </> button on the toolbar above the box where you type your post. That helps to make code easier to read. 👋
Just a few formatting notes to help you get better help - if you post your code in the CODE tags (the </> on the edit bar) it will preserve indentation and brackets.

And if you provide actual screenshots of the error trace in your Console tab and where you're calling the script in the editor, it will help us eliminate any contextual things or typos.

Are you starting a new game since you added the plugin, not trying to load a save?

You have extra parentheses at the end of your plugin file which will make it try to run immediately.

You are also misusing the value() function. You can't set the return value of a function equal to something. You need to be using $gameVariables.setValue(177, 1);

Edit: lol, @caethyril ninja'd me 😀
Updated Code :
Code:
ASCheckAppointment = function() {

switch($gameVariables.value(175)) {
  case 'MM':
      $gameVariables.setValue(177,1);
      $gameVariables.setValue(178,1);
    break;
  case 'MA':
      $gameVariables.setValue(177,1);
      $gameVariables.setValue(178,2);
    break;
  case 'ME':
      $gameVariables.setValue(177,1);
      $gameVariables.setValue(178,3);
    break;
  case 'TM':
      $gameVariables.setValue(177,2);
      $gameVariables.setValue(178,1);
    break;
 
  default: // Not Defined
    // code block
} 

};

I'm not getting a reference error anymore and after I adjusted one of the variable numbers, it's working as designed!

Thank you very much!
Just to mention it, there's no need to include a default case if you don't actually do anything in it.

If the switch value doesn't meet one of the criteria, it will do nothing - which is, of course, the same as having a default case that does nothing.

It's not, like, a required piece of syntax.
In addition to that, a suggestion: rather than separately dealing with every combination of day and time of day, split the string into two and calculate the variables separately. Then you only need 10 switch statements: 7 for what day it is, and 3 for what time of day it is.
Updated Code :
Code:
ASCheckAppointment = function() {

switch($gameVariables.value(175)) {
  case 'MM':
      $gameVariables.setValue(177,1);
      $gameVariables.setValue(178,1);
    break;
  case 'MA':
      $gameVariables.setValue(177,1);
      $gameVariables.setValue(178,2);
    break;
  case 'ME':
      $gameVariables.setValue(177,1);
      $gameVariables.setValue(178,3);
    break;
  case 'TM':
      $gameVariables.setValue(177,2);
      $gameVariables.setValue(178,1);
    break;
 
  default: // Not Defined
    // code block
}

};

I'm not getting a reference error anymore and after I adjusted one of the variable numbers, it's working as designed!

Thank you very much!
Assuming the rest of these cases do the same thing for other days of the week, the function can be simplified to this:
JavaScript:
ASCheckAppointment = function() {
    $gameVariables.setValue(177,"MTWHFSU".indexOf($gameVariables.value(175)[0])+1);
    $gameVariables.setValue(178,"MAE".indexOf($gameVariables.value(175)[1])+1);
}

EDIT: What Trihan proposes is also a good idea if you want to do more special things in each case.
this is what I did for time system, but convert it in your case, you can do
something like this:
JavaScript:
function ASCheckAppointment(appointment) 
    switch(appointment) {
        case 'MM':
            $gameVariables.setValue(177,1);
            $gameVariables.setValue(178,1);
          break;
        case 'MA':
            $gameVariables.setValue(177,1);
            $gameVariables.setValue(178,2);
          break;
        case 'ME':
            //code
            break;
        default:
            alert("something was missing!")
    }
}
but it depends how you want to controll them, you can use the control variable
and set that as:
control variable #175: script: ASCheckAppointment("MM")

so when you call that, it should show what you want, whatever is in the
switch statement as I use it for a real time system check 🙂

Original thread: https://forums.rpgmakerweb.com/threads/reference-error-in-my-plugin.161917/