Understanding this code for movement

Godot Version

extends CharacterBody2D

var speed = 600
var player_state

func _physics_process(delta):
var direction = Input.get_vector(“left”, “right”, “up”, “down”)

velocity = direction * speed
move_and_slide()

if direction.x == 0 and direction.y == 0:
	player_state = "idle"
elif direction.x != 0 or direction.y != 0:
	player_state ="walking"

Question

I’ve copied this and remembered it off a YouTube video and the movement works fine. However, I am new to coding, and I do not have any idea what the codes means and I wish to understand it so I can grasp onto the concept stronger.

(i do understand what the line “var speed = 600” means but other than that I am lost)

I added some comments to your code to help give an idea what is happening.

extends CharacterBody2D

var speed = 600

# this variable is being used to store a string which
# represents the players state, in this case, are we idle or walking?
var player_state 

# the _physics_process gets called automatically every physics frame
# which is 60 times per second. The docs describe the loop concept here:
# [https://docs.godotengine.org/en/stable/tutorials/scripting/idle_and_physics_processing.html](https://docs.godotengine.org/en/stable/tutorials/scripting/idle_and_physics_processing.html)
# each indented line of code belongs to this method, and is called sequentially in order
func _physics_process(delta):
	# creating a new variable called "direction" which is a Vector2
	# Vector2 variables are typically used to represent 2D coordinates ( x, y )
	# The line of code is assigning the X and Y vectors from the Input as the value of "direction"
	# these values will be in ranges of -1 to 1 on each axis, with a 0 meaning no direction on that axis
	var direction = Input.get_vector(“left”, “right”, “up”, “down”)
	
	# multiply the direction vectors by our speed to get a velocity.
	# ie if direction was ( 1, 0 ) it will now become ( 600, 0 )
	velocity = direction * speed
	
	# moves the CharacterBody2D based on it's velocity. See more info in the docs:
	# [https://docs.godotengine.org/en/stable/classes/class_characterbody2d.html#method-descriptions](https://docs.godotengine.org/en/stable/classes/class_characterbody2d.html#method-descriptions)
	move_and_slide()
	
	# if statement.( "==" is like asking "True or False, are these two things equal?" )
	# if both directions are equal to 0 we know that we aren't mocing, so set the state to "idle"
	# elif means, if the first condition is false, try this other condition
	# "!=" is like asking "True or False, are these 2 things NOT equal?" "!" means "NOT"
	if direction.x == 0 and direction.y == 0:
		player_state = "idle"
	elif direction.x != 0 or direction.y != 0:
		player_state ="walking"

Let me know if that helps explain things at all :slight_smile:

Also, do you have a link to the YouTube video you were following?

Hi thank you for your time it has helped me tons, yes I do have the video link -(https://www.youtube.com/watch?v=eAEe_9jCV4s&ab_channel=DevWorm)

1 Like