foreach loop syntax?

Bugs that have been investigated and resolved somehow.

Moderator: GZDoom Developers

Forum rules
Please don't bump threads here if you have a problem - it will often be forgotten about if you do. Instead, make a new thread here.
D2JK
Posts: 545
Joined: Sat Aug 30, 2014 8:21 am

foreach loop syntax?

Post by D2JK »

Hi all. I'm having trouble getting "foreach" loops to work:

Test code:

Code: Select all

44    override void Tick() {
45        array<String> testStrings;
46
47        testStrings.append("A");
48        testStrings.append("B");
49        testStrings.append("C");
50
51        foreach (item : testStrings) {
52            console.printf("This string: %s", item);
53        }
54
55        Super.Tick();
56    }
Error message:
...
LoadActors: Load actor definitions.
Script error, "<file.zs>" line 51:
Unexpected '{'
Expecting ';'
Example provided on ZDoom.org wiki:

Code: Select all

Array<Actor> things;
foreach (mo : things)
{
  if (mo)
  {
    // Do something
  }
}
And as additional information, if in my code example above I delete the curly braces forming the foreach loop block, the error message becomes:
...
LoadActors: Load actor definitions.
Script error, "<file>.zs" line 52:
Unexpected identifier
Expecting ';'
User avatar
Xeotroid
Posts: 443
Joined: Sat Jun 23, 2012 7:44 am
Graphics Processor: nVidia with Vulkan support
Location: Czech Rep.

Re: foreach loop syntax?

Post by Xeotroid »

Not a bug. Use a ZScript version 4.10.0 or higher. You set that in your main ZScript file at the very top, like so (here for the latest stable release of GZDoom):

Code: Select all

version "4.12.2"
If using this gives you a different error about a newer version of GZDoom being required, update your GZDoom to the latest release. More info regarding ZScript versions is here.
D2JK
Posts: 545
Joined: Sat Aug 30, 2014 8:21 am

Re: foreach loop syntax?

Post by D2JK »

Ah, ok. I did already update to the latest development build, but didn't think of the version specification change.

Thank you for the help. It looks like I have number of feature deprecation warnings to wade through next, but I'm guessing this fix will work for the problem in OP.

Return to “Closed Bugs [GZDoom]”