0 votes

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 getinput():
velocity = Vector2()
if Input.is
actionpressed('right'):
velocity.x += 1
if Input.is
actionpressed('left'):
velocity.x -= 1
if Input.is
actionpressed('down'):
velocity.y += 1
if Input.is
action_pressed('up'):
velocity.y -= 1
velocity = velocity.normalized() * speed

func physicsprocess(delta):
getinput()
move
and_slide(velocity)`

... 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 'moveandslide()' returns a value, but this value is never used."

Can anyone please tell me how to fix this?

in Engine by (26 points)

1 Answer

0 votes

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.

by (21,313 points)

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?

Did you add the "right", "left", "up", and "down" input actions as described in the "Setup" section: https://docs.godotengine.org/en/latest/tutorials/2d/2d_movement.html#setup ?

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

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

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 Frequently asked questions and 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.