+1 vote

hello. i am trying to learn about moveand*collide and moveandslide 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 'moveandcollide()' returns a value, but this value is never used.

KinematicBody2D.gd:28

W 0:00:00:0681 The function 'moveandslide()' 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))
in Engine by (39 points)

2 Answers

+2 votes

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.

by (20,381 points)

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 moveandslide so it dosent affect the moveandcollide.

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.

+1 vote

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()
by (211 points)
Welcome to Godot Engine Q&A, where you can ask questions and receive answers from other members of the community.

Please make sure to read How to use this Q&A? before posting your first questions.
Social login is currently unavailable. If you've previously logged in with a Facebook or GitHub account, use the I forgot my password link in the login box to set a password for your account. If you still can't access your account, send an email to webmaster@godotengine.org with your username.