Basic switch plugin - will this work?

This is for a quest that I'm making that'll require some work off JavaScript. I took some Code Academy lessons and figure out the basic languages. Here is what I input:


function puzzle1()
{


    var switch1 = a;
    var switch2 = b;
    var switch3 = c;
    var switch4 = d;


    if(switch2, switch4, switch1, switch3 === true)
    {
        confirm("The chamber gives out a loud click, and the door unlocks!");
        //Hidden door unlocks
    }
    else
    {
        confirm("The switches gave a surge, restarting the puzzle.");
    }
}
puzzle1();


My objective with this script is to use it for a basic puzzle in a specific chamber. There will be 4 switches, and the user will have infinite opportunities to guess. It'll be out of order, but in a fixed order that the user won't know. It'll require all 4 switches to be active to determine whether the order is true or false. If false, the puzzle will reset and the user will have to retry.


If true, however, a door will open for the user and a break will occur within the loop, ending the scripts repeat and allow the actor to go onward. This order is perm, and won't random if starting a new game or load.
no.


What is a, b, c and d?  Are they variables, or something that should be passed in?  Why are you assigning them to NEW variables and then testing the new variables?  Why not just test a, b, c and d directly?


The correct condition is this:

Code:
if (switch1 && switch2 && switch3 && switch4)

Or, to remove the need for temporary variables, this:

Code:
if (a && b && c && d)

again depending on what those letters stand for and where they come from.


If you have a boolean and are testing for true, the === true is unnecessary.  Just saying if (switch) will return true or false.


I've moved this thread to Learning Javascript. Please be sure to post your threads in the correct forum next time. Thank you.


Also, why are you writing a script for this?  It can be done in events:

Code:
Conditional Branch: Switch1 is ON
  Conditional Branch: Switch2 is ON
    Conditional Branch: Switch3 is ON
      Conditional Branch: Switch4 is ON
        Show Text: The chamber gives out a loud click, and the door unlocks!
        Control Switches: DoorUnlocked = ON
        Exit Event Processing
      Branch End
    Branch End
  Branch End
Branch End
Show Text: The switches gave a surge, restarting the puzzle.
(assume you want to turn all the switches off here again)
I was trying to make the switches be in order for the door to open if the switches have been chosen correctly in that order. 


I'm also use to C#'s defining and still new to JavaScript. For the variables, I plan to link a, b, c, and d onto the switch objects for them to be called. I didnt think switch alone would be enough. 


Lastly, true was suppose to be to check and see if the choices are correct. If not, the loop I created with function would restart the process
As I already said, if (switch1 === true)  is the same as if (switch1)


I know what you were trying to do.  I explained why it wouldn't work, and gave you the correct commands to use, and also showed you how to do it with an event so that you don't even need to use a script.


If Switch1, Switch2, Switch3 and Switch4 are being turned on when you choose each switch, after you turn them on, add a command to call a new common event, and then put the code above in the common event.


