0
\$\begingroup\$

this is the whole code

extends KinematicBody

signal health_updated(health)
signal killed()

var speed = 10
var acceleration = 10
var gravity = 0.2
var jump = 5


var mouse_sensitivity = 0.3

var direction = Vector3()
var velocity = Vector3()
var fall = Vector3() 
export (float) var max_health = 100

onready var health = max_health setget _set_health
onready var head = $Head

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

func _input(event):

    if event is InputEventMouseMotion:
        rotate_y(deg2rad(-event.relative.x * mouse_sensitivity)) 
        head.rotate_x(deg2rad(-event.relative.y * mouse_sensitivity)) 
        head.rotation.x = clamp(head.rotation.x, deg2rad(-90), deg2rad(90))

func _physics_process(delta):

    direction = Vector3()

    move_and_slide(fall, Vector3.UP)

    if not is_on_floor():
        fall.y -= gravity
onready var invulnerability_timer = $invulnerabilitytimer
func damage(amount):
    if invulnerability_timer.is_stopped():
        invulnerability_timer.start()
        _set_health(health - amount)
func kill():
    pass
func _set_health(value):
    var prev_health = health
    health = clamp(value, 0, max_health)
    if health != prev_health:
        emit_signal ("health_updated", health)
        if health == 0:
            kill()
            emit_signal("killed")

    if Input.is_action_just_pressed("jump") and is_on_floor():
        fall.y = jump


    if Input.is_action_pressed("move_forward"):

        direction -= transform.basis.z

    elif Input.is_action_pressed("move_backward"):

        direction += transform.basis.z

    if Input.is_action_pressed("move_left"):

        direction -= transform.basis.x          

    elif Input.is_action_pressed("move_right"):

        direction += transform.basis.x



    direction = direction.normalized()
    velocity = velocity.linear_interpolate(direction * speed, acceleration * delta) 
    velocity = move_and_slide(velocity, Vector3.UP) 
\$\endgroup\$
1
  • \$\begingroup\$ Is the input and velocity related code at the bottom really meant to be inside your _set_health method? Or did you want it to be part of _physics_process? \$\endgroup\$
    – DMGregory
    May 14, 2020 at 11:37

1 Answer 1

2
\$\begingroup\$

You have a lot of code in the function func _set_health which does not appear to be about health. It looks more like physics code, which would make more sense in func _physics_process.

That code also refers to a variable "delta" in this line:

velocity = velocity.linear_interpolate(direction * speed, acceleration * delta) 

But a variable by that name does not seem to exist within the scope of the function _set_health which is the reason for that parser error. However, the function _physics_process has such a variable in form of a parameter which gets passed to it. So it seems like that section of code definitely belongs there instead.

\$\endgroup\$

You must log in to answer this question.

Not the answer you're looking for? Browse other questions tagged .