returns value but never used error.

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

hello. i am trying to learn about move_and*_collide and move_and_slide functions. i am getting 2 errors where they returns a value but never used. i would like to know why it happens and what to do to fix it.

i want to inform that everything is working. it moves, bounces and jumps. but i do want to learn about how it works but why, and solving errors is one way to improve.

here is the errors.
W 0:00:00:0681 The function ‘move_and_collide()’ returns a value, but this value is never used.

KinematicBody2D.gd:28

W 0:00:00:0681 The function ‘move_and_slide()’ returns a value, but this value is never used.

KinematicBody2D.gd:33
extends KinematicBody2D

const GRAVITY = 400.0
const WALK_SPEED = 200
const run_speed = 400

var velocity = Vector2()

var bounce_coefficent = -1

func _physics_process(delta):
	
	
	velocity.y += delta * GRAVITY
	if Input.is_action_pressed("ui_left"):
		velocity.x = -WALK_SPEED
	elif Input.is_action_pressed("ui_right"):
		velocity.x = WALK_SPEED
	else: velocity.x = 0
	if is_on_floor():
		if Input.is_action_pressed("ui_up"):
			velocity.y = -200
	
	var collision = move_and_collide(velocity * delta)
	if collision:
        var motion = collision.remainder.bounce(collision.normal)
        velocity = velocity.bounce(collision.normal)
        move_and_collide(motion)
	if collision:
		velocity.bounce(collision.normal)
		velocity = velocity.bounce(collision.normal) * bounce_coefficent
	
	move_and_slide(velocity, Vector2(0, -1))
	
:bust_in_silhouette: Reply From: kidscancode

First of all, this is not an error - it’s a warning. Warnings are information telling you that you’re doing something that may be incorrect.

In this case, move_and_slide() returns a value. An important value - from the docs:

Returns the linear_velocity vector, rotated and/or scaled if a slide collision occurred.

So when using move_and_slide() you should always capture that return value as it’s the changed velocity.

velocity = move_and_slide(velocity, Vector2(0, -1))

The other thing you’re doing wrong is moving the body twice in the same frame. Don’t use both movement methods at once. Use move_and_collide() if you need moving/bouncing. Use move_and_slide() if you need movement plus a slide collision response.

This will ensure, for example, that the gravity doesn’t accumulate when you’re moving along the ground.

i understand some of what your saying, but i dont understand how im using both at the same time. and in that case, how to fix it. do i move the move_and_slide so it dosent affect the move_and_collide.

deezy | 2019-08-29 14:04

What I mean by using multiple calls: first, you have move_and_collide(). If there’s a collision, you call move_and_collide() again, but don’t check for a returned collision from that one. And then after that, you call move_and_slide(), also failing to check for its returned value of the new velocity vector after a slide collision.

What happens if there’s a collision from the second move_and_collide() call? Why do you need the move_and_slide() at all? What if that move hits something? It’s not at all clear what you’re trying to accomplish here.

kidscancode | 2019-08-30 00:44

:bust_in_silhouette: Reply From: luislodosm

If you get this warning:

The function ‘some_function()’ returns a value, but this value is never used.

Store the value in a variable with underscore to indicate that you are not using it on purpose. Example:

var _error = get_tree().reload_current_scene()