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
in Engine by (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.

by (51 points)
selected by

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.
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 webmaster@godotengine.org with your username.