How do i get the position of an object that has entered an Area2D??

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

hi i am currently making a simulation game involving creatures and i have this KinematicBody2D with the “Sight” Area2D attached and i have a piece of fruit is there anyway i can get the position of the fruit once it enters the area ( i am using on_body_entered) without it telling me via a file or something because i realised that if i want to use files i would have to have a different file for every creature and piece of fruit but then i would have to send the file path in a file and then to make that file random i would have to make yet another file etc. please let me know thanks.

:bust_in_silhouette: Reply From: mdubaisi

if you don’t know, the function OnBodyEntered gives you the body that entered, so you can tell it to set position of the body, so you type:

var position = Vector2.ZERO
if body.name == the name of the body:
    position = body.global_position

and if you want to get several positions without creating a lot of variables, you can create an array:

var positions = []
 if body.name == the name of the body:
    positions.append(body.global_position)

would this work similarly on an instanciated object?? because fruit in this case is in a different scene entirely and will be spawned in.

sorry i should have made that clearer i my original comment.

MARIOCULTLEADER | 2020-11-22 16:03

yes, it should work.

mdubaisi | 2020-11-23 05:29