I've always used arrays in the following manner :

- define it:
global MyArray[10,30]

Then I could set an element in it like:
MyArray[4, 18] = 3920

Access it:
print "Val = " + MyArray[4,18];

Results:
"Val = 3920"

I see in Godot arrays are treated differently, they more or less act like placeholders for data. Question is, what other means would I have to mimic classic array access as described above?

Your example is a 2D array. GDScript only supports 1D arrays. You can easily "emulate" multi dimensional array lookup though. Simply compute the 1D index from multidimensional indexes and supposed dimension sizes.

Brilliant, I'm going to paste it here for my own reference later ( at work right now ). Also, I suppose this would work with a three-dim array as well? Just add an additional component to it.

func create_2d_array(width, height, value):
    var a = []

    for y in range(height):
        a.append([])
        a[y].resize(width)

        for x in range(width):
            a[y][x] = value

    return a

Yes, that approach should work for a three-dimensional array.

Making an array of arrays (of arrays...etc) is not the most optimal way to do it.

Flattening the whole array into one dimension is the best approach for multitude of reasons I won't go into here. This is true in general not only for GDScript.

var a = []
a.resize(width * height)

Ahh so resizing would be the outcome from the original problem I posted up top.

var a = [] a.resize(width * height) a[1,3] =21

print a[1,3] #output 21

Not sure I understand what you see as a problem here. Simply use an one dimensional array and imagine all the rows are packed one after another.

@NoDoubt said: Ahh so resizing would be the outcome from the original problem I posted up top.

var a = [] a.resize(width * height) a[1,3] =21

print a[1,3] #output 21

a[1*3] = 21
print a[1*3]

It's just a different way to think about it but you can use the width and height numbers multiplied together and get the same result. I know what you mean, it's easier to think about rows and columns that way, but it still should work if you multiply them. Although, maybe I'm wrong about that because 3 times 6 and 6 times 3 would be the same but they are different in a 2 dimensional array. There must be a formula to convert anyway so you just need a function to do it. a[ getArrayPos(5,19)] = 21 and it would return the flat array position.

Width height is correct for the total size, but the formula for converting a 2D index into 1D is: index = x + y width

So for example for coordinates 1,2, the 1D value would be 1+2*4, which is 9.

To go the opposite way: x = index % width y = index / width

10 months later