+1 vote

I have try to write custom draw script witch can call globally (use autoloaded) but when I call a script
ERROR: drawprimitive: Drawing is only allowed inside NOTIFICATIONDRAW, 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
realwindowsize()/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

asked Feb 16 in Engine by newbie09 (15 points)

1 Answer

+1 vote

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
)
answered Feb 17 by Eric Ellingson (1,579 points)
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.