0 votes

I'm working with the following code:

    for digit in str(game.score_current):
        var texture_frame = TextureFrame.new()
        texture_frame.set_texture(sprite_numbers[int(digit)])
        add_child(texture_frame)

When game.score_current is 0, two children are added. I have confirmed that the string is "0" (no spaces/padding) and that the for loop is entered twice. What am I doing wrong?

in Engine by (15 points)

The code you posted looks fine.
Maybe the error is in another part of the script.
Do you get any error messages?
What exactly is sprite_numbers ?

No error messages... sprite_numbersis an array containing textures:

const sprite_numbers = [
    preload("res://sprites/number_large_0.png"),
    preload("res://sprites/number_large_1.png"),
    preload("res://sprites/number_large_2.png"),
    preload("res://sprites/number_large_3.png"),
    preload("res://sprites/number_large_4.png"),
    preload("res://sprites/number_large_5.png"),
    preload("res://sprites/number_large_6.png"),
    preload("res://sprites/number_large_7.png"),
    preload("res://sprites/number_large_8.png"),
    preload("res://sprites/number_large_9.png")
]    

Maybe the problem is my understanding of the for loop? I just tested with for digit in "0" and I get the same result.

Hm,

for me the code:

for digit in "0":
    print(digit)

works just fine(only prints the number once). Did you check if the function just get called twice, by adding a print before the for loop and after it?

1 Answer

0 votes
Best answer

This is a known issue and has already been fixed in the master branch.
The next 2.1.x maintenance release should include this fix.

Reference:
Issue
PR

by (924 points)
selected by

Oh I switched to a nightly build a while ago, which explains why it worked for me. Good to know ^^

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.