How would I use a body_enter or area_enter signal with an Area2D to make something become visible?

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

What the title says.
I currently have this code:

extends Area2D

member variables here, example:

var a=2

var b=“textvar”

var dumbile = false
func _ready():
set_process(true)
connect(“body_enter”,self,“collison_now”)
connect(“area_enter”,self,“this_better_work”)
func collision_now():
dumbile = true
self.get_parent().show()

func stenchian( body_id, body, body_shape, area_shape ):
dumbile = true
self.get_parent().show()
func this_better_work():
dumbile = true
self.get_parent().show()
func _process(delta):
if(dumbile == false):
self.get_parent().hide()

PS I have tried the same thing on the CollisionShape2D, to no avail.

:bust_in_silhouette: Reply From: ericdl

It looks like you may have your nodes layered correctly and signals set up properly, but here’s a rundown anyway:

Area2D needs a child CollisionShape2D in order to detect object collisions:
node heirarchy

Connect the body_enter and body_exit signals, either in the editor or via code:

get_node("Area2D").connect("body_enter",self,"_on_Area2D_body_enter")
get_node("Area2D").connect("body_exit",self,"_on_Area2D_body_exit")

In the body_enter and body_exit functions, set the object visibility accordingly:

func _on_Area2D_body_enter( body ):
	if body.has_method("show"): body.show()

func _on_Area2D_body_exit( body ):
	if body.has_method("hide"): body.hide()

Example project download: https://drive.google.com/file/d/0BwnfZQAEnciARS1idGN5cjZvZ3M/view?usp=sharing

Comment for the PS: CollisionShape is just an editor helper (not an “real” active Node), it adds a shape resource on the parent, these can be added via code too (_shape methods).

eons | 2016-11-10 15:15

So on what node would I put the script? The Area2D or the body it’s colliding with?
PS When I add the CollisionShape2D to the node, it becomes visible as soon as the scene starts, I think this is because it’s on top of a RigidBody2D, is there a way to ignore that RigidBody2D in particular or ignore all RigidBody2Ds?
PSS Nvm I found a workaround to that problem, this worked thanks for the help

scovelme | 2016-11-10 17:07