I'm making an idle game about selling eggs and I want to create an egg every 2 sec. There are 5 types of eggs (as shown in the eggs
array), and I have the function GenerateEgg
to choose one randomly and instantiate it. Basically, the function loops over chances
(a copy of eggChances
) and generates a random number between 0 and 1, to determine if the egg is going to be created or not. If it isn't, the function uptades the percentages in chances (so they sum up to 1), and goes for the next element in the array.
`var eggs = ["Normal", "Half Treasure", "Treasure", "Half Trash", "Trash"]
var eggChances = [0.5, 0.15, 0.05, 0.2, 0.1]
func GenerateEgg():
var eggIndex = 0
var chances = eggChances.duplicate(true)
for egg in chances:
var rngNum = randf_range(0, 1)
if rngNum <= egg:
print(eggs[eggIndex])
var eggInstance
match eggs[eggIndex]:
"Normal":
eggInstance = normalEgg.instantiate()
"Half Treasure":
eggInstance = halfTreasureEgg.instantiate()
"Treasure":
eggInstance = treasureEgg.instantiate()
"Half Trash":
eggInstance = halfTrashEgg.instantiate()
"Trash":
eggInstance = trashEgg.instantiate()
eggInstance.position = conveyorBelt.get_node("Start").global_position
eggInstance.rotation_degrees = rotation_degrees
get_tree().get_root().call_deferred("add_child", eggInstance)
break
else:
var lastPercentage = chances[0]
chances.remove_at(0)
eggIndex += 1
for i in chances.size():
chances[i] = chances[i] / (1 - lastPercentage)`
The problem is, the normal and both treasure eggs generate normally, but if none of them are generated the for loop just stops, it doens't even go through the rest of the array (Therefore, not generating any trash eggs). I know the problem is in the for loop because I asked it to print chances
and rngNum
at every iteration, and after the treasure egg it simply stops:
generate!
[0.5, 0.15, 0.05, 0.2, 0.1]
0.54929866835448 (normal egg is not generated)
[0.3, 0.1, 0.4, 0.2] (new percentages)
0.386365563353 (half treasure egg is not generated)
[0.14285714285714, 0.57142857142857, 0.28571428571429] (new percentages)
0.73908705766343 (treasure egg is not generated, nothing more printed after this)