#Connect a script to an arbitrary signal from a parent node

2 messages · Page 1 of 1 (latest)

worthy widget
#

I want to be able to add this "DamageComponent" node to anything with a RigidBody2D.

Then I want that arbitrary RigidBody2D OnEntered signal to trigger a function on the DamageComponent.

This DamageComponent will be used in a few other places and I'm trying to isolate what it needs as much as possible. I just want to be able to add it, maybe assign an exposed variable or two, and that's it. This is the part that's confusing me since I don't know how to connect it to an arbitrary "OnCollision" signal from a parent node with code.

I may be misunderstanding some key things about signals still, this is day 2 in the engine for me. Please excuse any ignorance :).

Damage is done on physics collisions if that context helps.

ebon grove
#

letss assume you want to connect the parent's body_shape_entered() signal to a function in your DamageComponent

Keep in mind your custom function must accept the same number of arguments as the signal sends out.

func _ready() -> void:
    var parent := get_parent()
    parent.body_shape_entered.connect("custom_function")

https://docs.godotengine.org/en/stable/getting_started/step_by_step/signals.html

https://docs.godotengine.org/en/stable/classes/class_rigidbody2d.html#signals