+3 votes

Is it possible to somehow get a variable to get timed for a certain time like 5 seconds and make it go from True to False?

I keep trying to figure something out but i somehow cant get anything to work.

var Timing = true

to

var Timing = false

After like 5 seconds the variable goes from True to False.
Any help with this would be great thanks.

Godot version 3.3.4
in Engine by (41 points)

1 Answer

+2 votes

Depending on your need - there are several options

  • Do this inside the code with yield
yield(get_tree().create_timer(5.0), "timeout")
timing = false
  • Do it in another method asynchronously with connect to the timer
get_tree().create_timer(5.0).connect("timeout", self, "set_timing", [false])
func set_timing(value: bool):
   timing = value
by (162 points)
edited by

Bit late but would like to ask is there some thing that needs to be done first for the first line of code to work? I am not so familiar with yields cant get it to work i might be missing something.

  • Thanks

You don't need to do anything extra. yield stops code execution and waits for what is inside the yield to complete.

So if there is a fragment of code which should be executed immediately it is better to place it above yield or use the second option with connect (written above).

Welcome to Godot Engine Q&A, where you can ask questions and receive answers from other members of the community.

Please make sure to read Frequently asked questions and How to use this Q&A? before posting your first questions.
Social login is currently unavailable. If you've previously logged in with a Facebook or GitHub account, use the I forgot my password link in the login box to set a password for your account. If you still can't access your account, send an email to [email protected] with your username.