how do I use timer and coroutine yield to toggle a bool

:information_source: Attention Topic was automatically imported from the old Question2Answer platform.
:bust_in_silhouette: Asked By Abdulelah444

Hi…

I want to give my character a gliding ability where at certain condition it starts gliding for a period of time then it drops down, I have everything done well, I made a simple bool condition if true , character glides if false it drops down…

now how do I toggle this Boolean?
I used coroutine yield it worked but it was not consistent, how do I get it to be consistent or how do I use a timer instead because I tried and it did not work well

Well how long should the gliding last? If it’s a set amount of time, then a Timer is probably the most appropriate route to take and it should be consistent. In what way did it not work well?

i_love_godot | 2019-12-11 15:17

I couldn’t get it to work and I don’t know why…

func _ready():
     hovering_timer.set_one_shot(true)
     hovering_timer.set_wait_time(Gravity_Dilution_Time)
     hovering_timer.connect("timeout", self,"On_hovering_timeout_complete")
     add_child(hovering_timer)

func movement():
    if Enable_Gravity_Dilution :
                   *logic*
	hovering_timer.set_wait_time(Gravity_Dilution_Time)
	hovering_timer.start()

func On_hovering_timeout_complete():
       print("timeout")
       Enable_Gravity_Dilution = false

I really don’t know how to use timer correctly, I came from unity and it was much easier using coroutines

Abdulelah444 | 2019-12-11 17:17

What behaviour are you seeing? And how does that differ from what you expect to happen? Is the timer running? Does “timeout” print to the screen?

i_love_godot | 2019-12-11 17:26

I am seeing no changes, timer has no effect what so ever and I would expect it to execute what’s under if Enable_Gravity_Dilution for a period of Gravity_Dilution_time after time runs out Enable_Gravity_Dilution would equal false and only reset back when is_on_floor() is true.
no "timeout" does not print

Abdulelah444 | 2019-12-12 08:49

:bust_in_silhouette: Reply From: Magso

Assuming movement() is called once every time the glide starts you can use this.

func movement():
    if Enable_Gravity_Dilution :
        *logic*
        yield(get_tree().create_timer(Gravity_Dilution_Time),"timeout")
        Enable_Gravity_Dilution = false

I did this and it works one time, but if I wanna use it again immediately I can’t , I have to take some time before attempting this again…

Abdulelah444 | 2019-12-12 08:43

How do you mean it one time? If you’re calling movement() and setting Enable_Gravity_Dilution back to true before the yield time is up, it will turn it back to false before expected because it will only be the remaining time left of when the function was called previously.

Magso | 2019-12-12 09:18

I mean I do the hover and time runs out and I fall back to floor, on the floor Enable_Gravity_Dilution is turned back to true.
so i go immediately trying to hover again and it doesn’t work I quickly fall to the floor like there was no hovering effect, after I fall back to the floor I immediately try doing it again and the same thing happens, on the third time it works and the pattern repeats unless I wait some time between the times I try to hover, it is as if it Enable_Gravity_Dilution gets turned off for the same amount I set in yield function or maybe it’s an arbitrary time I couldn’t measure it

Abdulelah444 | 2019-12-12 11:59

print(Enable_Gravity_Dilution) in _process can tell you that. However the problem maybe where you’re calling movement() from or variables that aren’t being set correctly after it executes the first time.

Magso | 2019-12-12 17:17