• Godot HelpQuestion
  • Connecting dependencies of nested nodes/objects inside the root main component?

Connecting dependencies of nested nodes/objects inside the root main component?

Hey all, started a course on Godot and I wanted to get your opinion on the best approach to pass dependencies to nested Nodes.
I have the following main scene:

Now inside Player is the following:

And SwordAbilityController:

Now inside of SwordAbilityController when the Timer ends, a reference to Player and the Enemies is gotten via groups like get_tree().get_nodes_in_group("enemy") and get_tree().get_first_node_in_group("player"), and then one deals damage to the other, for example. Both player and enemy have been added to groups of course.
This is according to the course.

Now I felt strange having to reference Player and Enemies directly like this so I thought I would pass the dependencies via a setup function in SwordAbilityController, and removed the groups:

extends Node
class_name SwordAbilityController

@export var enemies: Array

var player: Node2D

func _ready():
	$Timer.timeout.connect(on_timeout)

func setup(player: Node2D, enemies: Array):
	self.player = player
	self.enemies = enemies

After this I call this setup function from a script attached to Main:

extends Node

@onready var sword_ability_controller = (
	$Player/AbilityManager/SwordAbilityController as SwordAbilityController
)


func _ready() -> void:
	sword_ability_controller.setup($Player, [$BasicEnemy, $BasicEnemy2])

Now being a developer myself, but since I am new to Godot and gamedev, I am trying to understand what a scalable approach for the future might be, where I have cases like these. I could propagate the Timer end signal all the way up to the Main root node, however this would cause too many signal bubbling, as per this so
So is there anything wrong with this or do you thin this is perfectly fine also for other such controllers and so on? Is there some better way to handle this separation of concerns?

Thanks!

  • xyz replied to this.

    jazz
    Get rid of the SwordAbilityController.
    Let the main or player script handle the combat.