Invalid set index '1' (on base: Array) with value of type int?

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

Well this is my code:

var t = 1;
var us = [72];
us[0] = t;

for i in range (72):
  if (i==0):
    does stuff that changes the value of t
    does stuff that changes the value of t.
    us[i] = t;

t is a number that will get multiplied and used for math. As it is only being multiplied and other simple arithmetic, it is always an integer number, and will never be a float or double because it is not getting divided.

us is an array of values of t that have already been used for math. I use this array to not make the computer repeat calculations that it’s already done.

The program checks every value of us and compares it with t. If it’s already been used, it skips math and changes the value of t, checks again until a value hasn’t been used before and does math with it.

I thought this should save values of t in the us array starting by index 1, but for some reason when I execute the program it says that index 1 is invalid. Or that is what I understand by “Invalid set index ‘1’ (on base: Array) with value of type int”

I really don’t understand what’s the problem. I have read the code about 12 times, line by line and I can’t find logic errors.
This is the first time I use godot so I don’t know the syntax very well, so I suppose my error is caused by writting something wrong.

If you want I could post the original code but it is strange and tedious to explain and understand, because it has a lot of loops. I hope only the information posted here is enough, if not, tell me.

I think I have an idea of what may be causing this, but I will stop programming for today. It has been a productive day, I’ve done some good things and I’m satisfied. Hope you all had a good day too!

Thank you in advance!

:bust_in_silhouette: Reply From: hilfazer
var us = [72]

It creates an array with one element of type int and value of 72. Only index of 0 is valid for this array.

I don’t know how to initialize and array with given size filled with nulls/zeroes. But there’s a resize() method:


call it before you use your array.

Other option is to initialize array with t

  var us = [t]

and append other values to it


Great! I will try right now. It…WORKS!!
Thank you very much! As you can see I am new to this engine and don’t know exactly how it works. Let’s keep improving!

C:\Flavius | 2018-04-25 16:10