Adventure Game Studio

AGS Support => Beginners' Technical Questions => Topic started by: wynni2 on Mon 19/11/2018 20:57:00

Title: question about initializing arrays of managed structs
Post by: wynni2 on Mon 19/11/2018 20:57:00
Hi all -

I'm working with managed structs for the first time, and I'm running into a syntax issue. I've reviewed the manual and some other forum entries but can't figure what I'm doing wrong.

I made a managed struct "Particle" to do some particle effects (floating dust, things like that). I'm having trouble initiating it. Here's the relevant code

Code (ags) Select

Particle* pRaindrops

void LoadParticleArrays() {
    pRaindrops = new Particle[1000];
    int i;
    while (i < 1000 {
        pRaindrops[i] = new Particle;
        i++;
    }
}

function game_start() {
    LoadParticleArrays();
}


When I launch the game I get an error "Type mismatch: Cannot convert 'Particle' to 'Particle,'" which is a little baffling.

What am I doing wrong here?

Thanks.
Title: Re: question about initializing arrays of managed structs
Post by: Crimson Wizard on Mon 19/11/2018 21:15:44
Yes, the error messages in script compiler are often broken. Also, the pointers become weird when used in conjunction with arrays.
In this particular case the problem is that you have declared a single pointer to Particle, which you then treat as a dynamic array.

You would need to declare dynamic array of pointers instead:
Code (ags) Select

Particle* pRaindrops[];


On a side note, AGS supports "for" loop now:
Code (ags) Select

for (int i = 0; i < 1000; i++)
    pRaindrops[i] = new Particle;
Title: Re: question about initializing arrays of managed structs
Post by: wynni2 on Mon 19/11/2018 21:49:38
Thanks - I knew it was something simple.  The revised code worked:

Code (ags) Select

Particle* pRaindrops[1000];

void LoadParticleArrays() {
    pRaindrops = new Particle;
    int i;
    while (i < 1000 {
        pRaindrops[i] = new Particle;
        i++;
    }
}



I haven't adopted for loops yet because I got so accustomed to the old-fashioned way. The do/while and switch/case formats have been a great addition, though.