How do I signal attack power across scenes

:information_source: Attention Topic was automatically imported from the old Question2Answer platform.
:bust_in_silhouette: 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.

:bust_in_silhouette: Reply From: sergkis

See the comment

The parameters in the signal are not arguments that are passed to destination? According to the documentation:

A signal can also optionally declare one or more arguments. Specify
the argument names between parentheses:

extends Node

signal my_signal(value, other_value)

func _ready():
    emit_signal("my_signal", true, 42)

Is this what you mean by parameter transmission?

estebanmolca | 2021-03-10 11:59

if “value” equaled 10 and "my_signal: was emitted, I want “value” to be shared with the enemy scene, so what would it look like on the other end?

jacho317 | 2021-03-11 00:13

the other side only needs to have a function with a parameter to know what to do with the signal. I leave an example as an answer.

estebanmolca | 2021-03-11 00:56

:bust_in_silhouette: Reply From: estebanmolca

Player Script:

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

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.

estebanmolca | 2021-03-12 10:52