0 votes

Hello, I'm looking for a way to simplify this piece of code :

onready var colo;
onready var characterlist = [0,0,0,0,0,0];   # red / blue / yellow / black

    if characterlist[0] == 0 :
        characterlist[0] = 1
        colo = "red"

    elif characterlist[1] == 0 :
        characterlist[1] = 1
        colo = "blue"

    elif characterlist[2] == 0 :
        characterlist[2] = 1
        colo = "yellow"

    elif characterlist[3] == 0 :
        characterlist[3] = 1
        colo = "black"

It's checking if the next colour in line is being used, and use it if available.
How can I do that more efficiently please ?

in Engine by (245 points)
edited by

1 Answer

0 votes
Best answer

How about a loop?

onready var colo
onready var characterlist = [0,0,0,0,0,0] # red / blue / yellow / black

func some_function():
   # This all ought to be in a function anyway
    var colours = ["red", "blue", "yellow", "black"]
    for i in range(4):
        if characterlist[i] == 0:
            characterlist[i] = 1
            colo = colours[i]
            break
by (3,869 points)
selected by

thanks SIsilicon, that's perfect ! :)

Your welcome. ;-)

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.