The line works because the area
parameter passes the Area2D object that entered in the area of the signal emitter, in my example it would be the player. I assume you have a function in the player script that deals when they receive damage, that function would be take_damage(value)
. Since you passed the player object as a parameter to the _on_Enemy_area_entered(area)
you can access its "inner function" (a method) with the dot .
passing the enemy's knock_back
as a parameter. Basically what you are doing is calling the take_damage()
function in the player script from the enemy script.
Are you familiar with OOP (Object Oriented Programing)? If not, it would be good to have an overview about it, since it's wildly used in Godot, game and software development in general.
You didn't mention your enemy was a Kinematic Body, so I assumed it was an Area2D. But this is minimal, the only change you need to make in the instructions I passed you is that now the Player's Area2D should be the one detecting the overlap. You can use the body_entered
signal from the Area2D to detect when the enemy's body enters in the Player's Area2D, then in the _on_Player_body_entered(body)
something like:
func _on_Player_body_entered(body):
if body.is_in_group("enemies"):
# knock_back_damage is a variable in your enemy script
var damage = body.knock_back_damage
# take_damage is a function in your player that deals with the damage taken
take_damage(damage)
In other words, you can access a variable of other object using the .
.
I didn't continue with your answer in the other topic because you accepted an answer and because I don't know what more I could do. I would probably need to re implement all your code locally and run tests by myself, something I do not have time to perform.