Script play one time for multiple object

Godot Version

4.0

Question

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 :
image_2023-12-11_173936284

(Sorry if my English is not good is not my first language)

That just means you made all your trees the same object instead of an instance of the tree scene.

How can I make my object an instance if the tree scene ? (sorry if is a dum question)

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())

get_children

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?

This is already like this, I didn’t know it was called that

Then post the code where that happens.

I want that when the player cut the tree, the tree drop logs that is gonna be use to craft things, ect… but I haven’t implemented this yet

Here the main scene :
image_2023-12-11_174119923

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.

Thank for the respond I search for that and I find the solution I just have to make the area2d unique (in the prefab)

1 Like

This topic was automatically closed 30 days after the last reply. New replies are no longer allowed.