[ACS] trying to initialize a static constant in for loop

Ask about ACS, DECORATE, ZScript, or any other scripting questions here!

Moderator: GZDoom Developers

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!)
Post Reply
User avatar
ROBO-KY
Posts: 9
Joined: Sat Oct 26, 2019 7:34 am
Location: Belarus, Minsk

[ACS] trying to initialize a static constant in for loop

Post by ROBO-KY »

Hello everyone, I just want to understand how static works in ACS. I tried to initialize a static variable in for loop but I got a compiler error.

This code was compiled successfully:

Code: Select all

script "test" (void) {
	for(static int i = 1 + 1; i < 10; i++) {}
}
But here I got a compiler error

Code: Select all

script "test" (void) {
	int j = 1;
	for(static int i = 1 + j; i < 10; i++) {} 
}
Syntax error in constant expression.
> for(static int i = 1 + j; (j symbol)

But if I remove static modifier everything will work. Is this a normal behavior or this is a bug?
zd_mallard
Posts: 2
Joined: Mon Mar 14, 2022 9:09 am

Re: [ACS] trying to initialize a static constant in for loop

Post by zd_mallard »

I'm no expert in ACS, but that's not valid in C either, so is probably intentional.
User avatar
nova++
Posts: 177
Joined: Sat Sep 04, 2021 3:13 am

Re: [ACS] trying to initialize a static constant in for loop

Post by nova++ »

I can't even figure out what in the world you're trying to do.
User avatar
ROBO-KY
Posts: 9
Joined: Sat Oct 26, 2019 7:34 am
Location: Belarus, Minsk

Re: [ACS] trying to initialize a static constant in for loop

Post by ROBO-KY »

nova++ wrote:I can't even figure out what in the world you're trying to do.
I'm testing the language behavior in different scenarios.

Btw, this code will not compile.

Code: Select all

function void loops(void) {
    for(int i = 0; i < 10; i++) {
        //TODO
    }

    for(int i = 0; i < 10; i++) { //shows error here because of 'redefined variable', which is strange because it's declaired in different scope
        //TODO
    }
}
Jarewill
 
 
Posts: 1853
Joined: Sun Jul 21, 2019 8:54 am

Re: [ACS] trying to initialize a static constant in for loop

Post by Jarewill »

ROBO-KY wrote:Btw, this code will not compile.
That's because you already defined variable i inside the scope of the function.
This should fix it:

Code: Select all

function void loops(void) {
    for(int i = 0; i < 10; i++) {
        //TODO
    }

    for(i = 0; i < 10; i++) { //Instead of redefining the same variable again you can reuse it by setting it to 0 or define another variable in it's place such as int j
        //TODO
    }
} 
Post Reply

Return to “Scripting”