+8 votes

How to draw a line in 2D?

enter image description here

in Engine by (345 points)

5 Answers

+10 votes
Best answer

The CanvasItem node has the function draw_line(...). Add a CanvasItem node to your project and attach a script with this code added to it:

func _draw():
    draw_line(Vector2(0,0), Vector2(50, 50), Color(255, 0, 0), 1)

This creates a red diagonal line in the top-left corner. Note that draw_line(...) function must be put in the _draw() function. To call the _draw() function again, call update().

by (156 points)
selected by
0 votes

use: func _draw() instead

by (429 points)

oh, dont see the answer above :)

+6 votes

Function _draw() is executed only once. How to make the function run forever?

by (345 points)

function _draw() is executed *automatically* only once. It is then executed whenever the node calls update(). If you want _draw() to be called forever, then do the following:

  1. In ready(), add the line "set_process(true)". This line will allow you to do something every iteration of the game loop.
  2. Add function _process(delta). This is the function that is called for every iteration of the game loop. In this function, add "update()"

In summary:

extends Area2D

func _ready():
    set_process(true)

func _process(delta):
    update()

func _draw():
    draw_line(Vector2(0,0), Vector2(50, 50), Color(255, 0, 0), 1)

Thanks. Now I try to draw a curved line on the coordinates of the list ...

Add update() at the end of func _draw()

+2 votes

Solved

var draw_list = []

func _ready():
    set_fixed_process(true)
    set_process(true)
    draw_list = [Vector2(0, 0), Vector2(250, 10), Vector2(50, 300), Vector2(500, 500)]

func _fixed_process(delta):
    pass

func _process(delta):
    update()

func _draw():
    if draw_list != []:
        var temp_draw_list = []
        for ob in draw_list:
            temp_draw_list.append(ob)
            if temp_draw_list != []:
                if temp_draw_list != draw_list:
                    draw_line(temp_draw_list[temp_draw_list.size()-1], draw_list[temp_draw_list.size()], Color(1.0, 1.0, 0.5, 1.0), 3)
            print(ob)
by (345 points)
edited by
+3 votes

A Line2D node type was added in 3.0

https://docs.godotengine.org/en/3.1/classes/class_line2d.html

In the example below $MyLine is a reference to a Line2D on my scene. from and to are Vector2D:

func createLine(from, to):
  $MyLine.add_point(from)
  $MyLine.add_point(to)

func removeLine():
  $MyLine.points = []
by (20 points)
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 Frequently asked questions and 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 [email protected] with your username.