First of all is in the line:
set_cell_item(clickLocation.x, clickLocation.y, 3, selectedObject, 1)
you always send 3 as the Z coordinate. In Godot the Y axis is up so unless your grid is oriented "up" and you are certain you want to work on the third level of the grid map you can replace your code to this one set_cell_item(clickLocation.x, 3, clickLocation.y, selectedObject, 1)
If however your grid is oriented up there is still two problems with the way you determine where the user have clicked on your grid. the mouse position in the viewport is an absolute value that is not correlated to the scene the 3D camera is looking at.
In order to get the correct grid cell the user is pressing on you need to send a ray from the camera through the mouse position in the viewport and test where does the ray intersect and with what.
The first way of doing it is adding for each of your meshes in the MeshLib a collision body like seen here. then when the user clicks the mouse run this code to get the object you clicked on:
# cast a ray from camera at mouse position, and get the object colliding with the ray
func get_object_under_mouse():
var mouse_pos = get_viewport().get_mouse_position()
var ray_from = $Camera.project_ray_origin(mouse_pos)
var ray_to = ray_from + $Camera.project_ray_normal(mouse_pos) * RAY_LENGTH
var space_state = get_world().direct_space_state
var selection = space_state.intersect_ray(ray_from, ray_to)
return selection
After you have the object the user clicked on convert the objects translation to grid coordinates and then you can remove the magic 3 number from your code and replace it with the coordinates you get.
The second method is a little harder to understand and maintain but you can use it if for some reason you don't want collision on your meshes.
Instead of finding any intersection the ray has with the world you can test the intersection of the ray with a Plane object. Assuming that the center of meshes in your GridMap has Y value of zero you can create a plane with Y value zero like so var plane = Plane(Vector3.UP, 0)
and then replace the lines:
var space_state = get_world().direct_space_state
var selection = space_state.intersect_ray(ray_from, ray_to)
With this line that calculates the 3D coordinate in the world where you ray intersects with the plane:
var intersection = plane.intersects_ray (ray_from, ray_to )
I would recommand the first approch especilly if you are just prototyping because you can get it to work in a couple of minutes.
Good luck, hope this help you.