question about initializing arrays of managed structs

Started by wynni2, Mon 19/11/2018 20:57:00

Previous topic - Next topic

wynni2

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

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.

Crimson Wizard

#1
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

Particle* pRaindrops[];


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

for (int i = 0; i < 1000; i++)
    pRaindrops[i] = new Particle;

wynni2

Thanks - I knew it was something simple.  The revised code worked:

Code: ags

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.

SMF spam blocked by CleanTalk