0 votes

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!

asked Nov 23, 2019 in Engine by Frasse (16 points)

2 Answers

0 votes

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
answered Nov 23, 2019 by kidscancode (16,476 points)

I don't really understand. I use the "func onArea2Dbodyentered(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.

https://ibb.co/HP8Kw57
https://ibb.co/4R9NKpV
https://ibb.co/CMzQMDj

Please help me!

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?

https://docs.godotengine.org/en/3.1/getting_started/step_by_step/index.html

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.

0 votes

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 onArea2Dbodyentered(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 onArea2Dbodyentered(body):
    if body.isingroup("player"):
    body.functioncall()

answered Nov 25, 2019 by blank (130 points)
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.