0 votes
var screen_color = Color(1,1,1,1)

func _on_Start_button_toggled(button_pressed):
    if button_pressed == true:
        while (screen_color != Color(0,0,0,1)):
            screen_color -= Color(0.05,0.05,0.05,0)
in Engine by (26 points)

1 Answer

+2 votes
Best answer

Probably float imprecision. At the point when the color elements reach 0, some of them could be something like -0.00000001, so the comparison evaluates to False and the color values continue to be subtracted below zero forever.

# output              comparison

0.15,0.15,0.15,1    |   False
0.1,0.1,0.1,1       |   False
0.05,0.05,0.05,1    |   False
-0,-0,-0,1          |   False    # <-- could be -0.0000001 
-0.05,-0.05,-0.05,1 |   False    #     (printing doesn't show it)
-0.1,-0.1,-0.1,1    |   False
-0.15,-0.15,-0.15,1 |   False

To work around it you could test if each of the color elements is greater than zero:

while screen_color.r > 0 or screen_color.g > 0 or screen_color.b > 0:
    # (...)

Although in your case you could probably get away with testing just one:

while screen_color.r > 0:
by (186 points)
selected by

Yes I though of it before, but thought it was ineffecient. But to only use one color is a good idea! Thanks.

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.