[1.3.56+] foreach() and c() not playing nice

Confirmed bugs should get a single post here. Check the READ ME FIRST sticky thread for the format.

Moderators: dorpond, trevor, Azhrei, giliath, jay, Mr.Ice, MapTool BugReport Manager

Forum rules
Posts that do not conform to the READ ME FIRST sticky thread are subject to deletion.
Post Reply
User avatar
plothos
Great Wyrm
Posts: 1890
Joined: Sat Jun 21, 2008 1:07 am

[1.3.56+] foreach() and c() not playing nice

Post by plothos »

Wrathgon recently tried to code something with a basic structure like this:

Code: Select all

[r,c(1),code:{
  [h,foreach(letter,"a,b,c,d,e,f,g,h,i,j,k,l,m,n,o"),code:{
    [h:num=roll.count]
  }]
  [r:roll.count]
}]
This reports 15, I believe because foreach() also uses a hidden variable roll.count to track where it is in the string. This is a problem because his code is such that he needs to run the foreach before referencing roll.count. Moreover, if the foreach() sets roll.count high enough, the c() function will terminate after just the initial runthrough.

I believe the solution would be as simple as setting foreach() to use a variable by a different name than roll.count (I recommend for.count), but I'm just guessing.
Drop-In Utilities:
My Spell Manager for D&D3.5 and PFRPG
My Inventory Manager for D&D and PFRPG, but more generally useable than that.
My Message Manager -- My Top-Down D&D Token Images
and my Custom Initiative & Status/Spell-Effect Tracker (work in progress, but functional).

User avatar
Rumble
Deity
Posts: 6235
Joined: Tue Jul 01, 2008 7:48 pm

Re: [1.3.56+] foreach() and c() not playing nice

Post by Rumble »

I noticed that a ways back; roll.count is not unique to each individual looping structure in a nested arrangement (although I think it is reset at the end of a loop, so that entirely separate loops are unaffected - but perhaps not). I've had to use other counters and the "for" loop at times.

User avatar
Azhrei
Site Admin
Posts: 12086
Joined: Mon Jun 12, 2006 1:20 pm
Location: Tampa, FL

Re: [1.3.56+] foreach() and c() not playing nice

Post by Azhrei »

Yeah, the obvious workaround is to use the FOR() loop instead of COUNT().

It's certainly possible to have a different variable prefix for each loop type but that could break an awful lot of existing macro code. :(

The other solution (which can't break any existing code) is to scope the roll.count variable to the enclosing set of braces. However, I have no idea how the loops are structured inside MapTool so such scoping may not even be possible. If it is possible, this fix preserves backward compatibility (because who was doing it this already has a workaround!).

I'm sure Craig will take a look at it. But depending on the investment required, it may not be worth the effort. Instead a note about the problem and the suggestion to use FOR() instead of COUNT() can be made on the wiki.

User avatar
Mr. Pokeylope
Giant
Posts: 118
Joined: Mon Aug 11, 2008 9:24 pm

Re: [1.3.56+] foreach() and c() not playing nice

Post by Mr. Pokeylope »

I'm working on a fix, but it looks like I'll have to make some modifications to the parser library. Should have a patch sometime this weekend.

User avatar
plothos
Great Wyrm
Posts: 1890
Joined: Sat Jun 21, 2008 1:07 am

Re: [1.3.56+] foreach() and c() not playing nice

Post by plothos »

Wow, thanks. :D
Drop-In Utilities:
My Spell Manager for D&D3.5 and PFRPG
My Inventory Manager for D&D and PFRPG, but more generally useable than that.
My Message Manager -- My Top-Down D&D Token Images
and my Custom Initiative & Status/Spell-Effect Tracker (work in progress, but functional).

Post Reply

Return to “Bug Reports”