+1 vote

I have lots of loops where I want to do the same task several times but not iterate through a data set. For example to spawn x amount of something, I do:

var amount
for i in range(amount):
 var scene = packedScene.instance()
 add_child(scene)

I do this quite often. Now, this leaves me with many warnings saying "Local variable i is declared but never used". My warnings are stacking up are getting cluttered with these warnings I can't really do anything about.

What would be the best way to avoid this? Would love for gdscript to have something like:

var amount
do amount times:
    add_child(scene)
in Engine by (57 points)
edited by

1 Answer

+3 votes
Best answer

underscore

for _i in range(amount):
    var scene = packedScene.instance()
    add_child(scene)
by (1,939 points)
selected by

Thank you, I did not know that! That also solved all my "The argument 'xxxx' is never used in the function..."-warnings. Do you also know how to solve the warning "The function 'connect()' returns a value, but the value is never used", if you don't want to use the value?

extends Object
class_name Utility

# for connect and int warnings
static func ok(e: int) -> void:
    if e != OK:
        print_debug("Error: %s" % e)

# for tween and bool warnings - to be removed in 4
static func stfu(_ignore) -> void: pass
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.