0 votes

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?

asked Mar 1 in Engine by AmagicalFishy (15 points)

1 Answer

+1 vote
Best answer
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.

answered Mar 1 by Zylann (27,004 points)
selected Mar 1 by AmagicalFishy

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

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.