To many arguments in move_and_slide() godot 4.0.2

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

I really don’t know why but in that code:

extends CharacterBody2D

var movespeed = 500

# Called when the node enters the scene tree for the first time.
func _ready():
	pass # Replace with function body
func _physics_process(delta):
	var motion = Vector2()
	
	if Input.is_action_pressed("ui_up"):
		motion.y -= 1
	if Input.is_action_pressed("ui_down"):
		motion.y += 1
	if Input.is_action_pressed("ui_right"):
		motion.x += 1
	if Input.is_action_pressed("ui_left"):
		motion.x -= 1
		
	motion = move_and_slide(motion * movespeed)

i have that error: Parser Error: Too many arguments for “move_and_slide()” call. Expected at most 0 but received 1.

and is not just in that code, I desist of a full game beacuse of that error

( sorry my english suck )
( sorry if is a obvious code but i started like a week and i don’t know nothing about gd script)

:bust_in_silhouette: Reply From: zerowacked

Hey there! You’re doing great, don’t apologize.

You don’t pass an argument into move_and_slide() in Godot 4, which is why you’re receiving that error specifically.

I don’t believe you’ll want to perform an assignment on the final line either (don’t say motion = anything, you just need to call move_and_slide() completely by itself)

I recommend three things that should create the behavior I THINK you’re looking for:

  1. change all motion references to velocity
  2. define velocity on its own, without including move_and_slide()
  3. call move_and_slide() on its own with no assignment

It could look something like this:

extends CharacterBody2D

var movespeed = 500

    func _physics_process(delta):

    if Input.is_action_pressed("ui_up"):
        velocity.y -= 1
    if Input.is_action_pressed("ui_down"):
        velocity.y += 1
    if Input.is_action_pressed("ui_right"):
        velocity.x += 1
    if Input.is_action_pressed("ui_left"):
        velocity.x -= 1

    velocity = velocity * movespeed
    move_and_slide()

I’m pretty new myself, so I could be wrong, but give that a shot and see if it works.

IF THIS WORKS, I believe this will produce a 2D body that moves non-stop in the direction of your input, so you may also want to read the documentation for move_toward()

Good luck!