simplify some code

:information_source: Attention Topic was automatically imported from the old Question2Answer platform.
:bust_in_silhouette: Asked By nonomiyo

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 ?

:bust_in_silhouette: Reply From: SIsilicon

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

thanks SIsilicon, that’s perfect ! :slight_smile:

nonomiyo | 2018-11-17 15:11

Your welcome. :wink:

SIsilicon | 2018-11-17 15:19