Hello, I am new to Godot and I am trying to create a system to cut a tree, it works but the problem is that when the object deletes itself it also deletes all the other trees that I have copied, thank in advance for answers
The code :
extends StaticBody2D
var inside = false
var timeout = false
var life = 3
# Called every frame. 'delta' is the elapsed time since the previous frame.
func _process(delta):
if life <= 0:
queue_free()
if inside == true && Input.is_action_pressed("interaction") && $Timer.time_left <= 0:
life -= 1
$Timer.start()
await $Timer.timeout
func _on_area_2d_body_entered(body):
inside = true
func _on_area_2d_body_exited(body):
inside = false
A screenshot of my object :
(Sorry if my English is not good is not my first language)
Save the scene as something like tree.tscn and then you can just drag that file into your main scene (either the scene tree or the actual view in the editor) or you can use code like var tree = preload("res://tree.tscn") then wherever you want to make a tree parent_node_of_the_new_tree.add_child(tree.instance())
The nodes probably need to be cached first, but if they are deleted anyway, then you would have to copy them. That doesn’t seem like a good solution to me, what exactly do you want to achieve?
Oh, you’re using a signal to detect if the player is inside the area, but you’re not checking if that signal comes from the same tree object. (Plus your area is probably exactly the same object for each tree, tho that’s not relevant here.)
You need a way to make sure you’re only cutting down one tree and not all the trees that receive that signal.
Ok thanks you for the answer I’m going to search in that direction but do you have a way to do that or another node that can be use to replace area2d ?
All you need is your signal to send an unique identifier as a parameter, then the trees can check it and only self-destroy if it is the one that matches.
There are oher ways to do all this if you wanna rewrite the entire thing.