How to check if player is colliding and inputs at the same time?

:information_source: Attention Topic was automatically imported from the old Question2Answer platform.
:bust_in_silhouette: Asked By GunPoint
:warning: Old Version Published before Godot 3 was released.

I already asked this question, but never got a true answer
So, what i want to do? I have a player, and i want to check if he’s near the door. If he’s there, i want to check if the “E” key is pressed, so the player can “interact” and go through the door in another scene.
My player is a kinematicBody2d and the door is an area2d
If possible, can someone give me a code example? I can’t figure this out in GDScript
Also, how i can check if the player is colliding with one certain object? (in code)

:bust_in_silhouette: Reply From: rustyStriker

Well, you can add an Area2D to the door, which will represent the area in which the player can interact with the door, when the player clicks E check if he collides with the Area2D of the door, you can do so by using the Area2D function body_entered and body_exited or the get_overlapping_bodies to check if he is in or not.

Can you give me a code example? I know i can do it that way, but my code doesn’t work

GunPoint | 2017-11-03 17:27

Well,

func _input(event):
    var bodies = get_overlapping_bodies()
    var player_is_in = false
    for b in bodies:
            if b.is_in_group("Player"):
                          player_is_in = true
                          break
    if player_is_in and is_action_pressed("ineteract"): 
                Door_node.interact()

now, this is in the Area2D script, and the is_action_pressed part can be replaced for whatever grants you the value of the button you want in a way that if the button is pressed then it will return true(i dont remember the exact commands of the inputEvent) and you can change if so the is_action_pressed would be calculated before checking if the player is in the area(by inserting that part into an if with the is action pressed is presented as the statement

rustyStriker | 2017-11-03 18:15

Error:
Invalid call. Nonexistent function ‘get_overlapping_bodies’ in base ‘KinematicBody2D (Player.gd)’.
Looks like “get_overlapping_bodies” doesn’t exist in KinematicBody. Is there any method i can use instead of this?
(im using Godot 2.1.4)

GunPoint | 2017-11-03 19:06

Oh, right i need to check this in the area2d

GunPoint | 2017-11-03 19:07

Doesn’t work, even in the area2d script (i wanted to print the result and it didnt print)

GunPoint | 2017-11-03 19:12

does it print you anything at all?(in godot 2.1.4 there is something that you need to change in order to be able to print)

and just to make sure you did add a set_process_input in the _ready and a collision box to the area2D right?

rustyStriker | 2017-11-03 19:28

yea, i added a set_process_input(true) in the ready func and nothing
Godot 2.1.4 dont require something to print, i printed other stuff before this

GunPoint | 2017-11-04 07:54

ok, so i kinda forgot about it but did you add the player’s node to a group named “Player”?

rustyStriker | 2017-11-04 09:28

No, but instead i wrote

if b.get_name() == 'Player':
         player_is_in = true
        ...

GunPoint | 2017-11-04 15:42

Oh, now i understand.
But what do you mean by group? What is a group in Godot?
Sorry, but im new to Godot and its language

GunPoint | 2017-11-04 19:22

You put a good replacement, groups are just in order to organize things, have you tried printing every step? or placing it(with a few tweaks) in the _process instead?

rustyStriker | 2017-11-04 20:00

I added to group and nothing… Can u help me?? Nobody’s trying to help me :((

GunPoint | 2017-11-04 20:05

I did exactly what you said… and nothing

GunPoint | 2017-11-04 20:06

try placing the above code in the process, but change it so it will ask if the button is pressed first, if that doesnt work try adding print statements after the first if and in the last one to try and locate where the problem is coming from

rustyStriker | 2017-11-04 20:07

YESS, it works.
THANK YOU!

GunPoint | 2017-11-04 20:11