Attention | Topic was automatically imported from the old Question2Answer platform. | |
Asked By | dormccar |
How to call function after collision
Help
Hello,
So I have an Enemy object (scene)
it is a kinematicBody2D with a script… in said script is a function called takeDamage(damage):
So I am checking for collision with the player using a ray, I am able to get the collision, but when I call the object it says that kinematicBody2D doesn’t have such a function (script is for sure attached to it)
So how do i correctly call a function of an object I am colliding with?
Current Version:
if ray.is_colliding():
#doing some position stuff here
var object = ray.get_collider()
if object.is_in_group("Enemy"): <-----returns true btw
object.takeDamage(damage)
Exact error message: "Invalid Call. Nonexistent function ‘takeDamage’ in base ‘KinematicBody2D’
Code of Enemy Script:
extends KinematicBody2D
export (PackedScene) var scene_export
class_name Enemy
var maxHP
var HP
var speed = 64
var tile_Size = 16
var spriteNode
var tile
var lastPosition = Vector2() #where moving from
var targetPosition = Vector2() #where moving to
#const EnemyScene = preload("res://Scenes/Enemy.tscn")
var dead = false
var playerDetected = false
# Called when the node enters the scene tree for the first time.
func _init(x, y):
maxHP = 5
HP = maxHP
tile = Vector2(x, y)
#spriteNode = EnemyScene.instance()
self.position = position.snapped(Vector2(tile_Size, tile_Size))
lastPosition.x = position.x - 8
targetPosition.x = position.x - 8
lastPosition.y = position.y - 8
targetPosition.y = position.y - 8
func _on_body_enter(body):
if body.is_in_group("Player"):
print("hit player")
takeDamage(1)
func takeDamage(dmg):
print("enemy took damage")
if dead:
return
HP = HP - dmg
spriteNode.get_Node("HPBar").rect_size.x = tile_Size * HP / maxHP
if HP <= 0:
self.dead = true
func _process(delta):
self.position = position.snapped(Vector2(tile_Size, tile_Size))
Code from Player script where I try to call function:
if ray.is_colliding():
moveState = false
position = lastPosition
targetPosition = lastPosition
var object = ray.get_collider()
if object.is_in_group("Enemy"):
#object.takeDamage(damage)
print("object collided name: ", object.name)
pass
That is strange but try changing
var object = ray.get_collider()
to
var object = get_node(ray.get_collider().get_path())
Magso | 2019-12-13 01:32
Sadly it doesn’t work either, same result
dormccar | 2019-12-13 04:47
I went and tested it myself and as Magso said it is strange because it should work. The only way I see it not working is if you are colliding with your KinematicBody2D but your script is attached to a parent of that Node, in which case you would need to do this:
var object = ray.get_collider() # Get's the KinematicBody2D
var parent = object.parent # Get's the parent/owner of the script
parent.takeDamage(damage) # Calls the takeDamage method on the owner of the script
tastyshrimp | 2019-12-13 12:54