File and String index

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


I am looking for a file to fill ‘array’ type ‘string’
the declaration of the ‘string array’:

var test: PoolStringArray =

and then…
var i: int
i = 1

test [i] = file.get_line ()

But I have this message; invalid set index ‘1’ (on basis: ‘PoolStringArray’) with value of type ‘String’

:bust_in_silhouette: Reply From: Zylann

You get this error because you didn’t resize the array. Accessing indexes without giving proper size to the array results in invalid index.

For your use case, I would suggest you use append, which increases size by 1 and adds the line at the end of the array:


In case you know in advance how many lines there is:

test[i] = file.get_line()

Another thing to note: array indexes start from 0, not 1.

okay, and can we define the dimension of ‘Array’ to the declaration of the variable?

thank you
ps; original your audio story on your site.

vivid | 2019-09-05 04:47

You can’t declare the size of the array at the same line of creation of the array, it needs a second line:

var lines = []

Zylann | 2019-09-05 17:38

thank you very much

vivid | 2019-09-05 19:12