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


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

in Engine by (196 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()


    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")
by (196 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 Frequently asked questions and 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 [email protected] with your username.