Topic was automatically imported from the old Question2Answer platform.
Asked By
Cobaltiz
I want to check if the player is in the area of another node. I tried using signals but it only worked when it was entering:
func _on_Area2D_body_entered(body):
…
So instead I attached a script to the player’s Area2D:
...
onready var box_node=get_node("/root/box")
func _physics_process(delta):
if overlaps_body(box_node)==true:
print("player not in area")
else:
print("player not in area")
It didn’t work so I tried attaching a script to the box Area2D as well:
...
onready var player_inp =get_node("/root/Player")
func _ready():
pass
func _physics_process(delta):
if overlaps_area(player_inp)==true:
print("Player in are")
else:
print("Player not in area")
I also tried using “overlaps_body()” instead of area, still not working, I also tried attaching the script to the box and the player node itself(KinematicBody2D, StaticBody2D, RigidBody2D) Still not working… pls help
Your initial approach was the correct one
Won’t go into the cons of using overlaps_body() but what you could additionally do is add the body to an array on body_entered() signal then remove it on the body_exited() one, then when you need to know if that body is in the area you can with
if body in area_array:
#call the cops
For even more power you can use groups instead allowing you to query the above anywhere in your project
it works, but I want to know if the player also presses the key “e” (like an interact button). I check that in the player’s script:
var interact=false
if Input.is_action_just_pressed("interact"):
interact=true
then, in the box script, I implemented what you said and checked if interact is true:
var array1=[]
func _on_Area2D_body_entered(body):
if body.name=="Player":
array1.append(body)
if body in array1:
if player_inp.interact==true:
print("player pressed e")
func _on_Area2D_body_exited(body):
if body.name=="Player" and body in array1:
player_inp.interact=false
array1.erase(body)
Also I know this is basic stuff but I try as much as I can to stay off youtube tutorials because I want to learn stuff by myself and by reading online docs so it a bit harder to add stuff in my project.
Cobaltiz | 2022-03-26 11:21
While your dedication for self improvement is admirable try not to limit your sources to gather information.
You can check it anywhere
# Box script
if Input.is_action_just_pressed("interact"):
if player_inp in array1:
#do stuff
Or
# Player script
if Input.is_action_just_pressed("interact"):
if self in box_node.array1:
#do stuff
Another approach would be to keep your code centralized and in the Player script have a var to know which box you’re currently in and null if not in any
Wakatta | 2022-03-26 14:30
Thank you very much for responding me again. It worked perfectly.
For anyone who wants the code:
extends StaticBody2D
onready var player_inp =get_node("/root/Player")
func _ready():
pass
var array1=[]
func _on_Area2D_body_entered(body):
if body.name=="Player":
array1.append(player_inp)
func _physics_process(delta):
if Input.is_action_just_pressed("interact"):
if player_inp in array1:
#do stuff
func _on_Area2D_body_exited(body):
if body.name=="Player" and player_inp in array1:
array1.erase(player_inp)