[solved] How to show and hide a layer with one button?

Ok. I tried for an hour.
Then I read some Forum threads and tried another 45 minutes.
I think I just don’t understand the concept.
I want to show and hide a layer with one button.
I tried to use the run once condition.
I tried to use subevents.
I tried to use a variable for the visibility.
I just can’t figure it out.
It worked if I show the layer with one button and hide it with another. But I can’t get it to work with one button.
Can someone explain a simple solution and maybe post the code?

I would be very thankfull.

Edit, here is something that you could try:

I actually tried that solution:

That does not work, because when I press F1 it is true for both sub-conditions.
So technically it hides and show up again immediately.
To prove that, I changed the hide and show order in my example.
So now I can hide it one time, but never show it again.

In my case, it should work because we used mouse release (and not mouse click). It may not work, but it should (I could be wrong however).

In your case, you could use either variables, timer, or try to use “Key released” F1 instead of key pressed.

This is just a crude workaround. The right solution is in the next post.

-----------------------------------------------crude workaround-----------------------------------------------------
Ok. I got this.
Simply changing the condition from key pressed to key released doesn’t do the deal.
But with a timer I got it working.

Here is how i did it:

I hope this is helpfull for others too.
--------------------------------------------/crude workaround---------------------------------------------------------------------

Better Solution

To create a switch I recommend to use variables instead of timers unless you do want a delay when you switch.
This is how you do it with a variable:

Also, in case you decide to use a timer in your games, always consider to pause the timer when don’t needed and unpause when needed otherwise you just wasting resources on the timers.


Seems perfect. Not to confuse things, but 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 :laughing:

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.

Great explanation. :slight_smile:

Thank you very much. That solution worked just fine and did exactly what I wanted.