Better way to find an overlap between two bodies

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

In order to find if a body is over another I am using the following code:

overlaps_body(get_parent().get_parent().get_node("Character/KinematicBody2D"))

Although it works, it doesn’t seem the most practical way to do it. All those references to the parent node seem wrong and dependent on the structure of the tree of the project. Is there another way to do it?

:bust_in_silhouette: Reply From: Vladislav Vorobiev

You can export variable to set it up in editor rather than in code

export(NodePath) var char_nodepath

var char_node

func _ready():
  char_node = get_node(char_nodepath)

func check_overlap():
  if overlaps_body(char_node):
    print("yesh!")

Now press on node that has this script attached and look in Inspector: you will see category named Script Variables with field named Char Nodepath. Click on field and press “assign”, then choose your Character/KinematicBody2D node.