0 votes

I made a node2d singleton that draw a circle, but the draw_line won't draw. it gives me an error

drawline: Drawing is only allowed inside NOTIFICATIONDRAW, _draw()
function or 'draw' signal

if I used it inside a node it works, but my goal is to use it globally.

extends Node2D

func draw_circle_arc(center, radius, angle_from, angle_to, color):
    var nb_points = 16
    var points_arc = PoolVector2Array()

    for i in range(nb_points + 1):
        var angle_point = deg2rad(angle_from + i * (angle_to-angle_from) / nb_points - 90)
        points_arc.push_back(center + Vector2(cos(angle_point), sin(angle_point)) * radius)

    for index_point in range(nb_points):
        draw_line(points_arc[index_point], points_arc[index_point + 1], color)
Godot version latest
in Engine by (45 points)

Unsure what your use case is here. It feels like you've created a component; e.g. that you can instance in any scene to draw a circle rather than one instance (i.e. the singleton) that you call from anywhere (as a utility). Perhaps export the function parameters so you can change them in the editor and instance different kinds of circles (etc).

If the latter is true, essentially you can't do this outside of draw calls as noted by the error. Instead, have the singleton return the points that are calculated and use them to draw where appropriate.

Please log in or register to answer this question.

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.