0 votes

Hello guys I'm trying to learn movement and I came across an auto-move script from the book I'm learning, but I have a problem:

extends Sprite

export var speed  = 100
var action_move_up = InputEventAction.new()

func _ready():
    action_move_up.action = "move up"
    action_move_up.pressed = true

func auto_move():
    Input.parse_input_event(action_move_up)

func _fixed_process(delta):
    if Input.is_action_pressed("move_up"):
        move(Vector2(0,-1)*delta)

It says that the method move() isn't declared in the current class. I search the internet and I found people that use move() method without declared it and it works. What I'm doing wrong? I really appreciate an answer...Thank you in advance.

in Engine by (47 points)

I think the problem is that the script is attached to a sprite node. Sprite nodes don't have move functions, or at least i don't think they do. The sprite should probably be parented to a kinematic body 2d, and the script should be attached to that.

also, I'm not sure if the move() function is still used, because i can't find it anywhere on the docs. Someone please correct me if I'm wrong.

The move() function sounds like KinematicBody2D code from the 2.1.x version of the engine.

What is calling the move_up.action? this is AI, right?

Is calling an Input Map "move up" that I created and where I put the UP arrow and the "w" key. In theory this code do is that whenever I press the W or up arrow the sprite will go up automatically. I think this visualise a little the code... and sorry for bad english.

The book from I work said that is for the 3.1 build. I think that it shouldn't be that different from 3.2.

1 Answer

+2 votes
Best answer

What I'm doing wrong?

You're using a method that doesn't exist in Godot 3.X anymore.

The book from I work said that is for the 3.1 build.

Then that book is wrong! The move()-method was last used in 2.1!

Also as Millard already pointed out correctly in the comments, Sprite-nodes never had a move()-method - so your script is doomed to fail under any Godot version!

I search the internet and I found people that use move() method without declared it and it works.

In general, it's a good idea to link your sources. As of now I just know you read some book and found some people on the Internet that do it the same way.

by (10,423 points)
selected by

In general, it's a good iIndea to link your sources. As of now I just
know you read some book and found some people on the Internet that do
it the same way.

Sorry for that this is the book from where I learn:

https://www.amazon.com/Godot-Engine-Development-Hours-Yourself-ebook/dp/B07BFDQFL9

I read that was made by one of the creators of Godot

Also as Millard already pointed out correctly in the comments,
Sprite-nodes never had a move()-method - so your script is doomed to
fail under any Godot version!

Yeah, I looked better and the people that i looked for answers they used a Kinematic body 2d.

So just out of curiosity what is the best mode to do the automove script?
thank you for answer and sorry again if i upset you in some way.

sorry again if i upset you in some way.

No worries, you didn't! In fact: I'm sorry if it came across any different!

I read that was made by one of the creators of Godot

That's correct. Ariel Manzur is one of the project's founders and George Marques is the current maintainer of GDScript. I don't own their book, so I cannot check if they really state that one can use a move()-method without declaring it first in Godot 3.X, but if they do then that's simply not true! Consider contacting either of them e.g on Twitter or Discord: I'm sure they will be glad to find out about the mistake!

So just out of curiosity what is the best mode to do the automove script?

In Godot 3.X there are now move_and_collide (docs) and move_and_slide (docs).

As you're multiplying the up-vector with delta in your example, you should replace the call to move with a call to move_and_collide (and change the script to extend a KinematicBody2D instead of a Sprite) and everything should work fine.

move_and_slide does the multiplication with delta implicitly and is slightly more advanced as it includes a more refined collision response: where a character using move_and_collide will just stop when colliding with an obstacle, it would slide along that obstacle when using move_and_slide which is often useful.

Thank you so much for your help and patience :) with me. I'll try this to see how it works.Have a nice day!

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.