Can you connect a function to a Tilemap's collision?

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

I’m working on a project and it’s going super smoothly, but I ran into one problem I can’t seem to solve without doing super tedious collision drawing.

The problem is that I made an auto tileset to put down thorny vines. I enabled and set the built-in collisions for the auto tilesets and put them on a “hurtingTile” collision layer. The only problem is that now I pretty much just have a new wall tileset that looks like thorns.

What I wish for is the tileset’s collision to be the cause of a “HURT” state, but tilemaps don’t have any built-in collision signals.

As a temporary workaround, I made an area2d node with a ton of separate collision shapes that work as static enemies that can hurt the player. This method is functional, but the only problem is that this is not very neat and fills up the tree with a ton of unoptimized nodes.

What I’m asking is if there is anything that I can use that would,

if colliding ----> Get Collision ----> if collision = Player ----> Player.state = HURT

all built into the built-in auto tilemap collisions.

Thank you :slight_smile:

if your character can only move to one position per tile, the center one
like this: gotm.io | gotm.io

then collision can be simplified a lot… instead of adding an area2d and collision shapes and doing a raycast in a direction you can simply get the tile in any space beside the character and check if it is a wall tile or a blocking tile or damage tile
but if you can can move anywhere on the tile then you need raycasts and physics i guess

your probably know this and are using physics for a reason but just fyi maybe

rakkarage | 2020-08-12 16:52

Why not give the tile map a group, call it “hurt” or something, and then when the player collides with something it can check the collider’s group to see if it has that group. The tilemap won’t be moving, so it’s not like it needs to check collisions itself. You’ll need more than one tilemap, I think, but that should be OK. If we’re off base here, please give us some more details so we might be able to help.

tuon | 2020-08-12 22:25

That sounds pretty spot on. How would I go about giving the tilemap a “group”?

Rabbit_Soup | 2020-08-13 13:18

You can add a group to a node in the Node tab. It’s a sub-tab next to Signals. See this tutorial in the docs for a screenshot and some more instructions.

tuon | 2020-08-13 16:30

Well it took a few hours of trial and error, but I finally got it to work :))))
Thank you for the help :))

this is pretty much what I used (I use move_and_slide)

func _physics_process(delta):
	var slide_count = get_slide_count()
	if slide_count:
		var collision = get_slide_collision(slide_count - 1)
		var collider = collision.collider
		if collider.is_in_group("hurt"):
			queue_free()

Rabbit_Soup | 2020-08-13 22:59