Line2D not appearing when created in GDScript

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

Hi there. I’ve been investigating this for a little while but still have no idea what the difference is between these two methods of creating and displaying a Line2D:

  • Creating a Line2D as a sub-node of the base Node2D in the Godot GUI.
  • Attaching a script to the base Node2D and creating the Line2D within its _ready() function. The GDScript code in question:


func _ready():
    var line =
    line.add_point(Vector2(40, 40))

Only the former of these works and displays a line. What am I doing wrong in the second method?

As a side note I’ve also been unable to draw a line using draw_line() from within the base Node2D _draw() function:


func _draw():
    draw_line(Vector2(0, 0), Vector2(40, 40), Color.AQUA)

Thanks for the help

P.S. I’m not sure why the code block isn’t indenting the lines after line one.

Edited to fix code formatting (via the { } button). However there is some code block rendering bug if the block immediately follows a bullet list. My fix for that has always been a lone . just before the code block…

jgodfrey | 2023-04-28 22:30

:bust_in_silhouette: Reply From: jgodfrey

I expect you need at least 2 points in the point array to draw anything. Adding a second point (0,0) works for me. And, a Line2D node added via the editor works the same way. It doesn’t draw unless it has at least 2 points…

Right, that seems to fix it. I’m not sure why I assumed that it would add an initial point by default. Thanks!

Now to figure out why draw_line() still doesn’t work…

MRIG | 2023-04-28 22:40