0 votes

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.

in Engine by (12 points)

2 Answers

0 votes

See the comment

by (105 points)
edited by

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?

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?

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.

0 votes

Example.
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
    print(health)

You could also send and receive signals to a function of the same script:

self.connect("attack", self, "remove_health")
by (2,058 points)
edited by

Are these scripts connected to kinematic bodies? Because I extended them to themselves.

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.

Welcome to Godot Engine Q&A, where you can ask questions and receive answers from other members of the community.

Please make sure to read How to use this Q&A? before posting your first questions.
Social login is currently unavailable. If you've previously logged in with a Facebook or GitHub account, use the I forgot my password link in the login box to set a password for your account. If you still can't access your account, send an email to webmaster@godotengine.org with your username.