How to only check for the first collision of a KinematicBody2D

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

First, I am still working in Godot 2.1.4

I have two Kinematic Bodies one is a “monster” the other is the “Player” when they collide some calculations happen and who ever has the least amount of health “dies” and the player will get 1 point for defeating the “monster”.

But the calculations happens for the duration of the collision and the “monster” only dies when they are not colliding anymore. So the player ends up with a huge amount of points for as long as he is colliding with the monster.

How can I check for the first collision response, do the calculations, slay the monster or get slain and not wait for them to stop colliding first.

Here is my code for check if they are colliding:

if is_colliding():
   if (get_collider().has_method("is_player")):
		if (get_collider().player_size >= monster_size):
			get_node("AnimationPlayer").play("monster_die")
			get_owner().get_node("Player").points_collected += monster_value
			print("monster killed")
		else:
			print("player killed")
else:
	print("This is not the player")

Any help will be appreciated, I am still new at this so please be gentle if code or format is not up to standard :slight_smile:

:bust_in_silhouette: Reply From: camarones

If you only want to detect the first time that they collide, create an array in one of the nodes (in this case, the monster) that holds all the bodies that it is currently colliding with at the end of the step. Then in the next step, check whether the colliding body is found in the array. If not, it must have just collided.

Something like this: (I have only used Godot 3 so you may have to adjust it)

if is_colliding():
    if  (colliding_bodies.find(get_collider()) == -1):
    #if not found in the array
         if (get_collider().has_method("is_player")):
              if (get_collider().player_size >= monster_size):
                  get_node("AnimationPlayer").play("monster_die")
                  get_owner().get_node("Player").points_collected += monster_value
                  print("monster killed")
              else:
                  print("player killed")
    else:
         print("This is not the player")
#update array at the end of the step
colliding_bodies = get_colliders()