3
\$\begingroup\$

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?

\$\endgroup\$
5
\$\begingroup\$

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
\$\endgroup\$
  • \$\begingroup\$ 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! \$\endgroup\$ – Steven Dec 10 '18 at 20:16
  • 1
    \$\begingroup\$ 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. \$\endgroup\$ – The Great Duck Dec 28 '18 at 5:22
  • \$\begingroup\$ @TheGreatDuck sorry for the delay. Documentation says you're right, and short-circuiting the loop is definitely an improvement. Thanks! \$\endgroup\$ – Quentin Jan 3 at 16:06
  • \$\begingroup\$ @Quentin that's actually good that you found that because I have about 5 similar with statements that are suffering from efficiency issues. XD \$\endgroup\$ – The Great Duck Jan 4 at 1:18

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Not the answer you're looking for? Browse other questions tagged or ask your own question.