Parser Error: Method 'get_collider' is not declared in the current class.

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

Hi. Can anyone help me with this error? I don’t really get it. Here is a snippet:

func _input(event):
	if(event.is_action_pressed("interact")):
		print(canInteract)
	if(event.is_action_pressed("interact") and canInteract):
		print(self.get_collider())
		canMove = false

func movePlayer():
	velocity = Vector2()
	if Input.is_action_pressed("DOWN"):
		velocity.y += 1
	if Input.is_action_pressed("UP"):
		velocity.y -= 1
	if Input.is_action_pressed("LEFT"):
		velocity.x -= 1
	if Input.is_action_pressed("RIGHT"):
		velocity.x += 1
	velocity= velocity.normalized() * speed

func _physics_process(delta):
	if (canMove == true):
		movePlayer()
		velocity = move_and_collide(velocity) 

it extends KinematicBody2D ofc.

:bust_in_silhouette: Reply From: Dlean Jeans

KinematicBody2D doesn’t have a get_collider() function.
And move_and_collide() returns not a Vector2 but a KinematicCollision2D which has a collider property.

var collision:KinematicCollision2D

func _physics_process(delta):
    if canMove:
        movePlayer()
        collision = move_and_collide(velocity)
        if collision:
            print(collision.collider)

Thanks. And what if I want to use this in the input func?

I am slowly understanding gdscript, coming from c++.
edit: I am also getting "Invalid get index “collider” (on base:‘null instance’) error now

szaross | 2019-07-01 20:52

Try this in your _input:

if collision:
  print (collision.collider)

Dlean Jeans | 2019-07-01 21:13

it works!
thanks a lot

szaross | 2019-07-02 15:30