Is there something incorrect w/ the following event-connection code?

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

This code is pretty straightforward; itsets a cell on the map:

    func set_cell(coord, y=0):
	var hexBody = StaticBody.new()
	var hexCollisionShape = CollisionShape.new()
	var hexMesh = MeshInstance.new()

	hexCollisionShape.shape = hexCollisionResource.instance() # Preloaded earlier in the file
	hexMesh.set_mesh(hexMeshResource) # Preloaded earlier in the file
	hexMesh.set_translation(Vector3(coord.x, y, coord.z))

    # Put it all together...
	hexMesh.add_child(hexBody)
	hexBody.add_child(hexCollisionShape)
	hexBody.owner = hexMesh
	hexCollisionShape.owner = hexMesh

    # Connect to mouse signal
    hexBody.input_capture_on_drag = true
    hexBody.input_ray_pickable = true
    hexBody.connect('input_event', self, '_highlight')

    
	self.add_child(hexMesh)

func _highlight(event):
    print('This tile has been clicked')

… yet nothing is printed out upon clicking around in the screen (after many tiles have been generated). I believe all of my settings are correct (I’ve checked my Project settings, and even though input_ray_pickable is true by default, I set it to true just to make sure).

Am I doing something incorrectly?

:bust_in_silhouette: Reply From: Zylann
hexCollisionShape.shape = hexCollisionResource.instance() # Preloaded earlier in the file

The shape property must be assigned an object of type Shape.
instance() is a function only found on PackedScenes, which returns the root node of an instance of that scene. It does not return a Shape.

If your goal was to have the collision shape be in its own scene file, you should not create the CollisionShape yourself, because instance() will return it already.

If hexCollisionResource is actually a Shape resource, then you don’t need instance(). Godot should have printed an error.

Ah! Indeed you pinpointed the problem exactly, thank you. I changed the .tscn to .res (probably doesn’t matter, but it fits more), maintained the call to instance() but removed the line which set the shape manually. Everything’s now working as intended. Thank you. :slight_smile:

AmagicalFishy | 2020-03-01 18:45