Topic was automatically imported from the old Question2Answer platform.
Asked By
Frasse
Hi. I have two players (kinematic bodies). Player 1 and player 2.
I want that when Area2D is entered then send signals to the players code. Okay. My questions is how to tell the engine that when player 1 enters the area then send “area entered”-signal to player 1 code. And when player 2 entered the area then send signal to player 2 code. Please help!
You need to use the body_entered signal, because the players are KinematicBody2D nodes. area_entered detects other areas.
When the body_entered signal fires, it includes a reference to the body itself (the body parameter), so you can call methods and properties on that body.
func _on_Area2D_body_entered(body):
print(body.name) # will print "Player1" or "Player2"
body.some_function() # calls a function on the player
I don’t really understand. I use the “func _on_Area2D_body_entered(body):” function.
Please check the images I have uploaded. And thank you VERY much for the answer.
Here is the images.
First image: showing signals to players code.
Image 2: Showing player1 code.
Image 3: Showing player 2 code.
There are a lot of basics that you need to understand in order to make things work. That link is the best way to learn them, and it even has a complete game tutorial to show you how it all goes together.
You connect a “body_entered” signal of your Area2D to itself, you can find signals by selecting your Area2D node and clicking the “Node” tab in Inspector.
In the Area2D code you now have _on_Area2D_body_entered(body): function
Assign a group “player” to player characters, or better yet, put them on a separate collision layer, and set the collision mask of the Area2D to that layer and call the player characters inner function from withing the Area2D signal function. e.g.
func _on_Area2D_body_entered(body):
if body.is_in_group("player"):
body._function_call()