How to draw multiple lines without deleting the previous

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

Hi, I want my program to generate random numbers and draw a graph (chart) on them, but I ran into such a problem that when redrawing a frame through the update() method, my old line is deleted.
How can I draw new lines without deleting the old ones?
My current code:

var pre_y = 0
var y = 0
var pre_x = 0
var x = 0
func _on_Timer_timeout():
	rng.randomize()
	y = int(rng.randi_range(-500,500))
	x+=20
	update()
func _draw():
	draw_line(Vector2(pre_x,-pre_y), Vector2(x, -y/20), Color(255, 0, 0), 0)
	pre_y = y/20
	pre_x = x
:bust_in_silhouette: Reply From: Bornide

You could store each vector pair in a new array and store them all in an other array. Then, call the draw_line method by looping throught this array.

Here an exemple:

var lines_array= [
[Vector2(0, 0), Vector2(50, 0)], #array for the line 1
[Vector2(0, 50), Vector2(50, 50)], #array for the line 2
[Vector2(0, 100), Vector2(50, 100)], #array for the line 3
] #main array

func _draw():
for line in lines_array:
draw_line(line[0], line[1], Color(255, 0, 0), 0)

In your case, you have to add the new vectors pair in the main array from your update method.

Thanks, but how can I do this dynamically?
So that every second a new line is drawn (up or down, depending on the value) and the player sees a change in the graph in real time.

pronax | 2020-05-19 11:54