I have an Error message after copying a script.

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

Hello, everyone.

I am fairly new here. I copied and pasted this code from the documentation…

`extends KinematicBody2D

export (int) var speed = 200

var velocity = Vector2()

func get_input():
velocity = Vector2()
if Input.is_action_pressed(‘right’):
velocity.x += 1
if Input.is_action_pressed(‘left’):
velocity.x -= 1
if Input.is_action_pressed(‘down’):
velocity.y += 1
if Input.is_action_pressed(‘up’):
velocity.y -= 1
velocity = velocity.normalized() * speed

func _physics_process(delta):

… And then, I got two error messages. I’ve read that the types of warnings I’ve received were more like warnings. To fix one, I simply had to add an underscore before delta, but I still can’t get rid of this error.

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

Can anyone please tell me how to fix this?

:bust_in_silhouette: Reply From: kidscancode

First, as you can see your code is poorly formatted. When pasting code, use four spaces, or click the “Code sample” button when typing on this site.

Copy-and-paste from the web is not recommended. It can ruin formatting, including indentation, which is significant for GDScript. In addition, you learn more by typing the code yourself than by copy-and-paste.

What you quoted there is a warning, not an error. Nothing is wrong and there’s nothing that needs to be “fixed”.

I assume this is from the “Using KinematicBody2D” tutorial? To correct the warning, the code should be updated to read

velocity = move_and_slide(velocity)

In this way, the return value of the function is re-assigned to the velocity. It doesn’t really matter for this example, because there is no wall collision happening, but it is technically more correct.

More generally, warnings are there to inform, not to force changes. If you understand what the warning means, and what it means to ignore it, then it can be disregarded. However, it is highlighting something that could result in an issue down the road.

I apologize about the formatting- I will look into that more closely next time. I am also sorry for the newbie questions.

After making the change, I still cannot get my sprite to budge. The sprite is the child of the KinematicBody2D, as is a CollisionShape. I have an image as a background with everything attached to it- including the KinemaBody2D directly. (my PC isn’t letting me send a screenshot- otherwise, I would have done that.) Is there anything else I am missing here?

User457654654 | 2019-04-16 12:15

Did you add the “right”, “left”, “up”, and “down” input actions as described in the “Setup” section: 2D movement overview — Godot Engine (latest) documentation in English ?

kidscancode | 2019-04-16 16:03

I left it with A, S, D, and W. Should still work.

User457654654 | 2019-04-16 21:13

There’s a difference between actions, added in the InputMap, and keys, which can be assigned to actions. The code references action names via is_action_pressed(). You need your “A” key assigned to the “left” action, etc

kidscancode | 2019-04-16 21:23