Error Message "The function 'move_and_collide()' returns a value, but this value is never used."

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

So, first of all I wanna say I’m new to Godot. I don’t know if I’m ever going to program a decent game if I begin to struggle at the beginning.

So, I got a ‘KinematicBody2D’ and I want it to be able to move up, down left and right by pressing the arrow keys on the keyboard.

Well, it’s used to work. For some reason when I changed something the error began to show and - even though I’ve manually reversed my change - it still didn’t work.

For me, the error doesn’t make any sense. Here is the code:

extends KinematicBody2D

#Movement (Key)

var speed = 300

func _physics_process(delta):
	if Input.is_action_pressed("ui_down"):
		move_and_collide(Vector2(0, speed) * delta)
	if Input.is_action_pressed("ui_up"):
		move_and_collide(Vector2(0, -speed) * delta)
	if Input.is_action_pressed("ui_right"):
		move_and_collide(Vector2(speed, 0) * delta)
	if Input.is_action_pressed("ui_left"):
		move_and_collide(Vector2(-speed, 0) * delta)

and the Error Messages are:

> 0:00:00.847   The function 'move_and_collide()' returns a value, but this value is never used.
> 0:00:00.847   The function 'move_and_collide()' returns a value, but this value is never used.
> 0:00:00.847   The function 'move_and_collide()' returns a value, but this value is never used.
> 0:00:00.847   The function 'move_and_collide()' returns a value, but this value is never used.
:bust_in_silhouette: Reply From: kidscancode

First, this is not an error message, it is a warning message.

The move_and_collide() function returns a value. It returns a KinematicCollision2D object containing information about the collision that occurred (or null if there was no collision).

What this warning is telling you is that you’re not using that value. It’s fine if you don’t care, it’s just to let you know that you may be ignoring something important.

Also, don’t worry - everyone struggles at the beginning, that’s why it’s the beginning. Your code could be simplified quite a bit. Take a look at the examples in the docs:

:bust_in_silhouette: Reply From: rahsut

First, as kidscancode has said, this is a warning message not an error.

To fix this, put a variable for the question! Like this

var _moveCollision = move_and_collide()

Putting the underscore means that you purposely are not going to use the variable. The variable name can be whatever you want it to be. Without the underscore, it means that you are planning to use the variable somewhere else (and that will give you another warning message). But, there are better ways to have your player move.

You could first set a variable in your code, for example var motion = Vector2() Then, you could set a velocity (In the physics process,
var velocity = move_and_slide(velocity)) Lastly, instead of the move_and_collide(), you could do velocity.x += (what ever number) or velocity.y += (what ever number)
Hope this helps.

To others, please by nice to me as this is my first time giving an answer on here.
Thank you! :slight_smile: