How do you run code the moment a bool turns true or false

Godot Version

version 4.2.2

Question

so I’m quite new to godot, and I can only figure out how to run a line of code while a boolean is true or false, but not when it turns true or false. can anyone show me how to do this? thanks!

What context are we talking about? The property on a class you wrote? Or a property on some other class? What value is it you want to check?

If you want to run some code when a variable has changed inside of a Node script you should probably consider using setters. For example you could write code like that:

# it can be exported.
# If you use @tool at the start the code, the code inside of the setter will also run when changing the value inside of the editor. (See Engine.is_editor_hint())
@export
var someBooleanValue: bool = false: # default is false
    set(newValue):
        someBooleanValue = newValue # we have to save the change (if you don't add this line, someBooleanValue will never change)
        ## here some code to do after the value has changed

Reference: GDScript reference — Godot Engine (stable) documentation in English

2 Likes

Thanks a lot :)))