• 2D
  • How can I use non-static user draw functions in other script?

Let me explain what I mean. If I have static function in any script that extends CanvasItem like this:

static func DrawLine():
	draw_line(Vecor2(), Vector2(100, 100), Color.white)

That don't works because draw_line isn't static. Ok, if that function isn't static it works only in script that contains this function in _draw() method. But I want use this function in other node, child node for example. If I just write get_parent().DrawLine() it will throw exception like draw functions can be used only in _draw() method. Is it possible to use DrawLine() method in other nodes on scene? Because my code is longer than example, and repeat it isn't good idea, because I will have problems with debugging and writing new code. Thanks for your answers!

I do not think there is a way to use the drawing functions outside of the _draw method of the node. However, you could try storing the data in something like an array or dictionary, and have the functions populate this data. Something like this (untested, simple example):

static functions

static func draw_line(data_array):
	data_array.append(["Line", Vector2(), Vector2(100, 100), Color.white])

static func draw_circle(data_array):
	data_array.append(["Circle", Vector2(-40, -40), 200, Color.green])

Then in the node that is calling these functions:

func _draw():
	var array_of_drawing_data = [];
	draw_line(array_of_drawing_data )
	draw_circle(array_of_drawing_data )
	
	for drawing_data in array_of_drawing_data:
		if drawing_data[0] == "Line":
			draw_line(drawing_data[1], drawing_data[2], drawing_data[3])
		elif drawing_data[0] == "Circle":
			draw_circle(drawing_data[1], drawing_data[2], drawing_data[3])

It is not perhaps the most ideal, but it should allow for populating drawing data externally that only requires some minimal processing to draw in the _draw function of the node that is calling the static functions.

Unpleasantly that godot isn't support user render. But you tossed me the good idea, I will try to turn it into more usable thing, with my own class, using enums for example. Thanks ;)

6 months later

Addition. After a while I've returned to this problem and found info about VisualServer. I won't write a tutorial here, just see this and this. @TwistedTwigleg You might knew it, but just in case I ping you.