GML Is there any particular reason why expressions cannot be indexed multiple times?

A

a52

Guest
It's kind of strange that statements like "a[i ][j] = b[i ][j]" aren't allowed in GML. It's not something I've ever seen in any other language, and it doesn't really make sense.

Indexing is basically a function that takes whatever's in front of it and reduces its dimension by one, something like this: "<expr>[i ]" -> "index(<expr>, i)". Why should a function care whether or not it's being fed back into itself, so long as the input is still of the right type? This kind of check should be done by the compiler or during runtime, not enforced by the grammar.

It makes it really awkward to do anything with multidimensional arrays, because you have to define temporary variables until you reach a 1D array, and then shove them all back when you've changed the value.

Code that would look like this:
Code:
for (var i = 0; i < len(a); i++) {
    for (var j = 0; j < len(a[i]) {
        for (var k = 0; k < len(a[i][j]) {
            a[i][j][k] = (b[i][j][k] + a[i][j][k])/2
        }
    }
}

Becomes something like this in GML:
Code:
for (var i = 0; i < len(a); i++) {
    var a1 = a[i] //temp vars
    var b1 = b[i]
    for (var j = 0; j < len(a1) {
        var a2 = a1[j]
        var b2 = b1[j]
        for (var k = 0; k < len(a2) {
             a2[k] = (b2[k] + a2[k])/2
        }
        a1[j] = a2
        b1[j] = b2
    }
    a[i] = a1
    b[i] = b1
}

There are quite a few extra variables, quite a few extra lines, and it's a lot more difficult to tell at a glance what's going on.

There is the option to use the [i, j] syntax for 2D arrays, but that doesn't work for arrays of more than two dimensions, 1D arrays of 1D arrays instead of 2D arrays of scalars, jagged arrays, etc.

Please make it possible to index multiple times in one line, and make the [i, j, k...] syntax equivalent to [i ][j][k], as it is in numpy, rather than a whole separate type of data storage. It would make so many things so much easier.
 

YellowAfterlife

ᴏɴʟɪɴᴇ ᴍᴜʟᴛɪᴘʟᴀʏᴇʀ
Forum Staff
Moderator
Chained accessors are on the roadmap.

It would likely be [i ][j][k] rather than [i, j, k] because of data structure accessors and the fact that [i,k] is not the same as [i ][k] - due to historic reasons, 2d arrays are a separate data subtype in GML
 
  • Like
Reactions: a52
Top