How does this function work?

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

Hello, I am new to Godot and learning how to code. I want to put a light indicator in my game, and basically, if player uses one unit of power, one of the lights goes off. I get this line of code from a tutorial, it is working as planned, but I don’t quite understand the logic of this function. In the spirit of learning, could some one explain how the function works? Specially what the “i” means in the function. Thank you!

func set_light(value):
    for i in get_child_count():
	   if value > i:
		   get_child(i).texture = light_on
	   else:
		   get_child(i).texture = light_off
:bust_in_silhouette: Reply From: Jorge

Hi,
for is a loop that will finish when get to the end of its instruction.
i is a value that is only use to get the value at that moment and could be any other name.

for i in range (10):
print(i)

will print the number for 1 to 10 and then will go to the next line of code.

if after the loop you tupe print(i) , it will print 10

I found a video on YT

I hope it make sense.

Thank you for taking time to answer my question, and the video that you suggested was very insightful. I finally understand what this function is doing now.

Idleman | 2020-07-06 11:15