Topic was automatically imported from the old Question2Answer platform.
Asked By
SoftuGatsu
I’m new to programming (i only know the basics of python) and i watched a tutorial on how to make a 2d platformer character move, i blindly copied these lines of code and it worked, but i still have some questions:
1- can someone explain what each of these functions do ? (and please make it as beginner friendly as possible)
2- why aren’t these functions called ?
3- i have a rough time understanding the delta part and the gravity * delta …
extends KinematicBody2D
var speed = 400
var jump_speed = -600
var gravity = 1500
var vel = Vector2.ZERO
func get_input():
vel.x = 0
if Input.is_action_pressed("right"):
vel.x += speed
if Input.is_action_pressed("left"):
vel.x -= speed
func _physics_process(delta):
get_input()
vel.y += gravity * delta
vel = move_and_slide(vel, Vector2.UP)
if Input.is_action_just_pressed("jump"):
if is_on_floor():
vel.y = jump_speed
I strongly recommend you read the official tutorial, which explains many of these things:
It will give you a good foundation for how the engine works and how things are put together.
As for your questions:
Presumably the tutorial explained what those functions do. They’re getting the user’s input, and using that to move the body. Since it’s a KinematicBody2D, it has rules and built-in functions for how it moves.
get_input() is called - it’s the first line of _physics_process(). As for how _physics_process() is called, that’s explained in the above link. It’s a built-in function that’s called automatically by the engine every frame.
delta is the frame time. It’s the duration of one frame - typically 1/60 second. You use it in any time-based calculations, so that movement is consistent. This comes from the basic kinematic equations of motion, from beginner-level physics. This might help with that: