Code relating to the deletion of tiles runs slowly

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

I have this code which tracks if the player is hitting a specific tile from below.

Under my Player (KinematicBody2D) script:

for collision_id in get_slide_count():
	var collision = get_slide_collision(collision_id)
	if collision.collider is TileMap:
		var normal = collision.normal
		if normal == Vector2(0, 1):
			var tile_pos = collision.collider.world_to_map(position)
			tile_pos -= collision.normal
			var tile = collision.collider.get_cellv(tile_pos)
			var tile_name = collision.collider.tile_set.tile_get_name(tile)
			if "SpecialBlock01" in tile_name:
				if power > 0:
					emit_signal("collided", collision)

Under my World (Node2D) script:

func _on_Player_collided(collision):
	var tile_pos = collision.collider.world_to_map($Player.position)
	tile_pos -= collision.normal
	$TileMap.set_cellv(tile_pos, -1)

However, I’m experiencing a problem where my code works, but it there is a bit of a delay between when a character hits the block and when the block is removed. Is there a way to minimize this delay, or is the nature of what I’m trying to do not possible to be done in a faster manor.

:bust_in_silhouette: Reply From: CalmTurtle

I am not sure about your specific snip of code, but it does really look convoluted for simple collision. I recommend you break out each possible collision object into its own scene. When you need it, you can instantiate it. By doing that you will be able to more easily use collision masks, assigning each different object type its own mask.

Recommend taking a look here: Physics introduction — Godot Engine (3.1) documentation in English

The problem is that I’m collisions in a tilemap, so I don’t think I can break each object into its own scene.

Jackson King | 2019-04-09 00:02

No, but you can set each tilemap on its own collision layer and mask.

CalmTurtle | 2019-04-12 01:46

Ah, alright. Thank you, I’ll check this out.

Jackson King | 2019-04-12 22:21

I edited my answer. Each tilemap can have its own mask and layer, but as of right now, it will not differentiate between tiles on the map. Recommend you set up another tilemap layer for these pieces.

CalmTurtle | 2019-04-21 22:37