After a lot of troubleshooting I finally got my chunk generation to work. But, there is a problem:

When I destroy tiles in one chunk by clicking, it also destroys tiles in the other chunks! See this image

Nothing in the code posted looks like it would be causing an issue.

The only thing I can think of, and I don't think this is likely, is that the $TileMap code is getting all of the chunks. I don't think this is the issue though, because get_node (which $ is short hand for) only returns a single node, not multiple.

Maybe the code for tile destruction is being called on all three chunks? Like each chunk is running if Input.is_action_pressed("ui_mouseleft"): $TileMap.set_cellv(tile_at_mouse, -1) individually? I would look into seeing if there is any code for removing tiles being called on every chunk when it is only supposed to be called on an individual chunk.

I've been troubleshooting all day and can't fix it still.

I have a few updates and suspicions:

  1. I tried putting the mouse click tile destroy code in the Chunk Handler, but problem is, the destroy script uses world_to_map function and that can only be used with a tilemap. Same problem occurs if I do it through the ChunkHandler script.

  2. When I print(world_to_map(world_to_map(get_global_mouse_position()), it appears to be showing the correct cell coordinates for all of the hovered tiles. Yet still, it is deleting cells in unspecified coordinates.

  3. When I print both the global and local Transform2D built in variables of the chunks, first of all both their global and local transform are the same. It seems they are transformed by moving the origin to the new location(top left of each chunk), maybe they are checking mouse click positions with respect to THEIR origin, or from their perspective their origin is 0,0.

Maybe the solution is to move them and keep their origin at 0,0 somehow? I don't know how to do this, I've tried multiple ways of moving the chunks global_position =, set_global_position, set_position, translate(, global_translate(... All of them yield the same result.

How do I upload .gd files on this forum?

@Geffrey said: How do I upload .gd files on this forum?

paste the code, select it all and then under the paragraph(¶) menu/list there is a code item.

edit: You could also use pastebin, theres a plugin installed that should take the pastebin link and turn it into an embedding.

In the chunk script, try changing the code in _process to this:

func _process(delta):
	if (Input.is_action_pressed(“ui_mouseleft”):
		var mouse_position = get_global_mouse_position()
		if (mouse_position.x > global_position.x and mouse_position.y > global_position.y):
			If (mouse_position.x < global_position.x + (chunk_width*block_width) and mouse_position.y < (chunk_height*block_depth)):
				var tile_at_mouse = $TileMap.world_to_map(mouse_position)
				$TileMap.set_cellv(tile_at_mouse, -1)

The code is untested, and you might need to use ... and mouse_position.y > (chunk_height*block_depth) for the y coordinate in the third if check, but I think the code above should fix the problem.

I think the reason it is removing the tiles at each tilemap is because the code doesn’t check to see if the mouse position is within the bounds of the chunk node. I don’t know how the world_to_map function works, but if it wraps the position around until it finds a match then I could see how you could get the functionality you are seeing. Hopefully by checking to see if the mouse is within the bounds of the tilemap, it should only call the code on the tilemap under the mouse and only remove a single tile.

At least, that is what I think the issue could be. I don’t have a lot of experience manipulating tilemap nodes myself, so I could be completely and totally wrong.

Hopefully this helps!

It didn't work. When I click the middle chunk it only deletes tiles from the left chunk.

Btw, I should have mentioned the input code in any organization doesn't do anything when I click the left and right chunks. it only deletes chunks from all 3 when I click the center chunk, or with the code you presented, only the left chunk gets changed when I click the center.!

Example of what happened

@Geffrey said: It didn't work. When I click the middle chunk it only deletes tiles from the left chunk.

Hmm, I probably wrote the code for detecting the mouse wrong. I always have trouble writing stuff like that from memory.

Maybe this code will work?

func _process(delta):
	if (Input.is_action_pressed(“ui_mouseleft”):
		# Apparently this will get the mouse position local to this node. This means
		# in theory we just need to check to see if it's within a range of 0 to the width of the chunk.
		var local_mouse_position = get_local_mouse_position()
		if (local_mouse_position.x > 0 and local_mouse_position.x < chunk_width * block_width):
			if (local_mouse_position.y > 0 and local_mouse_position.y < chunk_width * block_depth):
				var tile_at_mouse = $TileMap.world_to_map(get_global_mouse_position)
				$TileMap.set_cellv(tile_at_mouse, -1)

But I don't know if that will work either. It is basically the same as the old code, just using the local coordinates instead of the global so it is a little easier to calculate with bounds.

If possible and you don't mind, could you send me a link to the project? That way I could better debug the problem and test things. That said, I totally understand if you don't want to and/or cannot share the project. It would just make it a tad easier to test things.

Btw, I should have mentioned the input code in any organization doesn't do anything when I click the left and right chunks. it only deletes chunks from all 3 when I click the center chunk, or with the code you presented, only the left chunk gets changed when I click the center.!

Interesting. So only the middle chunk was working with the previous/original code? And if I understand correctly, with the code I posted earlier when the middle chunk is clicked, the left chunk is effected? That is strange, maybe something else is causing the issue then.

Did you try testing 4 chunks or 2 chunks? I don't know if it would reveal anything, but maybe it would lead to what is causing the problem. Also, what does the node tree look like? Maybe all of the chunks except the middle chunk are accessing the wrong nodes or something.

Lol, my original code works perfectly with just change from global to local mouse position.

I don't know why I didn't try that before.

The local vs, global coordinates are very confusing to me.

Thanks for the suggestion.

Great! I'm glad it is working now :smile:

The local vs, global coordinates are very confusing to me.

You and me both! All the time I find I'm tripped up by coordinates being in the wrong space.


Edit: I switched this topic to a question, removed the [Solved] tag from the title, and marked your post as the answer so it is easier for others with the same issue to find this topic and the solution.

3 years later