# GMS2 How to check if all switch objects in a room are set to true?

I'm trying to implement a puzzle mechanic in Game Maker Studio 2.
Where you need to activate multiple switches for an event to happen (e.g. opening a door)

However, I'm currently stuck in recieving the booleans from all switch objects in a room.
Currently It's only working when any (or only the first) switch is hit.

Here's an example of how it works:

Switch collision with projectile:

switched = !switched


'Switchdoor' step event:

if (switch.switched == true) //this only seem te register the first switch placed
{
//do action
}


If I've used XNA, I could make a List<> to store all the rendered switches and get their boolean values. But what can I use instead in Game Maker Studio 2, who works with rooms?

If my GML is not too rusty, the following should work:

var allSwitchesOn = true;

with(switch) {
if(!switched) {
other.allSwitchesOn = false;
break;
}
}

// allSwitchesOn now has the correct value

• Clever! So instead of checking if all objects holds true, you're just checking if any object holds false. good use of the with() statement, I havn't thought to use it this way. Just letting you know that only the var keyword wasn't needed in this case. But thanks a lot! – Steven Dec 10 '18 at 20:16
• Actually this answer should be doing other.allSwitchesOn &= switched or add a break statement when finding that the value is false (though my GML is rusty and unsure if a break would work). I would definitely refactor to just "other.allSwitchesOn &= switched;" within the with statement. The if statement is asking a needless question. – The Great Duck Dec 28 '18 at 5:22
• @TheGreatDuck sorry for the delay. Documentation says you're right, and short-circuiting the loop is definitely an improvement. Thanks! – Quentin Jan 3 at 16:06
• @Quentin that's actually good that you found that because I have about 5 similar with statements that are suffering from efficiency issues. XD – The Great Duck Jan 4 at 1:18