how do I use draw_line() as singleton?

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

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

draw_line: Drawing is only allowed inside NOTIFICATION_DRAW, _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)

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.

spaceyjase | 2022-01-08 10:38