What is move() and why I can use it

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

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.

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.

Millard | 2020-09-27 16:11

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.

Millard | 2020-09-27 16:15

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

Ertain | 2020-09-27 16:38

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

Millard | 2020-09-27 16:54

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.

Shortanel | 2020-09-27 18:58

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.

Shortanel | 2020-09-27 19:02

:bust_in_silhouette: Reply From: njamster

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.

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:

Amazon.com

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.

Shortanel | 2020-09-28 12:53

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.

njamster | 2020-09-28 14:11

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

Shortanel | 2020-09-28 15:15