Problem with using array in for loop

Godot Version

i’m using godot 4.2.2

Question

hi, i’m trying to make an undertale themed clicker game and i’m having trouble with something.
i am trying to make the game loop, and i figured i should make a thing that gives me the currency per second because that’s what clicker games Are.
i decided that the most efficient way to do that would be to do a for loop in the global variables autoload script that iterates though the array containing the currency generating things (which in this case are monsters. because it’d undertale themed).
but then, when it gets to it, it spits out this error:

Invalid get index ‘[“Froggit”, 0, 25, 1, 1]’ (on base: ‘Array’).

anyway this’ll all probably make any amount of sense if i show you the code.

extends Node

#base stats
var gold = 0
var love = 1
var g_per_click = 1
var total_g = 0
var total_clicks = 0
var price_mult = 1.2

#monsters, buildings, towers, whatever you want to call them
var monsters = [
		[
			"Froggit", #monster name
			0, #monster count (will always be 0 here)
			25, #monster price
			1, #monster's g per second
			1 #monster g generation multiplier (will always be 0 here)
		],
		[
			"Whimsun",
			0,
			10,
			0.2,
			1
		],
		[
			"Toriel",
			0,
			100,
			10,
			1
		]
	]

# Called when the node enters the scene tree for the first time.
func _ready():
	pass


# Called every frame. 'delta' is the elapsed time since the previous frame.
func _process(delta):
	for i in monsters:
		gold += monsters[i][3] * monsters[i][1] * monsters[i][4] #this line. this line is the thing giving me trouble
		i += 1

You need to use range in for loop. E.g

var array = ['apple', 'banana', 'cherry']
for i in range(array.size()):
    print(array[i])

Also incrementing i+=1 is incorrect since i is set by the for loop

1 Like

alright thanks that worked! (also yeah i’m aware i just accidentally left that there to see if that was miraculously the problem)

This topic was automatically closed 30 days after the last reply. New replies are no longer allowed.