Do signals work in tilesets?

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

I’m making a simple 2D game prototype. Player can shoot and when a bullet hits a wall it should disappear.
So I made a scene with some sprites, floor and walls. A wall is an Area2D, it emits the “body_entered” signal to itself and, in the connected function, I wrote print(str(body)) (just to see how a body is made and if I can use it for my purpose). But when I debug the game, bullets passes through the walls and nothing appear in the console.
That said, I’m wondering if signals work in tilesets.
Thank you very much!

:bust_in_silhouette: Reply From: gcivico


Actually, based on my experience, scripts not work inside Tilesets. Yo can create a scene with a TileMap (and it use Tileset) and at the same level of hierarchy of the TileMap, create an Area2D node with a CollisionShape and connect the body_entered signal to the Area2D and it will work.

Another options is, if the bullet is a KinematicBody, you can check if it is colliding using something like this:

	var collisionCounter = get_slide_count() - 1
	if collisionCounter > -1:
		# get the collision
		var col = get_slide_collision(collisionCounter)
#		if col.collider.get_class() == "TileMap":
			# maybe you want to get the collision normal to verify 
			# if it is wall or not
			# the you can call bullet.queue_free()

Another approach is create a bullet as a scene and set a Area2D for it. Then you can connect that area with the body_entered signal and use something like this:

extends Area2D

func _on_Area2D_body_entered( body ):
	if body.get_class() == "TileMap":
		# queue_free() the bullet

Thank you!
I think that first and third solutions would work, but creating all those Area2D would be hard to handle for a full game. So I went for the second one using move_and_slide(velocity) and it works!
This is (part of) my code inside the script of the Bullet scene:

func _process(delta):
	if position:
		if get_slide_count() > 0:

fago | 2018-03-02 09:57

Great !!

Just an observation… You may want to use _physics_process(delta) rather than _process(delta). Please read here and here.

gcivico | 2018-03-02 12:14

Did it! Thank you again!

fago | 2018-03-02 12:37

:bust_in_silhouette: Reply From: Guilherme Furst

Read on Using tilemaps — Godot Engine (3.0) documentation in English

tl;dr : basically Tilemaps on themselves don’t provide much behavior other than the graphics themselves, but you can create custom tile maps from scenes which embed other nodes along with the tile, for example collision, or pretty anything else godot can offer.

Thank you!
I’m new to Godot and, considering that collisions work inside tilesets, I thought that they might have had other features.

fago | 2018-03-02 11:58

That’s alright, really try to read the docs, they’re pretty helpful initially.

Guilherme Furst | 2018-03-02 12:12

Sure, thanks! :slight_smile:

fago | 2018-03-02 12:37