0
\$\begingroup\$

When I try to run the following code in Godot, I get the error:

Line 45:Identifier "delta" not declared in the current scope.

I don't know what this means or what I should do about it.

extends CharacterBody3D

const SPEED = 5.0
const JUMP_VELOCITY = 4.5
const SENSITIVITY = 0.03

#bob variables
const BOB_FREQ = 2.0
const BOB_AMP = 0.08
var t_bob = 0.0

func _physics_process(delta: float) -> void:
    # Add the gravity.
    if not is_on_floor():
        velocity += get_gravity() * delta

@onready var head = $Head
@onready var camera = $Head/Camera3D

func _ready():
    Input.set_mouse_mode(Input.MOUSE_MODE_CAPTURED)

func _unhandled_input(event):
    if event is InputEventMouseMotion:
        head.rotate_y(-event.relative.x * SENSITIVITY)
        camera.rotate_x(-event.relative.y * SENSITIVITY)
        camera.rotation.x = clamp(camera.rotation.x, deg_to_rad (-40), deg_to_rad (60))


    # Handle jump.
    if Input.is_action_just_pressed("jump") and is_on_floor():
        velocity.y = JUMP_VELOCITY

    # Get the input direction and handle the movement/deceleration.
    # As good practice, you should replace UI actions with custom gameplay actions.
    var input_dir := Input.get_vector("left", "right", "up", "down")
    var direction: Vector3 = (head.transform.basis * Vector3(input_dir.x, 0, input_dir.y)).normalized()
    if direction:
        velocity.x = direction.x * SPEED
        velocity.z = direction.z * SPEED
    else:
        velocity.x = 0
        velocity.z = 0

    t_bob += delta * velocity.length() * float(is_on_floor())
    camera.transform.origin = _headbob(t_bob)
    
    move_and_slide()
    
func _headbob(time) -> Vector3:
    var pos = Vector3.ZERO
    pos.y = sin(time * BOB_FREQ) * BOB_AMP
    pos.x = cos(time *BOB_FREQ/2) * BOB_AMP
    return pos
\$\endgroup\$

1 Answer 1

3
\$\begingroup\$

"Line 45" tells you the problem is on line #45:

t_bob += delta * velocity.length() * float(is_on_floor())

You're referring to a variable called delta here (that's the "identifier"), but there's no variable defined with that name anywhere inside this function (the "current scope").

There is one declared as an input in the _physics_process(delta) function, representing the duration of simulation time since the last physics update. We can't access that from line 45 though, because that's a completely different function executed at a different time.

It looks like you should move this movement code into _physics_process so it's called once per physics tick, not once per input. That will be the general rule for code that depends on being called at regular time intervals measured by the delta variable.

I recommend brushing up on your fundamentals about variable scopes and other core GDScript concepts so that you can read, understand, and fix these kinds of common error messages unassisted.

Questions about general programming concepts like this that are not unique to games may be closed and redirected to StackOverflow, which already has a host of Q&A explaining variable scopes in Godot.

\$\endgroup\$

You must log in to answer this question.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.