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\$
6
\$\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\$
3
  • 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\$
    – user64742
    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 '19 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\$
    – user64742
    Jan 4 '19 at 1:18

You must log in to answer this question.

Not the answer you're looking for? Browse other questions tagged .