On code organization and node-signal relationships

Godot Version 4.2.2 stable

Hello! While I’m not new at programming, I’m new to game dev in general. I’ve modelled a “room” and named it as “base_cell”. The idea is when I click any of the walls in the base cell, it creates another cell in that direction and removes the connecting walls.

've got a Node3D for every wall and they are all under the base_cell node which is instanced in my main scene. The mouse click logic is attached to every Wall node, since it makes sense that I need to know which Wall was clicked on. However, the code I’ve mustered up to create a new base_cell hasn’t worked

extends Node3D
func _instantiate_cell(name):
	var root = self.get_parent()
	var new_cell = root.duplicate()
	for child in root.get_children():
		if child.name == name:
			continue
		new_cell.add_child(child.duplicate())

However, I can’t put the logic for cell duplication on the root Node3D since I’d lose access to the input_event call. Is my entire Node arrangement logic wrong? Is it possible to handle this signal in a parent node? Did I paint myself into a corner by trying to reuse as much code as possible? Is there an appropriate way to handle this? Should I just load the main scene inside the wall script and do it that way?

duplicate on root sounds like the wrong function for this, it will copy the children too, thus duplicating the entire “house” when clicking a “room”.

new_cell would need to be added as a child to something, duplicated nodes do not have parents, or try replacing new_cell with root

Bingo, I was missing the “add_child” part. I rearranged things such that the base cell dealt with instantiation and that was called from within the wall_click_detect script:

wall_click_detect.gd:

func _on_static_body_3d_input_event(camera, event, position, normal, shape_idx):
	if event is InputEventMouseButton:
		if event.button_index == MOUSE_BUTTON_LEFT and event.pressed == true:
			if can_expand:
				var root = get_parent_node_3d();
				root._instantiate_cell(self.name)			

Then, inside the BaseCell node:

func _instantiate_cell(name):
	var new_cell = Node3D.new()
	print(name)
	for child in self.get_children():
		if child.name == name:
			continue
		new_cell.add_child(child.duplicate())
	var sceneroot = self.get_parent()
	sceneroot.add_child(new_cell)
        _plop(name)

Now, the plop function will handle the cell position according to the clicked wall.

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