Hi, I'm trying to draw a line by using ImmediateGeometry. I found that if I draw the line after queue_free() the previous ImmediateGeometry node and regenerate a new one. The positions that the previous line had is still counted in. I'm sure I put the clear() function before beginning to draw. How to clear previous data when drawing a new line?

Godot version: v3.4.4.stable.arch_linux

extends Spatial

var vextex_pos: Vector3 = Vector3(-5, 0, 0)
var vertexs: Array
var spam = null
export var ball_node: NodePath = ""

var ball: Spatial = null
var geo: ImmediateGeometry

func _ready():
	ball = get_node(ball_node)
	create_geo()
	
func draw_mesh(geob: ImmediateGeometry, vextex_pos: PoolVector3Array):
	
	geob.clear()
	geob.begin(Mesh.PRIMITIVE_LINE_STRIP)
	
	for i in vextex_pos:
		geob.add_vertex(i)

	geob.end()

func create_geo():
	geo = ImmediateGeometry.new()
	add_child(geo)
	
	spam = SpatialMaterial.new()
	spam.albedo_color = Color.red
	spam.vertex_color_use_as_albedo = true
	spam.flags_unshaded = true
	geo.material_override = spam
	
	vertexs.push_back(ball.translation)

func _on_MoveButton_pressed():
	ball.translation.x += 1
	vertexs.push_back(ball.translation)
	
	draw_mesh(geo, vertexs)

func _on_ChangePosButton_pressed():
	var temp: Array = []
	
	for i in vertexs:
		temp.push_back(Vector3(i.x, i.y, i.z +0.2))
	
	vertexs = temp
	draw_mesh(geo, vertexs)

func _on_ClearLineButton_pressed():
	geo.clear()
	geo.queue_free()
	
func _on_CreateLineButton_pressed():
	create_geo()
  • You have to remove the ball positions from the vertexs array at some point. It looks like they are staying in the array and thus are being drawn forever. You can clear the array with:

    vertexs.clear()

    Or remove just the start of the line with this (call it as many times as you want, up to the size of the array):

    vertexs.pop_front()

You have to remove the ball positions from the vertexs array at some point. It looks like they are staying in the array and thus are being drawn forever. You can clear the array with:

vertexs.clear()

Or remove just the start of the line with this (call it as many times as you want, up to the size of the array):

vertexs.pop_front()

    Also, nitpicking but plural of vertex is vertices. Just like plural of index is indices.

      cybereality

      OK, I add vertexs.clear() before queue_free() and it works fine now, thank you very much!