Deleting GridMap item not working using Gdscript

Godot Version

Godot4.2.1

Question

I tried deleting already placed items on the gridmap using the set_cell_item(“coordinates”, -1) method . But it does not deletes the item on the gridmap.

1 Like

your code:

set_cell_item(“coordinates”, -1)

because you filled in the parameters incorrectly…
you have coordinates as string, do you have it declared as Vector3i() ? then don’t put it in apostrophes


void set_cell_item ( Vector3i position, int item, int orientation=0 )

Sets the mesh index for the cell referenced by its grid coordinates.

A negative item index such as INVALID_CELL_ITEM will clear the cell.

Optionally, the item’s orientation can be passed. For valid orientation values, see get_orthogonal_index_from_basis.

1 Like

well by “coordinates” i meant Vector3i(x, y, z). The part that really bugs me off is the invalid cell item . When i put valid cell item no like 0 or 1 new blocks are being placed at the said coordinate. But when invalid cell item no , in this case -1 is added there is no change in the gridmap

and how do you find the cell you want to delete? are you sure that the coordinates are correct for the specific cell to be deleted?

i used a raycast3d to find the coordinates
Like i said earlier it works perfectly fine when adding new blocks . The issue arises when clearing placed block by using invalid cell item no

could you post a screenshot of the object on the grid? just to give you an idea how it looks like

additional question:
when you create a block on the grid, is the creation via set_cell or do you just get the cell coordinates and instatiate block to cell position?

when you instantiated the block, your cell is still empty, so nothing is deleted… you just need to raycast the added object and delete it

It might have been the Raycass3D
Possible theory:
When you add the block the ray endpoint is inside the block but if you use the same method for deleting the ray endpoint is still outside the block and end up to modify the block next to it instead

1 Like

Can you provide a solution for the problem ?

as @pham150603 said, RayCast can interfere with the block and not detect the cell on the grid

but the question was whether you instantiate the block or create it via set_cell_item

the important thing is how you create the block, if you just use gridmap to get the coordinates and then you place(instantiate) the block at this position, then you just need to call queue_free on the block with RayCast because the cell on the grid is empty

2 Likes

I placed the blocks using the editor
But when using queue_free the enitre gridmap goes blank.I just want to only delete the block where my rayCast3D is colliding

then it will be a problem as @pham150603 writes … probably your RayCast is interfering

it will probably be necessary to set the collision mask for raycast to ignore the created block

the simplest way to delete if you run into that problem is to extend the coordinate a little bit but you might run into some other problem like corner access
here is an untested function from a guy who is not good at math:

delete_coordinates = [coordinates] - ((([origin raycast] - [coordinates]) / abs([origin raycast] - [coordinates])) * [unit */depend on how big or small your grid is/*])
set_cell_item(delete_coordinates, -1)

or you can use a double raycast system where the first ray marks the block as passable and the second ray can pass through and delete the block

1 Like

still Couldn’t get my block to be deleted dont know what im doing wrong .
https://youtu.be/sAZ8D3rt5yU?t=3122 , I followed this tutorial as reference

If you follow a working tutorial you should check if your code and the tutorial code are the same