I had an idea to draw 2D lines with the draw_line()
function using the mouse clicks to capture both points for each new line.
Something like this:
My thought was to use an array to store each line as its own separate sub-array with two Vector2s. Here's what I had:
extends Control
var lines: Array
var point: Vector2 = Vector2.ZERO
var new_line = []
func _input(event):
if event is InputEventMouseButton && event.is_pressed():
if event.button_index == BUTTON_LEFT:
point = get_local_mouse_position()
if new_line.size() < 3:
new_line.append(point)
if new_line.size() == 2:
lines.append(new_line)
update()
print(lines)
func _draw():
for line in lines:
draw_line(line[0], line[1], Color.white, 0, true)
new_line.clear()
The problem is that this doesn't work. This will successfully draw a 2d line, but the lines are not persistent. Only one line ever stays on the screen at a time. Not really sure what I'm doing wrong.