Two players enter Are2D. How? Help!

FrasseFrasse Posts: 4Member

Hi. I have two players, player1 and player2.
I want that when Area2D is entered by player1 then send "area entered" signal to player1 code. And when player2 enter Area2D then area2d send "area entered" signal to player2 code. How to do that? Please help! I am a beginner

Answers

  • TwistedTwiglegTwistedTwigleg Posts: 3,525Admin

    It depends on how you want to go about it, but you could, in theory, do something like this (untested):

    Player 1 code:

    extends Node2D
    # Other code here...
    func _on_special_area_entered(area_node_entered):
        print ("Player 1 entered Area2D node!")
        # do whatever you need here...
    

    Player 2 code:

    extends Node2D
    # Other code here...
    func _on_special_area_entered(area_node_entered):
        print ("Player 2 entered Area2D node!")
        # do whatever you need here...
    

    Area2D node code:

    extends Area2D
    # Other code here...
    
    func _ready():
        # If your players are using Area2D nodes for collision
        # detection, then use the following:
        connect("area_entered", self, "_on_area_entered_area2D")
        # If your players are using RigidBody2D, KinematicBody2D, or StaticBody2D
        # nodes for collision detection, then use the following instead:
        # connect("body_entered", self, "_on_body_entered_area2D")
    
    # For Area2D nodes:
    func _on_area_entered_area2D(other_area_node):
        print ("Area with name ", other_area_node.name, " entered!")
        if (other_area_node.has_method("_on_special_area_entered")  == true):
            other_area_node._on_special_area_entered()
    
    # For RigidBody2D, KinematicBody2D, and StaticBody2D nodes:
    func _on_body_entered_area2D(other_body_node):
        print ("Collision other node whose name is ", other_area_node.name)
        if (other_area_node.has_method("_on_special_area_entered")  == true):
            other_area_node._on_special_area_entered()
    

    There are other ways to do it, but that is one way you can work around the issue. Hopefully this helps!
    (Side note: Welcome to the forums)

  • FrasseFrasse Posts: 4Member

    @TwistedTwigleg said:
    It depends on how you want to go about it, but you could, in theory, do something like this (untested):

    Player 1 code:

    extends Node2D
    # Other code here...
    func _on_special_area_entered(area_node_entered):
    print ("Player 1 entered Area2D node!")
    # do whatever you need here...

    Player 2 code:

    extends Node2D
    # Other code here...
    func _on_special_area_entered(area_node_entered):
    print ("Player 2 entered Area2D node!")
    # do whatever you need here...

    Area2D node code:

    extends Area2D
    # Other code here...

    func _ready():
    # If your players are using Area2D nodes for collision
    # detection, then use the following:
    connect("area_entered", self, "_on_area_entered_area2D")
    # If your players are using RigidBody2D, KinematicBody2D, or StaticBody2D
    # nodes for collision detection, then use the following instead:
    # connect("body_entered", self, "_on_body_entered_area2D")

    # For Area2D nodes:
    func _on_area_entered_area2D(other_area_node):
    print ("Area with name ", other_area_node.name, " entered!")
    if (other_area_node.has_method("_on_special_area_entered") == true):
    other_area_node._on_special_area_entered()

    # For RigidBody2D, KinematicBody2D, and StaticBody2D nodes:
    func _on_body_entered_area2D(other_body_node):
    print ("Collision other node whose name is ", other_area_node.name)
    if (other_area_node.has_method("_on_special_area_entered") == true):
    other_area_node._on_special_area_entered()

    There are other ways to do it, but that is one way you can work around the issue. Hopefully this helps!
    (Side note: Welcome to the forums)

    It dosen't really work. Can you please check my 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

  • TwistedTwiglegTwistedTwigleg Posts: 3,525Admin

    How is the code not working? It is hard to know what needs to be done without knowing what is going wrong.

    Looking at the pictures, the issue with the Area2D signals you are having is because both player 1 and player 2 connected to the same signal. This means that when a collision is detected on the Area2D, both players will be told there is a collision. That is why the signals are not working.
    If you plan to use the body_entered signal in the Area2D node, you will need a way for either the area to determine which player entered (and send that information through the signal) or a way for the player to know whether they collided or not.

    The code I posted above should work. You might need to disconnect the Area2D signals in the Godot editor, as they might be causing interference. Looking at the pictures, you will need to un-comment the line connect("body_entered", self, "_on_body_entered_area2D") for it to work, as the player nodes are KinematicBody2d nodes.

Leave a Comment

BoldItalicStrikethroughOrdered listUnordered list
Emoji
Image
Align leftAlign centerAlign rightToggle HTML viewToggle full pageToggle lights
Drop image/file