![]() |
Attention | Topic was automatically imported from the old Question2Answer platform. |
![]() |
Asked By | chantunRey |
Hey guys and gals. I’m having trouble with a loop. In this loop I take an element from an array and I want to remove it from the array afterwards, but it seems that in some iterations of the loop this doesn’t happen. Any Ideas?
The function is invoked from the ready function of another node:
func _ready():
globalNode = get_node("/root/global")
rng.randomize()
#instancia player node
add_child(playerNode)
playerNode.add_to_group("Players")
#instancia bots
add_bot(howManyBots)
#despues que todos los elementos se instanciaron, se llama a la funcion de color
globalNode.color_giver()
The function per se:
var colors = [Color(0, 1, 1, 1), Color(1, 0.5, 0.31, 1), Color(0.86, 0.08, 0.24, 1),
Color(1, 0.55, 0, 1), Color(1, 0.08, 0.58, 1), Color(0.49, 0.99, 0, 1),
Color(1, 0, 1, 1), Color(1, 0.87, 0.68, 1), Color(0.93, 0.51, 0.93, 1),
Color(0.53, 0.81, 0.92, 1),Color(1, 0, 0, 1), Color(0.29, 0, 0.51, 1)]
var selectedColors = []
func color_giver():
randomize()
#tomamos un array de nodos del grupo players
var groupNodes = get_tree().get_nodes_in_group("Players")
#una seleccion de tantos colores como jugadores hay
selectedColors = slice_array(colors, 0, groupNodes.size()-1)
#Duplicamos el array
var tempColors = selectedColors.duplicate()
#Loopeamos el array, asignandole un color random a cada nodo
for n in range(groupNodes.size()):
var node = groupNodes[n]
var randomNum = randi()%tempColors.size()-1
var color = tempColors[randomNum]
#borramos el color del array duplicado
tempColors.remove(randomNum)
print(tempColors)
#llamamos a la funcion del nodo que setea el color en el sprite
node.selfColor = color
node.modulate_sprite()
Output:
[0,1,1,1, 1,0.5,0.31,1, 0.86,0.08,0.24,1, 1,0.55,0,1, 1,0.08,0.58,1, 0.49,0.99,0,1]
[0,1,1,1, 1,0.5,0.31,1, 1,0.55,0,1, 1,0.08,0.58,1, 0.49,0.99,0,1]
[0,1,1,1, 1,0.5,0.31,1, 1,0.55,0,1, 0.49,0.99,0,1]
[0,1,1,1, 1,0.5,0.31,1, 1,0.55,0,1, 0.49,0.99,0,1]
[0,1,1,1, 1,0.5,0.31,1, 0.49,0.99,0,1]
[1,0.5,0.31,1, 0.49,0.99,0,1]