Hi all, I must be using timers incorrectly or something, maybe someone can help me find/understand what I am doing wrong. I am following a tutorial series on youtube where you create an apple tree using a timer to transition the tree from not having apples, to having apples. Once you interact with the tree and “pick” the apple. It plays an animation, and then after a couple of seconds it starts the timer once more.
It starts in ready with no apples and the timer after X seconds will change the state which in turn renders the apples on the tree. When I debug the game with print, I can see all the instances are supposed to be starting their timers. But what I am experiencing is, that it only actually starts the timer when it’s visible on screen. I don’t have it wrapped in a VisibleOnScreen or anything like that. Why would this be the case?
The script:
extends Node2D
@export var item: InventoryItem
var player = null
var state = "no_apples"
var player_in_area = false
var apple = preload("res://scene/apple_collectable.tscn")
func _ready():
print("apple tree ready")
$growth_timer.start()
func _process(_delta):
$AnimatedSprite2D.play(state)
if state == "apples" and player_in_area and Input.is_action_just_pressed("interact"):
state = "no_apples"
drop_apple()
func _on_pickable_area_body_entered(body):
if body is Player:
player_in_area = true
player = body
func _on_pickable_area_body_exited(body):
if body is Player:
player_in_area = false
func _on_growth_timer_timeout():
print("apple_tree timer timeout", state)
if state == "no_apples":
state = "apples"
func drop_apple():
var apple_instance = apple.instantiate()
apple_instance.global_position = $Marker2D.global_position
get_parent().add_child(apple_instance)
player.collect(item)
await get_tree().create_timer(1.5).timeout
$growth_timer.start()
And this is how the scene looks: