Two players enter Area2D

:information_source: Attention Topic was automatically imported from the old Question2Answer platform.
:bust_in_silhouette: Asked By Frasse

I have two players. Player1 and Player2. (both are KinematicBodies2D).
I also have a Area2D.

I want that when Player1 enters the Area2D then make the sprite not visible.
(Sprite name: g1 ($g1.visible = not $g1.visible)).
And when Player2 enters the Area2D then make the sprite not visible.
(Sprite name: g2 ($g2.visible = not $g2.visible)).

And I have connected Area2D node signal on function “body_entered” with both player1 and player2.
So my problem is how to make that when Player1 enter the Area2D then send signal to player1 code AND NOT player2. And when player2 enter the Area2D then send signa to only player2 code and not player1.

Please help.

I have been stuck in many days.

Here are pictures on Player1 code, Player2 code, Area2D code and signals.

https://ibb.co/KxmckwQ

:bust_in_silhouette: Reply From: Ternarycat

you can check collision in the player1 and player 2 scripts with unique logic, not in the area 2D.

and i think better just player1.hide() or player1.show(), it’s so more clearly

I have changed to $g1.hide()

Can you please explain more “you can check collision in the player1 and player 2 scripts with unique logic, not in the area 2D.”
This is my main problem

Frasse | 2019-11-24 13:31

:bust_in_silhouette: Reply From: Dlean Jeans

You don’t need to connect the signal to individual players.
Just add body.visible = not body.visible to this:

Area2D.gd

func _on_Area2D_body_entered(body):
  queue_free()
  body.visible = not body.visible

Then disconnect the signals to players and their handling code.

This will work for any number of players if you plan to add more in the future.

I have done that now. Thank you very much. But I still have a problem. When I write
func _on_Area2D_body_entered(body):
queue_free()
body.visible = not body.visible
Så the player disappear when it enter the Area2D. I don’t want that the player disappears. I just want that the sprite will be not visible. How can i here:
body.visible = not body.visible
specify that only sprite (whos name is g1) will be not visible?

Frasse | 2019-11-24 20:17

You’ll probably want to write a function inside the player script for that:

func hide_sprite():
  $g1.hide()

Then update the code in Area2D.gd:

func _on_Area2D_body_entered(body):
  queue_free()
  body.hide_sprite()

That’s it!

It appears that you’re writing individual scripts for each player. I recommend that you read up on Scenes and nodes and Instancing. Save one of the player scene as Player.tscn and use only one Player.gd script. Rename g1 and g2 to Sprite, g12 and g22 to Sprite2. Update hide_sprite() function:

func hide_sprite():
  $Sprite.hide()

This way you only need to write the code once for the Player scene and you can have more than 2 players without writing the code multiple times for each player.

Dlean Jeans | 2019-11-25 01:12

It comes an error. I can’t even start the game now.

Please check the image:
Ny hosted at ImgBB — ImgBB

Frasse | 2019-11-25 09:26

I searched on that error and it was error because I copied your code. I tried to write it and now it works. Thank you very much!!!

Frasse | 2019-11-25 09:45