This one line of code is not allowed inside an expression

Godot Version

4.2.2

Question

So I’m trying to make a 2D platformer, been watching tutorials and I’m trying to make the players movement code, it is just a simple code. But one lines “Assignment is not allowed inside an expression.”
This is what the line is:

velocity = move_and_slide(velocity, Vector2.UP, floor_normal = Vector2())

It would be appreciated if someone helped. If you need more of the code just ask me.

i think you forgot to put var before velocity, also you should rename it to player_velocity because velocity is an engine term

Well that particular error message is probably due to trying to pass floor_normal = Vector2() as an argument, but additionally if you’re using Godot 4 as it says in your post then move_and_slide doesn’t take any arguments and returns a bool (indicating whether or not there was a collision), so that line should just be move_and_slide() .

1 Like

Well at the beginning of the code I made “var velocity = Vector2()”, and since I just made it move_and_slide(), now var velocity is the problem
this is the entire code:

extends CharacterBody2D

Player properties

const GRAVITY = 800
const MAX_SPEED = 200
const ACCELERATION = 800
const FRICTION = 1000
const JUMP_FORCE = -400

var velocity = Vector2()
var is_on_ground = false

func _physics_process(delta: float) → void:
# Apply gravity
velocity.y += GRAVITY * delta

# Movement
var input_vector = Vector2()
input_vector.x = Input.get_action_strength("ui_right") - Input.get_action_strength("ui_left")

if input_vector.x != 0:
	velocity.x += input_vector.x * ACCELERATION * delta
	velocity.x = clamp(velocity.x, -MAX_SPEED, MAX_SPEED)
else:
	# Apply friction
	if is_on_ground:
		var friction_direction = -sign(velocity.x)
		velocity.x += friction_direction * FRICTION * delta
		if abs(velocity.x) < 20:
			velocity.x = 0
			
# Jumping
if is_on_ground and Input.is_action_pressed("ui_up"):
	velocity.y = JUMP_FORCE
	is_on_ground = false
	
# Move the player
velocity = move_and_slide()

# Check if player is on the ground
is_on_ground = is_on_floor()

Collision handling

func _on_Player_body_entered(body: Node) → void:
pass

func _on_Player_body_exited(body: Node) → void:
pass

velocity is already defined in the script’s base class CharacterBody2D – that’s why you don’t have to (and in fact: cannot) re-define it here! You should get an error as well. Just drop the line.

Also, as @soundgnome already mentioned above, move_and_slide returns a bool – so obviously you shouldn’t write that return value into your velocity variable of type Vector2! Just don’t.

extends CharacterBody2D

# Player properties
const GRAVITY = 800
const MAX_SPEED = 200
const ACCELERATION = 800
const FRICTION = 1000
const JUMP_FORCE = -400

var is_on_ground = false

func _physics_process(delta: float) -> void:
	# Apply gravity
	velocity.y += GRAVITY * delta

	# Movement
	var input_vector = Vector2()
	input_vector.x = Input.get_action_strength("ui_right") - Input.get_action_strength("ui_left")

	if input_vector.x != 0:
		velocity.x += input_vector.x * ACCELERATION * delta
		velocity.x = clamp(velocity.x, -MAX_SPEED, MAX_SPEED)
	else:
		# Apply friction
		if is_on_ground:
			var friction_direction = -sign(velocity.x)
			velocity.x += friction_direction * FRICTION * delta
			if abs(velocity.x) < 20:
				velocity.x = 0

	# Jumping
	if is_on_ground and Input.is_action_pressed("ui_up"):
		velocity.y = JUMP_FORCE
		is_on_ground = false

	# Move the player
	move_and_slide()

	# Check if player is on the ground
	is_on_ground = is_on_floor()
2 Likes

Alright, I’m sorry, I’ve been trying to fix it, I’m just stupid and my first time coding in awhile, but thanks a lot.

No need to be sorry, we have all been there before. :slight_smile: