Two players enter area2D. Please help me.

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

:bust_in_silhouette: Reply From: kidscancode

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(  # 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.

AS hosted at ImgBB — ImgBB
Player2 hosted at ImgBB — ImgBB
Player3 hosted at ImgBB — ImgBB

Frasse | 2019-11-23 17:41

Please help me!

Frasse | 2019-11-24 09:15

Why did you connect the Area signal to the players? That means that whatever body enters the area, the signal will get sent to all the players.

Connect the Area2D’s body_entered signal to itself. In that you can use the code I gave you above.

If this is not making sense to you, then you are probably trying to move too fast. Have you read the official tutorial?

Step by step — Godot Engine (3.1) documentation in English

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.

kidscancode | 2019-11-24 16:21

:bust_in_silhouette: Reply From: blank

The steps to fix this:

  1. You attach a script to your Area2D.
  2. 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.
  3. In the Area2D code you now have _on_Area2D_body_entered(body): function
  4. 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"):