0 votes

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

    add_child(area)
    area.add_child(coll)

    area.set_translation(half_size)     # center of block object
    area.set_ray_pickable(true)

    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)
    add_child(b)
    b.init()        # create_area_shape() gets called from this one
    blocks.append(b)
    update_mesh()

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...

in Engine by (192 points)
edited by

1 Answer

0 votes
Best answer

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()

    shape.set_extents(half_size)

    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.add_child(coll)
    add_child(area)

    area.set_ray_pickable(true)
    area.set_translation(half_size)

    area.connect("area_enter", self, "_on_Area_area_enter")
    area.connect("input_event", self, "_on_Area_input_event")
by (192 points)
edited by
Welcome to Godot Engine Q&A, where you can ask questions and receive answers from other members of the community.

Please make sure to read How to use this Q&A? before posting your first questions.
Social login is currently unavailable. If you've previously logged in with a Facebook or GitHub account, use the I forgot my password link in the login box to set a password for your account. If you still can't access your account, send an email to webmaster@godotengine.org with your username.