0 votes

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 moveandslide, then check isonfloor. 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 onfloor 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...?

in Engine by (15 points)

1 Answer

+1 vote
Best answer

You must call update() to have your _draw() function executed and re-draw. See http://docs.godotengine.org/en/latest/tutorials/2d/custom_drawing_in_2d.html for details.

by (20,671 points)
selected by
Welcome to Godot Engine Q&A, where you can ask questions and receive answers from other members of the community.

Please make sure to read How to use this Q&A? before posting your first questions.
Social login is currently unavailable. If you've previously logged in with a Facebook or GitHub account, use the I forgot my password link in the login box to set a password for your account. If you still can't access your account, send an email to webmaster@godotengine.org with your username.