Odd if-listAppend results

Discuss macro implementations, ask for macro help (to share your creations, see User Creations, probably either Campaign Frameworks or Drop-in Resources).

Moderators: dorpond, trevor, Azhrei, giliath, jay, Mr.Ice

Post Reply
User avatar
RedDog
Dragon
Posts: 393
Joined: Sat Jan 05, 2008 10:02 pm
Location: Clearwater, FL

Odd if-listAppend results

Post by RedDog »

I tried using listAppend to generate a list based on active states, but I am questioning that the results are what is intended.

This is my code that checks a list of states against the active states on a selected token

Code: Select all

[H: tTokenID=getSelected()]
[H: tStateInfo=""]
[H: tStateIcons=""]
[H: tStateList="Blind,Dazed,Deaf,Disabled,Diseased,Hidden,Enraged,Flying,Held,Hidden"]
[R, FOREACH(tIndex, tStateList), CODE: {	
	[if(getState(tIndex, tTokenID), listAppend(tStateInfo, tIndex), "")]
	}]
[R: tStateInfo]
I ran the above code in chat while selecting a token with the Blind, Deaf, and Held states activated.
The result is: Blind, , Deaf, , , , , , Held,

Despite the use of an IF statement, it appears that the listAppend is triggered on every FOREACH cycle regardless if a match occurs. I believe my code is fine. That really feels like a bug and not an intended result. I expected a clean list like "Blind, Deaf, Held".

Anyone have an insight? Perhaps my use of listAppend is not correct...?

User avatar
Full Bleed
Demigod
Posts: 4736
Joined: Sun Feb 25, 2007 11:53 am
Location: FL

Re: Odd if-listAppend results

Post by Full Bleed »

This works:

Code: Select all

[H: tTokenID=getSelected()]

[H: tStateInfo=""]
[H: tStateIcons=""]

[H: tStateList="Dead, Disabled, Hidden, Prone"]

[h, FOREACH(state, tStateList, ""), CODE:
	{	
		[h: tStateInfo = if(getState(state, tTokenID) == 1, listAppend(tStateInfo, state), tStateInfo)]
	}
]

[h: broadcast(tStateInfo)]
Maptool is the Millennium Falcon of VTT's -- "She may not look like much, but she's got it where it counts."

User avatar
wolph42
Winter Wolph
Posts: 9999
Joined: Fri Mar 20, 2009 5:40 am
Location: Netherlands
Contact:

Re: Odd if-listAppend results

Post by wolph42 »

To explain: no bug, you're code is wrong!

The core lesson: All maptool functions RETURN a result and NEVER ASSIGN a result to a variable! Hence that last part you need to do yourself.

In this case your listAppend result is never assigned to a value so gets lost each time. That is tStateInfo remains "" the entire time, you only see the result of r,foreach() loop. If you hide that, so h,foreach() you'll see that the result is "" (empty).

And yes, if you're a coder this is frikkn counterintuitive, I still make this mistake from time to time!

fullbleed showed you the right path :D

User avatar
RedDog
Dragon
Posts: 393
Joined: Sat Jan 05, 2008 10:02 pm
Location: Clearwater, FL

Re: Odd if-listAppend results

Post by RedDog »

wolph42 wrote:
Mon Oct 18, 2021 6:11 am
To explain: no bug, you're code is wrong!

The core lesson: All maptool functions RETURN a result and NEVER ASSIGN a result to a variable! Hence that last part you need to do yourself.
And yes, if you're a coder this is frikkn counterintuitive, I still make this mistake from time to time!
LOL, Yeah, I am a coder (but still shaking off some of the rust) and when I saw my results I thought "Well, that doesn't seam right".

Thanks again wolph and FullBleed.

Post Reply

Return to “Macros”