How to access array indices in a for loop?

:information_source: Attention Topic was automatically imported from the old Question2Answer platform.
:bust_in_silhouette: Asked By del
:warning: Old Version Published before Godot 3 was released.

Hi!
So, i have an array for a scoreboard, which holds a number of arrays for rank, name and score values and i want to print them into labels on a grid container with a for loop.
I’ve tried something like this:

for entry in board:
      var rank = Label.new()
      var score = Label.new()
      var name = Label.new()
      rank.set_text(str(entry[0]))
      score.set_text(str(entry[1]))
      name.set_text(str(entry[2]))

But i always get an ‘Invalid get index ‘1’ (on base: ‘Array’).’ message. Index[0] works fine, but everything above it not.
I’ve also tried “for i in range(0, entry.size())” in a new for loop, but i still get the same error.

To just print the strings i could do
for i in entry: var lable = Label.new() table.add_child(label) lable.set_text(str(i))
But i want to access the indices.

:bust_in_silhouette: Reply From: Skyfrit

Is this what you want?

var board = [[50, 10, "John"], [60, 5, "Ray"]]

for entry in range(board.size()):
	print(board[entry][0])
	print(board[entry][1])
	print(board[entry][2])

That’s what i want, but it still does not work. I get the same error message.

del | 2017-07-08 20:30

OK, i’ve tried it again with a self written array and it worked. It seems there’s something wrong with my original array. I get the array from a long string, which i convert with

global.board = Array(text.split(";"))

and than again in a for loop like this:

for i in global.board:
		var boardSub = Array(i.split(":"))
		board.append(boardSub)

But your method definitely works. Thank you!

del | 2017-07-08 21:00

you’re welcome.

Skyfrit | 2017-07-08 21:32