[Godot 2.1.4] Area created through code not working

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

Trying to create an area that covers the extents of a block mesh, so I can detect mouse clicks on it or other overlapping blocks. The following code is where the area is created. I can’t figure out what’s wrong with it. There’s no errors or warnings, it’s just not working as expected.

func create_area_shape(half_size):
	print("creating area")
	area = Area.new()
	var coll = CollisionShape.new()

	var shape = BoxShape.new()          # running game with collision shapes
	shape.set_extents(half_size)        # enabled, shows that this is there
	coll.set_shape(shape)               # and correctly placed


	area.set_translation(half_size)     # center of block object

	area.connect("area_enter", self, "_on_Area_area_enter")		    # does nothing
	area.connect("input_event", self, "_on_Area_input_event")	    # does nothing

The signal functions, which end up never getting called:

func _on_Area_area_enter(other_area):
    print("%s entered &s" % [other_area.get_name(), get_name()])

func _on_Area_input_event( camera, event, click_pos, click_normal, shape_idx ):
    print("touching block area: %s" % [get_name()])

If it makes any difference, the block is already added as child when this function gets called, but it all happens in the same frame:

func create_block(verts):
	var b = pre_block.new( next_block_id(), verts, mesher)
	b.init()        # create_area_shape() gets called from this one

On further testing I noticed both area.connect(...) functions return 0, which I think it means no errors (if I connect them more times they return 31 every time except the first).

Also, if I do this right after connecting…

print( area.is_connected( "area_enter", self, "_on_Area_area_enter" ) )

… it returns true.

So the signals are connected, so I guess the problem may be with the CollisionShape or the BoxShape, but I can’t figure it out…

:bust_in_silhouette: Reply From: woopdeedoo

Found the problem. There was a crucial line missing: area.add_shape(shape). So the following code is working:

func create_area_body(half_size):
	print("creating body")
	area = Area.new()
	var coll = CollisionShape.new()
	var shape = BoxShape.new()


	area.add_shape(shape)  # <-- makes the shape work!
	coll.set_shape(shape)  # <-- not needed to work, but needed to make the collision shape 
                             #   visible when debugging (using 'Visible Collision Shapes')



	area.connect("area_enter", self, "_on_Area_area_enter")
	area.connect("input_event", self, "_on_Area_input_event")