_draw doesn't read values set by _physics_process correctly. Bug or am I doing something wrong?

:information_source: Attention Topic was automatically imported from the old Question2Answer platform.
:bust_in_silhouette: Asked By Anders Elfgren

I have a fairly simple platformer script. When I press space I jump, and I’d like to draw a debug line to indicate that this is possible.

To do this, I call move_and_slide, then check is_on_floor. I assumed that this would return true if I have a positive y-velocity (i.e. downwards) and the character stands on something, but it needs to pass a certain threshold. So I have a timer that I set to 0 whenever I’m on the floor and then I set on_floor = true if the timer is < 0.2. Fair enough. And it seems to work from a mechanics PoV. But my debug render doesn’t work.

extends KinematicBody2D

export var GRAVITY = 500
var velocity = Vector2(0,0)
var time_since_on_floor = 1
var on_floor = true

func _ready():

func _physics_process(delta):
    velocity += Vector2(0, GRAVITY * delta)

    var right_input = Input.is_action_pressed("right")
    var left_input = Input.is_action_pressed("left")
    var jump_input = Input.is_action_pressed("jump")
    velocity.x *= 0.9
    if right_input:
        velocity.x =150
    if left_input:
        velocity.x = -150

    move_and_slide(velocity, Vector2(0, -1))

    time_since_on_floor += delta
    #var test = test_move(get_global_transform(), Vector2(0, 2))
    if is_on_floor():
        time_since_on_floor = 0

    on_floor = time_since_on_floor < 0.2
    if on_floor:
        velocity.y = 0
        if jump_input:
            velocity.y = -250
            on_floor = false
            time_since_on_floor = 1 #hack

    if Input.is_action_pressed("exit"):

func _draw():
    if time_since_on_floor < 0.5:
        draw_line(Vector2(10, 20), Vector2(20, 50), Color(1, 0, 1))

    draw_line(Vector2(20, 20), Vector2(10, 50), Color(1, 1, 0))

in _draw, I’ve tried both if checking on_floor and the above, and neither really works. This one “works” if I check if the timer is < 1, but that’s obviously not correct, it needs to match gameplay.

(To be clear - I get the yellow line that I’m always printing but not the purple one).

I assume it’s some kind of threading issue but I thought GDScript was single threaded…?

:bust_in_silhouette: Reply From: kidscancode

You must call update() to have your _draw() function executed and re-draw. See Custom drawing in 2D — Godot Engine (latest) documentation in English for details.