Appending a JSON object to a JSON array token property

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
ziltmilt
Dragon
Posts: 331
Joined: Sun Apr 29, 2007 9:28 pm
Contact:

Appending a JSON object to a JSON array token property

Post by ziltmilt »

I'm a little rusty with my Maptool macro syntax, and I'm working on a framework for d6 Star Wars. One of the campaign properties is a list of JSON objects representing ranged weapons. I've got a table of JSON objects, with each row representing a single ranged weapon.

This macro cycles through the table, presents a LIST of weapons, and then is supposed to add the selected weapon to the selected token. It works fine, except that it always adds the weapon from row #2 of my table. In other words, the variable weaponNum is always set to = 1. BTW, the variable rangedWeapons is the token's property, a JSON array holding a list of JSON object.

Code: Select all

[h: rangedWeaponsAll = ""]
[h: wpnName = ""]
[h, for(x, 1, 7, 1, ""), code: {

   [h: wpnRow = table("RangedWeapons", x)]
   [h: wpnName = wpnName + "," + json.get(wpnRow, "WeaponName")]
	<!-- rangedWeaponsAll is a JSON array of all Ranged Weapons -->       
   [h: rangedWeaponsAll = json.append(rangedWeaponsAll, table("RangedWeapons", x))]

}]

[h: wpnName = trim(wpnName)]
[h: wpnName = substring(wpnName, 1, length(wpnName))]

[h: weaponNum=input("varName|" + wpnName + "|Select Ranged Weapon to Add to Token|LIST|SELECT=0")]
[h: RangedWeapons = json.append(rangedWeapons, json.get(rangedWeaponsAll, weaponNum))]

I can't figure out why it would do this. the INPUT function is supposed to return a number representing the selecting, starting at an index of 0. Any idea what I'm doing wrong? Thanks in advance.

ziltmilt
Dragon
Posts: 331
Joined: Sun Apr 29, 2007 9:28 pm
Contact:

Re: Appending a JSON object to a JSON array token property

Post by ziltmilt »

Yeah, I'm still stumped. This always returns a value of 1 as well:

Code: Select all

[h: var = 0]
[h: var=input("varName|a,b,c,e,f|Choose a letter|LIST")]
{var}

User avatar
aliasmask
RPTools Team
Posts: 9031
Joined: Tue Nov 10, 2009 6:11 pm
Location: California

Re: Appending a JSON object to a JSON array token property

Post by aliasmask »

ziltmilt wrote:Yeah, I'm still stumped. This always returns a value of 1 as well:

Code: Select all

[h: var = 0]
[h: var=input("varName|a,b,c,e,f|Choose a letter|LIST")]
{var}
input is a function that returns 1 or 0. It returns 1 if you click okay and 0 if you click cancel. That way you know if someone entered input. The value you're looking for is varName which holds the index of your list. You can have it return the value by adding to the end |VALUE=STRING after LIST.

This is how I usually do it.

Code: Select all

[H: myList = "a,b,c,e,f"]
[H: abort(input(strformat("index|%{myList}|Choose a letter.|LIST")))]
[H: letter = listGet(myList,index)]
[R: letter]
OR

Code: Select all

[H: myList = "a,b,c,e,f"]
[H: abort(input(strformat("letter|%{myList}|Choose a letter.|LIST|VALUE=STRING")))]
[R: letter]
The difference is that my list could be a nicely formatted list specifically for the input functions output and I use the index for another list that correlates to the input to get my value. For example

Code: Select all

[H: myList = "a,b,c,e,f"]
[H: resultList = "A is for Apple,B is for Ball,C is for Cat,E is for Elephant,F is for Falcon"]
[H: abort(input(strformat("index|%{myList}|Choose a letter.|LIST")))]
[H: result = listGet(resultList ,index)]
[R: result]

ziltmilt
Dragon
Posts: 331
Joined: Sun Apr 29, 2007 9:28 pm
Contact:

Re: Appending a JSON object to a JSON array token property

Post by ziltmilt »

Thank you! Once again, I'm reminded of how awesome this community is.

Post Reply

Return to “Macros”