zscript multidimensional arrays?

Ask about ACS, DECORATE, ZScript, or any other scripting questions here!
Forum rules
Before asking on how to use a ZDoom feature, read the ZDoom wiki first. If you still don't understand how to use a feature, then ask here.

Please bear in mind that the people helping you do not automatically know how much you know. You may be asked to upload your project file to look at. Don't be afraid to ask questions about what things mean, but also please be patient with the people trying to help you. (And helpers, please be patient with the person you're trying to help!)
gramps
Posts: 300
Joined: Thu Oct 18, 2018 2:16 pm

zscript multidimensional arrays?

Post by gramps »

Does zscript fully support multidimensional arrays?

If I write `int foo[32][16]` and then `foo[17][14] = 123`, I'll get an index out of bounds error.

If I write `int foo[32][16]` and then `foo[15][14] = 123`, I get no error.

In other words it looks like indices for every dimension have to fit within the smallest dimension size, if that makes sense. Either I'm doing something horribly wrong here or there's a nasty bug.
User avatar
Apeirogon
Posts: 1606
Joined: Mon Jun 12, 2017 12:57 am

Re: zscript multidimensional arrays?

Post by Apeirogon »

gramps
Posts: 300
Joined: Thu Oct 18, 2018 2:16 pm

Re: zscript multidimensional arrays?

Post by gramps »

Huh, so it's backwards?

So just to be clear, if an array were defined in ACS as `int foo[10][20][30]`, then in zscript it should be defined as `int foo[30][20][10]`, right? But when indexing the array, as in `bar = foo[1][2][3]`, it would be the same in both ACS in zscript?
User avatar
Graf Zahl
Lead GZDoom+Raze Developer
Lead GZDoom+Raze Developer
Posts: 49252
Joined: Sat Jul 19, 2003 10:19 am
Location: Germany

Re: zscript multidimensional arrays?

Post by Graf Zahl »

It's a bug, but unfortunately one buried deep in the parser logic so it's not easily fixable.
gramps
Posts: 300
Joined: Thu Oct 18, 2018 2:16 pm

Re: zscript multidimensional arrays?

Post by gramps »

Got it, thanks :)

For anyone else who happens to find this, it does work the way I described in the previous comment... declaration is reversed from ACS, but indexing is the same. The other thing to watch out for is ACS array access is more robust, allowing you to index arrays with out-of-bounds indices (evals to 0), while zscript will not allow this. So you might need some extra guards around your array access in zscript, when porting stuff from ACS.
User avatar
Mikk-
Posts: 2274
Joined: Tue Jun 30, 2009 1:31 pm

Re: zscript multidimensional arrays?

Post by Mikk- »

Could this be fixed with versioning? or is that completely impossible and I'm an idiot?
User avatar
Graf Zahl
Lead GZDoom+Raze Developer
Lead GZDoom+Raze Developer
Posts: 49252
Joined: Sat Jul 19, 2003 10:19 am
Location: Germany

Re: zscript multidimensional arrays?

Post by Graf Zahl »

The problem is not versioning, it's how the compiler frontend constructs types out of the source code. The code is rather badly designed but changing it would risk breaking stuff way beyond the order of array indices.

Return to “Scripting”