Problem with duplicating an object within its own code

Godot Version

4.2.1

Question

I’m having a problem where the code in my object runs fine but when I instantiate and add another one of the same object none of its code is run. I have tested both the ready and physics process and the only one that was ever run was the original. My goal is for this object to spread over the tilemap over time but it only creates 4 new objects then stops since the new ones don’t run their code. I’ve tried looking for the answer but I can’t find it anywhere. Does anyone know why this is happening.

This is the code in the node “tree_roots.tscn”

extends StaticBody2D

@onready var tilemap = $“…/Grass”
@onready var corruptedTilemap = $“…/Corruption”
@onready var treeRoots = preload(“res://Objects/Towers/tree_roots.tscn”)

var coords

func _ready():
coords = tilemap.local_to_map(global_position)

func _physics_process(delta):
if(corruptedTilemap.get_cell_tile_data(0,coords) != null):
shoot()
tilemap.erase_cell(2,coords)
queue_free()

func _on_timer_timeout():
var neighbors = tilemap.get_surrounding_cells(coords)
for i in 4:
if(tilemap.get_cell_tile_data(2,neighbors[i]) == null):
tilemap.set_cell(2,neighbors[i],0,Vector2i(0,0))
var tempPlant = treeRoots.instantiate()
tempPlant.global_position = tilemap.map_to_local(neighbors[i])
get_parent().add_child(tempPlant)

func shoot():
corruptedTilemap.corruptionPurified(coords)

who call this? because this block of code the one responsible of creating the treeRoots, it could be not triggered this function, hence no spawning

You are triggering the instantiation from within a callback to a timer, right? Did you make that timer Autostart? You could also start it manually in the _ready() call.

I must have worded my question wrong, because the code in the original object all runs fine and it creates 4 new versions of itself. The problem is that the new versions seem to not even have a script because they don’t run any of there functions. Not even their _ready(). But they should work just fine since they are the same object as the original, it’s just duplicating itself.

You are setting the global position before adding them as child.
I always get error or warning messages when doing this and the position is not being applied. I had to add them as child first, then set global position.
Is it possible they are all stacked on the same position? Try checking in the remote tab to see if your parent object has multiple plants or print something in ready.

I haven’t had any problems with this in the past, and on this specific instance there are 4 objects that are created in the right locations. They just don’t run their own code once created.

Can you show your scene structures for tree_roots and the scene where your first tree root scene is in?

Post your project files, it will be easier.

I think I figured out the problem. Apparently both my node file and gdscript file were corrupted somehow. I don’t know why it would even work once but that was why it was being so weird

I fully figured it out now. the line @onready var treeRoots = preload(“res://Objects/Towers/tree_roots.tscn”), was causing the code to corrupt the node I had attached to it because it was preloading itself. So I just deleted that and replaced treeRoots.instantiate() with duplicate(). But I also had to start the Timer manually, for some reason the autostart wouldn’t work on the new objects only the original.

That seems like something the syntax engine should call out. Wanna report it as a bug?

Didn’t think about that, will do

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