0 votes

So i created a Tilemap with two different Tilesets.
One is the "floor" Tileset and the other is the "danger" Tileset.
Now i want to detect when my Player (KinematicBody2D) collides with the "danger" Tileset so he will loose 1 life f.e.
And later on i want to add new Tilesets "jump" to make him jump higher and so on...

I managed to do it with a new node (Area2D -> Sprite (added the "danger" tile .png) -> CollisionShape2D ) and added a script to the Area2D to detect collision.
But this process seems way to complex and when i want to add new tilesets in the future this would take way to much space.

hope i made my question clear :) i really appreciate any answers
thank you in advance

asked May 2 in Engine by sprengerjan (22 points)

1 Answer

+1 vote
Best answer

┬┐Do you necessary need to put your "danger" tileset inside that TileMap?
I think the better approach is:

Scene
(TileMap)TileWorld --> Tilset1, Tileset2... TilesetN

Scene
(TileMap)TileDanger --> Tileset1, Tileset2... TilesetN

Add this last tilemap into a "danger" group

Then add the damage logic inside the player

func damage() -> void:
    for i in get_slide_count():
        var collision = get_slide_collision(i)
        if collision.collider.is_in_group("danger"):
            die()

check in documentacion getslidecount() and getslidecollision() so you can understand what is doing (and maybe improve the code)
"die()" is whatever func you want to implement

whit this you can instanciate this scenes in any other scene with your player
Scene
Node
Node
Player
TileMapDanger
TileMapWorld

answered May 2 by Omar13 (164 points)
selected May 2 by sprengerjan

Thanks a lot, so i made my tiles in a new Scene and everything works fine. One problem occured, maybe you can help me with it.
I added this to my player code

for i in get_slide_count():
    var collision = get_slide_collision(i)
    var danger = get_parent().get_node("Danger")
    if collision.collider == danger:
        get_tree().change_scene("res://World1.tscn")

My function now looks like this.
I have 2 scenes, World1 and World2 where i instanced the Danger tiles, the World tiles and my Player.

how do i write the last line:

get_tree().change_scene("res://World1.tscn")

so that it changes to the root node of the current scene.
So when i collide in World1 with my Danger tiles my World1 scene reloads and
when i collide in World2 with my Danger tiles my World2 scene reloads.

thank you very much for your answer

make an export var (export var allow you to change that var from editor)

export var next_level := ""

...
...

get_tree().change_scene(next_level)

so in the world1 you can modify your player on editor (editable children) and put res://World2.tscn on the editor
on world2 same thing but res://World1.tscn

thank you very much Omar, that really helped a lot and gave me a tool to work with other things that bothered me :)
grazie mille

Welcome to Godot Engine Q&A, where you can ask questions and receive answers from other members of the community.

Please make sure to read How to use this Q&A? before posting your first questions.