Reset when reaching end of Array

Godot Version

4.7

Question

English is not my native language, and the explanation on this one got a bit to complicated for me.
I know and understand what it does, but there are code I don’t understand.
Does anyone bother too try to explain it in a bit simpler way for me who does not have a master in programming … :face_with_peeking_eye:Its “for turn in range” and “%” I struggle with.

var type_of_drink:Array[String] = [“Beer”, “Juice”, “Coffee”, “Vodka”, “Cola”, “Water”]
var total_turns: int = 15

for turn in range(total_turns):
  var index: int = turn % type_of_drink.size()
  var member: String = type_of_drink[index]
  print(“Turn”, turn, “:”, " This is a ", member)

M:)

You don’t need range() here, you can do:

for turn in total_turns:

That will iterate as you would expect.

The % is “modulo” or “remainder”; it should give you a value between zero and one less than the number on the right side of the operator.

0 % 3 -> 0
1 % 3 -> 1
2 % 3 -> 2
3 % 3 -> 0
4 % 3 -> 1
5 % 3 -> 2
6 % 3 -> 0
7 % 3 -> 1
...
333333333333333 % 3 -> 0
333333333333334 % 3 -> 1

if you want to print all members of array there’s a simpler way:

var turn: int ## It will be 0 cuz its default value for int
for member in type_of_drink:
  print("Turn", turn, ":", "Is", member
  turn += 1 ## We add 1 because turn finished