Topic was automatically imported from the old Question2Answer platform.
Asked By
jacho317
For example, “Player” has an attack power of 10 and attacks “Dummy”, who loses 10 hit points. Rather than coding an enemy to take a fixed amount of damage from a specific attack, I want the damage the enemy takes to be reliant on the attack power of the player’s attack value, so the damage dealt changes whenever the player’s attack power changes.
extends Node2D
var power_attack = 34.0
signal attack(power) #create signal with parameter
func _ready():
#we connect the signal to the target: (self refers to the current node)
#<source_node>.connect(<signal_name>, <target_node>, <target_function_name>)
self.connect("attack", $Enemy, "remove_health")
#in this case, target is a chilld named Enemy.
#in this case, the target (or targets) must have a function remove_health()
func _input(event):
if Input.is_action_just_pressed("mouse_click"):
#if the my custom action is just pressed, emit with attack power:
emit_signal("attack", power_attack)
Enemy script:
extends Node2D
var health = 300
func remove_health(h):
health = health - h
print(health)
You could also send and receive signals to a function of the same script:
self.connect("attack", self, "remove_health")
Are these scripts connected to kinematic bodies? Because I extended them to themselves.
jacho317 | 2021-03-11 21:24
The example I gave is a custom signal, you can go to any node that declares the target function (for that you need that node to have a script). Then there are also specific signals from the nodes. They are the ones that can be seen in the inspector.