Getting "Invalid call. Nonexistent function 'length' in base 'bool'." when following a tutorial from 3.1 in godot 4.0

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

Hi! I’m following HeartBeasts tutorial on smooth top-down movement, trying to get the same results. However, I’m using godot 4.0 and the tutorial is from 3.1 so I am getting an error: Invalid call. Nonexistent function ‘length’ in base ‘bool’.

I’m having trouble understanding what this errior means and why it’s there as I have copied his code from the video and I am very new to this. Help?

My code:

extends CharacterBody2D
    
    @export var max_speed = 500
    @export var ACCELERATION = 2000
    var motion = Vector2.ZERO
    
    
    #==========================================================================
    func _physics_process(delta):
    	var axis = get_input_axis()
    	if axis == Vector2.ZERO: 
    		apply_friction(ACCELERATION * delta)
    	else:
    		apply_movement(axis * ACCELERATION * delta)
    	
    	motion = move_and_slide() 
    	
    	
    #==========================================================================
    func get_input_axis():
    	var axis = Vector2.ZERO
    	axis.x = Input.get_action_strength("ui_right") - Input.get_action_strength("ui_left")
    	axis.y = Input.get_action_strength("ui_down") - Input.get_action_strength("ui_up")
    	return axis.normalized()
    	
    	
    #==========================================================================
    func apply_friction(amount):
    	
    	if motion.length() > amount:
    		motion -= motion.normalized() * amount
    	else:
    		motion = Vector2.ZERO
    		
    		
    #==========================================================================
    func apply_movement(acceleration):
    	
    	motion += acceleration
    	if motion.length() > max_speed:
    		motion = motion.normalized() * max_speed
:bust_in_silhouette: Reply From: jgodfrey

In Godot 4, move_and_slide() returns a bool. So, your problem is here:

motion = move_and_slide() 

Even though motion started out as a Vector2, in the above code, it’s assigned a bool value. That should probably just be:

move_and_slide() 

Check out the docs for details.