HOWEVER, even that will not do what you want.  All the above would do (and your code, if you'd had the correct syntax) is test whether they'd all been turned on - in any order.  It would really help if you explain what you're trying to do, rather than just show us the code.  You said nothing in your OP about switches needing to be turned on in the correct order.  In that case, you need to use self switches and variables, not switches at all.  And you still don't need to use a script for this, so I'm going to go ahead and move the thread over to MV Support.


So now that we know you have a puzzle where you have to turn on switches in the correct order - do you want the puzzle to be reset the moment a switch is turned on out of order?  Or do you want the player to have to turn on all 4, and THEN check if they're in the correct order?
Sincere apologies for not explaining earlier. I'll edit my post after I explain.


Anyway, this is a basic puzzle for one domain. I will want the puzzle to reset if the order is incorrect, but not randomize, such as the Random feature off C# (If JavaScript has that.) technically, it's a fixed order and the user is allowed to try many times as they want. Until the puzzle is correct, it'll forever reset. Soon as the puzzle is finalized and correct, a door will open, and the script will break out of it's loop and become finished.


It would be too easy to simply go with "the one wrong switch, everything restarts" route and it'll catch the user on quickly, so activating all 4 switches and then have the script check whether the switches are correct or not will determine if the door opens. I was also thinking about a Skyrim setup, except the switches will be define "color" and must all have the same color to match, but in return will randomly change the other switches color after one switch is activated, but I think that be too advanced for the mean time.


So primarily speaking, I want to go with the all 4 switches route and check to see if true or false.


Apologies for not briefing this earlier! Thank you for the help!
Okay.  We can do this a couple of ways.  I'll give you a method that will use the smallest number of switches/variables, and require no plugins/scripts.


Each of your switch events will have 3 pages.  


Page 1 has no conditions, and the graphic is the switch in the off/unpressed state.  Add the following commands:

Code:
Control Self Switch: A = ON
Control Variables: Puzzle Order *= 10
Control Variables: Puzzle Order += 1
Call Common Event: Check Switch Order

Now, where I said += 1, you need to change that for each of your switches.  The first switch to be pressed will have += 1, the second will have += 2, the third will have += 3 and the fourth will have += 4.  We're multiplying by 10 each time so we can get the order of the switches being pressed.  What will happen is that they'll press the first switch, the variable will be 1.  They'll press the second, the variable will be 12.  They'll press the third, the variable will be 123.  They'll press the fourth, the variable will be 1234.  If they press them in any other order, the variable will not be 1234 at the end, but some other 4-digit number.


Page 2 is conditioned by self switch A and has an image of the switch in the on/pressed position.  No event commands.


For this method, we'll add a third page conditioned by switch Reset Puzzle.  Set this to parallel process, and the only command will be to turn off self switch A.  


Now for the common event - we're going to check if all 4 switches have been pressed.  If they have, and they weren't pressed in the right order, we'll reset the puzzle.  If pressed in the right order, we'll open the door.  If not all have been pressed, we just won't do anything.

Code:
Conditional Branch: Puzzle Order > 1000
  Conditional Branch: Puzzle Order = 1234
    Text: The chamber gives out a loud click, and the door unlocks!
    Control Switches: Door Unlocked = ON
  Else
    Text: The switches gave a surge, restarting the puzzle.
    Control Variables: Puzzle Order = 0
    Control Switches: Reset Puzzle = ON
    Wait 3 Frames
    Control Switches: Reset Puzzle = OFF
  Branch End
Branch End

And that's all you need.  It should work perfectly like that.  The reason we turn the Reset Puzzle switch on, wait a few frames, then turn it off again, is just to activate page 3 of the switch events, which will turn off all the self switches.


Here are some alternatives that will allow you to just have the first 2 pages on each event.  You will need to replace the two Control Switch commands for Reset Puzzle and the Wait 3 Frames command as instructed below:


Method 1: Use the Kadokawa plugin 'Outer Self Switch' (included in DLC shipped with the engine).  In the common event, where you need to reset the puzzle, use the plugin command to turn of outer self switch A on each of the 4 events.


Method 2: Use a script command to turn off the self switches.  In the common event, where you need to reset the puzzle, use the script command to turn off self switch A on each event (replace map_id and event1_id, event2_id, event3_id, event4_id with the correct numbers - no leading zeros):

Code:
$gameSelfSwitches.setValue([map_id, event1_id, 'A'], false)
$gameSelfSwitches.setValue([map_id, event2_id, 'A'], false)
$gameSelfSwitches.setValue([map_id, event3_id, 'A'], false)
$gameSelfSwitches.setValue([map_id, event4_id, 'A'], false)

Method 3: Use an individual switch for each event rather than self switches.  On page 1 you'll turn on the switch instead of the self switch, page 2 will be conditioned by the switch instead of the self switch, and in the common event you'll turn off all 4 switches rather than the self switches.  This method just uses more switches.
Alright. Ill test each of these methods to see which will work best.


Thank you again for your help!
They will all work the same 🙂   Which method you use depends on which one you find easiest to understand, and which one you prefer to use.


If you are happy doing the script call to change self switches on other events, use method 2.  This will result in the lowest number of switches and variables being used, and no plugins.


If you're happy using plugins, use method 1 (listed under alternatives).  This will also result in the lowest number of switches and variables being used, and no requirement for script calls.


Personally I'd use method 2, as I don't like using switches when only one event is going to be affected by it (as per method 3), and I'm not fond of using parallel process event pages unless I have to (as per the original method), and for something so simple, and because I'm familiar with the scripts, I'd rather just do a quick script call than use a plugin, in a case like this.


But for you, it all comes down to which method you like better or find easier to understand.  The logic of detecting the order in which the switches were activated, and whether they've been done in the correct order or not, is the same for all methods.

Original thread: https://forums.rpgmakerweb.com/threads/basic-switch-plugin-will-this-work.56011/