Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

AGS 4 / new compiler: support dynamic arrays of normal structs #1818

Closed
ivan-mogilko opened this issue Oct 23, 2022 · 6 comments
Closed

AGS 4 / new compiler: support dynamic arrays of normal structs #1818

ivan-mogilko opened this issue Oct 23, 2022 · 6 comments
Labels
ags 4 related to the ags4 development context: script compiler type: enhancement a suggestion or necessity to have something improved

Comments

@ivan-mogilko
Copy link
Contributor

ivan-mogilko commented Oct 23, 2022

I've been wondering, if it's possible to support dynamic arrays of non-managed structs?
For example:

struct Cell
{
    int data;
};

Cell cells[];

Above results with error in the extended compiler:

Can only have dynamic arrays of integer types, 'float', or managed structs. 'Cell' isn't any of this.

(old compiler does not error on declaration, but on the new operator instead)

Right now this is restricted in both old and new compilers. Although, theoretically, you should be able to store an element of any size in dynamic arrays. Perhaps there have been a problem in coding accessing these elements, or anything else I cannot think of right now?

@ivan-mogilko ivan-mogilko added type: enhancement a suggestion or necessity to have something improved ags 4 related to the ags4 development context: script compiler labels Oct 23, 2022
@fernewelten
Copy link
Contributor

At first sight I'd think that you're right, this should work. I don't see any problems that could come up if this limitation were removed. We'd need to make sure, however, that the struct doesn't contain any dynamic entities, either directly or indirectly.

@fernewelten
Copy link
Contributor

fernewelten commented Oct 26, 2022

Yes, I could implement this, by eliminating the error message and adding a check to make sure that Cell doesn't contain any dynamic arrays or pointers. Do we want to have it?

From the vantage point of the game coders, the code would be very similar to the code below that both the compilers can compile:

managed struct Cell
{
    int data;
};

Cell *cells[];

In the example above, after initializing the array Cells, each array element would be null and would need to be initialized individually with a cells[…]= new Cell;. In Ivan's code, above, all the elements would be ready for use at the get-go after Cells has been initialized.

@ivan-mogilko
Copy link
Contributor Author

Note that compiler will still have to deny dynamic arrays of structs that have nested managed pointers, until #1923 is completed.

@ericoporto
Copy link
Member

A note, that #1923 was finished and is working, so perhaps this could be done now with less checks.

@fernewelten
Copy link
Contributor

Addressed in #2106

@ivan-mogilko
Copy link
Contributor Author

Implemented by #2106.

Example of use:

struct Warrior
{
    int Strength;
    int Health;
};

function room_FirstLoad()
{
    Warrior Lemmings[] = new Warrior[10];
    player.Say("That one's strength is %d", Lemmings[1].Strength);
}

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
ags 4 related to the ags4 development context: script compiler type: enhancement a suggestion or necessity to have something improved
Projects
None yet
Development

No branches or pull requests

3 participants