0 votes

I get the following error "Drawing is only allowed inside NOTIFICATION_DRAW, _draw() function or 'draw'.", on the code attached bellow.

I don't understand the reason for the error, as the function is called from inside a _draw() function.

extends Node

var cur_width = 0
var x = 0
var y = 0
var done
var pixels_per_second
var time_fill_bar = 0.5 #seconds
var width
var coe

onready var refrence_frame = get_parent()

func _ready():
    var health = get_node("/root/Root/Samurai").get("health")
    width = refrence_frame.get_size().x
    coe = float(width)/health
    pixels_per_second = float(health)/float(time_fill_bar)
    set_process(true)

func _process(delta):
    if !done:
        if x < width:
            x += pixels_per_second*delta
        if x >= width:
            done = true
            x = width
    else:
        x = get_node("/root/Root/Samurai").get("health")*coe
    _draw()

func _draw():
    update()
    var rect = Rect2(0,0,x,refrence_frame.get_size().y)
    var color = Color(1,0,0,1)
    draw_rect(rect,color)

Is this a bug? should I report it to the GitHub?

in Engine by (40 points)

1 Answer

+2 votes
Best answer

You're doing it the other way around, call update in _process instead of draw, and don't call update inside _draw.

Look at docs for more detail:
http://docs.godotengine.org/en/stable/tutorials/2d/custom_drawing_in_2d.html

by (7,866 points)
selected by
extends Node

var cur_width = 0
var x = 0
var y = 0
var done
var pixels_per_second
var time_fill_bar = 0.5 #seconds
var width
var coe

onready var refrence_frame = get_parent()

func _ready():
    var health = get_node("/root/Root/Samurai").get("health")
    width = refrence_frame.get_size().x
    coe = float(width)/health
    pixels_per_second = float(health)/float(time_fill_bar)
    set_process(true)

func _process(delta):
    if !done:
        if x < width:
            x += pixels_per_second*delta
        if x >= width:
            done = true
            x = width
    else:
        x = get_node("/root/Root/Samurai").get("health")*coe
    update()

func _draw():
    var rect = Rect2(0,0,x,refrence_frame.get_size().y)
    var color = Color(1,0,0,1)
    draw_rect(rect,color)
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.
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 webmaster@godotengine.org with your username.