- Sat 18 Feb 2017, 12:25
how would it work if the "execute once" was removed?
If you remove the trigger once condition the event would be executed every single frame which normally means 60 times in 1 second.
So it would show and hide the layer so fast (60 times in 1 second) it would be difficult to toggle the layer on-off because you would need extremely fast reflexes to catch the moment you need
With the trigger once condition the action is going to be executed only a single time when the condition (F1 key is pressed) is met, after you need to release the key and press it again. So even if you hold the F1 key down, it is going to be triggered only once and then no more. That's why we need to use it in this case.
But to make the switch actually work we also need a little variable trick, because GDevelop trigger every single event from top to bottom every single frame and when you hide the layer the next event is going to be executed immediately after because the layer is going to be hidden and the condition is true. This is where the variables helps. As you can see the show_debug variable need to be 1 to execute an event but we set it to 0 after hide the layer and set it to 1 only if you release the F1 key and press it again. You see, if we don't use the trigger once condition, we set the variable to 1 constantly, every single frame while the key is down and it is something we don't want in this case. By using the trigger once condition we set the variable to 1 only once when the key is pressed and after need to release the key and press it again to set it to 1 again.
By using the "trigger once" condition and the "show_debug" variable you make a single thing executed a single time when you press the key and basically make a switch this way.