[RESOLVED] Multidimensional arrays in AGS

Started by Joacim Andersson, Fri 27/12/2024 09:31:07

Previous topic - Next topic

Joacim Andersson

Does AGS support multidimensional or jagged arrays? I've tried to declare one but only get errors, my workaround is to create a struct containing one array member and then create an array of that struct.

That works, for now, I'm just interested if there's a "traditional" way of doing it.

Crimson Wizard

#1
AGS 4 does support both multidimensional and jagged arrays.
I suggest reading the list of changes for ags4, and a cheet sheet which explains new features in detail:
https://github.com/adventuregamestudio/ags/wiki/New-compiler's-end-user-cheat-sheet

The classic workaround for non-jagged multidimensional arrays is to have a linear array which stores all dimensions. For example 2D array emulation will be a 1D array that stores elements row by row, where element of (x,y) is located at (x + y * row_width).

Joacim Andersson

Yes, I know how to convert a non-jagged multidimensional array into a 1-D array, but in this case, I would need a jagged array.

I'm glad to hear all the new stuff that will arrive with AGS 4 however, I'm not using that since it's still in alpha.

So, I'm guessing I'll have to use the struct workaround for now.

As always, thank you so much for your rapid reply.

Joacim Andersson

I spoke too early, it seems like nested structs are also not supported in v. 3.6.x. If AGS treats a struct as a type why can't I use that type in another struct?

Crimson Wizard

#4
Quote from: Joacim Andersson on Fri 27/12/2024 10:47:31I spoke too early, it seems like nested structs are also not supported in v. 3.6.x. If AGS treats a struct as a type why can't I use that type in another struct?

The answer to this and other similar questions is simply that the original ags compiler is not complete, and cannot parse or handle certain constructs correctly.


EDIT:
But if you were speaking of managed structs, the situation is more complicated.
Having nested managed structs requires, besides compiler supporting this syntax:
a) runtime type information (reflection) in the engine, to let engine know which fields in struct are managed pointers, so that it could correctly dispose allocated objects when the parent struct is disposed.
b) full garbage collector to deal with managed object's cross-references of any complexity.

these were added only in ags4.

SMF spam blocked by CleanTalk