Ask here your questions to get help using GDevelop.
By don_ron
#61121 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.
Last edited by don_ron on Fri 17 Feb 2017, 19:21, edited 1 time in total.
User avatar
By kalel
#61122
don_ron wrote: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:
Image
By don_ron
#61136 I actually tried that solution:
Image

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.
User avatar
By kalel
#61138
don_ron wrote:I actually tried that solution:
Image

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.
By don_ron
#61139 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:

Image

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


Better Solution
V V V V V V V V V V V V V V V V V V V V V V V V
Last edited by don_ron on Tue 21 Feb 2017, 11:26, edited 3 times in total.
User avatar
By ddabrahim
#61145 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:
toggle_layer.png
toggle_layer.png (15.82 KiB) Viewed 400 times


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.
User avatar
By kalel
#61160
ddabrahim wrote: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:
toggle_layer.png


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?
User avatar
By ddabrahim
#61163
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 :lol:

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.
By don_ron
#61234
ddabrahim wrote: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:
toggle_layer.png


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.


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