Check for any collision with tilemap from inside the tilemap script?

TercesTerces Posts: 3Member

I have multiple tilemaps in my map. One for the visual map, one where I draw the collisions and one where I mark areas that are deadly (such as spikes).

I would like to attach a script to the whole death tilemap, that kills whatever touches it. Sadly, this does not seem to be possible since there is no function or check that works on the tilemap and can check whether anything is touching it.

I do not want to find out if a specific tile inside a tilemap is being touched. I want that whenever anything touches any part of that tilemap, a script runs. Is there any good solution to this?

Tagged:

Tags :

Answers

  • TwistedTwiglegTwistedTwigleg Posts: 1,000Admin

    I have not done this myself, but you might be able to put a Area node around the player (or whatever dies upon collision) and then detect if what the player collided with is the TileMap using the body_entered(body) and body_exited(body) functions. Then you could do something like this, in theory:

    func body_entered(body):
        if body.is_in_group("deadly") == true:
            print ("Player collided with deadly thing!")
    

    Hopefully this helps! :smile:

  • TercesTerces Posts: 3Member

    but this is only working if I do that in the player-script or any other entity that might come in touch with any part of the tilemap, right? I am surprised that there seems to be no way to run a tilemap-script and check for any such event. In my opinion, if there are two bodies colliding, an event should be fired in both directions.

    Right now, I have accepted, that this might not be possible, so I am changing my code. But even now I am having trouble to find out, what I am colliding with from the player side.

  • TercesTerces Posts: 3Member
    edited December 2018

    my current solution for this is the following (Godot 3.1):

    motion = move_and_slide(motion, UP_ORIENTATION)
    
    if get_slide_count() > 0:
        for i in range(get_slide_count()):
            if(get_slide_collision(i).collider.get_name().find("death") != -1):
                get_tree().reload_current_scene()
    

    after moving, I check for any collisions. I loop through those collisions and if any of them contain the name "death" (which is the name of the deadly tilemap-node), I reset the scene. Later on I will fire a custom signal here and can use that to process the event in a global script. This way the code is relatively small and the same logic can be applied to different entities.

Sign In or Register to comment.