'Godot 4.0. how stop a auto call SceneTreeTimer?

I have this code :

extends Area2D

func _ready() -> void : auto_call()
    
func auto_call() : 
    await get_tree().create_timer(1, false).timeout
    print( "area autocall" )
    auto_call()

func stop_auto_call() : 
    auto_call = null # how stop?

I know, I can use two options : create a normal Timer or save the references in a global array to later stop it/them, but... it could take a lot of refactoring in several nodes.

I'm tring desactive or pause the node and the code works... less the auto_call calls. I try using unreference(), null, stoping : process, input and physics. But nothing. I see a little hope with get_tree().get_processed_tweens() but it see than don't exist any for SceneTreeTimer. Anyone knows what to do, alternatives or ideas?



Solution 1:[1]

Consider just using tween? Would be nice if timer had the same capabilities but...

extends Node

var _tween: Tween

func _ready() -> void:
    # every 1 second call _auto_call
    _tween = create_tween().set_loops()
    _tween.tween_callback(_auto_call).set_delay(1)
    # cancel after 10 seconds
    await get_tree().create_timer(10).timeout
    _stop_auto_call()

func _auto_call():
    print( "area autocall" )

func _stop_auto_call() :
    _tween.kill()

Sources

This article follows the attribution requirements of Stack Overflow and is licensed under CC BY-SA 3.0.

Source: Stack Overflow

Solution Source
Solution 1 Rakka Rage