custom draw script from another script

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

I have try to write custom draw script witch can call globally (use autoloaded) but when I call a script
ERROR: draw_primitive: Drawing is only allowed inside NOTIFICATION_DRAW, _draw() function or ‘draw’ signal.
At: scene/2d/canvas_item.cpp:866.
appear

this is a script

extends Node2D

func polygon(center:Vector2, points, radius, rotation, outline:bool, thick, color:Color, alpha):

print_debug("drawing polygon")

var c : PoolColorArray
var p : PoolVector2Array

var x
var y
var ang = 360/points

color.a = alpha

if not outline:
	thick = radius

for i in range(points+1):
	
	x = cos(deg2rad(ang*(i%points)+rotation))*radius + center.x
	y = sin(deg2rad(ang*(i%points)+rotation))*radius + center.y
	
	c.append(color)
	p.append(Vector2(x,y))
	
	if i > 0:
		draw_primitive(p, c, p, null, 1, null)
		c.remove(0)
		p.remove(0)
	
	x = cos(deg2rad(ang*(i%points)+rotation))*(radius-thick) + center.x
	y = sin(deg2rad(ang*(i%points)+rotation))*(radius-thick) + center.y
	
	c.append(color)
	p.append(Vector2(x,y))
	
	if i > 0:
		draw_primitive(p, c, p, null, 1, null)
		c.remove(0)
		p.remove(0)

and this is another script

extends Node2D

var runtime = 0

func _ready():
pass

func _process(delta):
runtime += delta

update()

func _draw():
engine.polygon(
OS.get_real_window_size()/2,
3, 50, runtime*100, true, 25, Color.cyan, 1)

I’m totally new to godot and can’t find a solution myself T^T
btw sorry for may bad english

:bust_in_silhouette: Reply From: Eric Ellingson

Can you try passing in a reference to the node itself and call draw_primitive(...) on the node?

func polygon(node: CanvasItem, center:Vector2, points, radius, rotation, outline:bool, thick, color:Color, alpha):
    ...

    node.draw_primitive(p, c, p, null, 1, null)

Then in the other script:

engine.polygon(
	self, 
	OS.get_real_window_size()/2,
	3,
	50,
	runtime*100,
	true,
	25,
	Color.cyan,
	1
)