0 votes

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
asked May 18 in Engine by pronax (20 points)

1 Answer

0 votes
Best answer

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.

answered May 18 by Bornide (51 points)
selected May 19 by pronax

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.

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.