The Godot Q&A is currently undergoing maintenance!

Your ability to ask and answer questions is temporarily disabled. You can browse existing threads in read-only mode.

We are working on bringing this community platform back to its full functionality, stay tuned for updates.

godotengine.org | Twitter

+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

in Engine by (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
)
by (1,663 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 Frequently asked questions and 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 [email protected] with your username.