Adding information as the first entry of a json array

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
Hekau Chasut
Cave Troll
Posts: 54
Joined: Fri Jul 02, 2010 4:53 am

Adding information as the first entry of a json array

Post by Hekau Chasut »

Hallo,

I learned a lot from you in the last days about how macros work, therefore I hope that you can help me another time.

I am building an attack-macro with a lot of stuff integrated. One thing is choosing an aim for the attack in an input-window. That has been no problem with the help of the wiki. I used getVisibleTokenNames("json") to create an array for this purpose. Now I want to add another entry to the Json-array. The entry should be defined in the macro itself and be the first entry of the array.
With this I want to add the possibility to use the attack macro without a predefined aim. This could be useful for example if someone wants to shoot onto an enemy he/she cannot see.

Do you have any ideas?

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

Re: Adding information as the first entry of a json array

Post by plothos »

One thing you could do is turn the value you want to add into a json array and then append the other stuff to it. I'm not sure if json.append() takes jsons as the added value, but if not, you can convert the first array into a list with json.toList().

So it'd be something like

Code: Select all

[newarray = json.set("[]", newvalue)]
[listtoappend = json.toList(oldarray)]
[newarray = json.append(newarray, listtoappend)]
Haven't tested it, but I think that would work.
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).

Hekau Chasut
Cave Troll
Posts: 54
Joined: Fri Jul 02, 2010 4:53 am

Re: Adding information as the first entry of a json array

Post by Hekau Chasut »

I think this should work for me.

Code: Select all

[h: isVisible = json.append("no target selected", getVisibleTokenNames())]
if "getVisibleTokenNames" in "json.append" is no problem, this should work just like one of the examples the wiki provides for json.append

Do you think this will work? Otherwise I will try your code instead.

prestidigitator
Dragon
Posts: 317
Joined: Fri Apr 23, 2010 8:17 pm

Re: Adding information as the first entry of a json array

Post by prestidigitator »

I don't think Wiki: json.append() is what you want. If you try to append a JSON array to a JSON array, the added array will be the last element of the result (rather than the result containing all the elements from both). What you can do to add an element to the beginning of a JSON array is use Wiki: json.merge() as follows:

Code: Select all

arr = json.merge(json.append("[]", newFirstElement), arr)
"He knows not how to know who knows not also how to un-know." --Sir Richard Burton

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

Re: Adding information as the first entry of a json array

Post by plothos »

Hekau Chasut wrote:I think this should work for me.

Code: Select all

[h: isVisible = json.append("no target selected", getVisibleTokenNames())]
if "getVisibleTokenNames" in "json.append" is no problem, this should work just like one of the examples the wiki provides for json.append

Do you think this will work? Otherwise I will try your code instead.
I see better what you're trying to get and out of what. Yeah, that will almost work. getVisibleTokenNames() should return a list and that can be appended to an array, but what you've got there at the start is not an array, but just what you want to be an element in the array.

This should work for ya.

Code: Select all

[h: isVisible=json.set("[]", "no target selected")]
[h: isVisible=json.append(isVisible, getVisibleTokenNames())]
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
plothos
Great Wyrm
Posts: 1890
Joined: Sat Jun 21, 2008 1:07 am

Re: Adding information as the first entry of a json array

Post by plothos »

json.merge(), Prest is right, would be another option, but you could do it either way, since getVisibleTokenNames(), I believe, can return either a list or an array.
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
biodude
Dragon
Posts: 444
Joined: Sun Jun 15, 2008 2:40 pm
Location: Montréal, QC

Re: Adding information as the first entry of a json array

Post by biodude »

plothos wrote:

Code: Select all

[h: isVisible=json.set("[]", "no target selected")]
[h: isVisible=json.append(isVisible, getVisibleTokenNames())]
I don't expect this will work, nor would it give you what you are looking for:
- The first line has a small syntax problem (json.set is missing the second argument for the index, or should just be json.append).
- The second line will produce a nested json array: the first index will contain the string "no target selected", while the next item will contain a string list returned by getVisibleTokenNames (or another array if the 'json' delimiter is used). I suppose you could convert this to a string list with json.toList, but it's potentially messy.

prestidigitator's suggestion of json.merge is probably safer, and I would recommend that approach if you want to use json arrays.
If you want to use string lists, you can use listAppend:

Code: Select all

[H: isVisible = listAppend( "no target selected" , getVisibleTokenNames() ) ]
"The trouble with communicating is believing you have achieved it"
[ d20 StatBlock Importer ] [ Batch Edit Macros ] [ Canned Speech UI ] [ Lib: Math ]

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

Re: Adding information as the first entry of a json array

Post by plothos »

Sorry, yeah, what I posted I should have tested.

This works, and has been tested. :)

Code: Select all

[h: isVisible=json.append("[]", "no target selected")]
[h, foreach(element, getVisibleTokenNames()):  isVisible=json.append(isVisible, element)]
But json.merge would probably be better and faster.
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).

Hekau Chasut
Cave Troll
Posts: 54
Joined: Fri Jul 02, 2010 4:53 am

Re: Adding information as the first entry of a json array

Post by Hekau Chasut »

I think faster is my keyword... i fear my macro becoming to extensive, so I think I will use the json array.

But I have two more questions about that:

looking up json.merge I found json.union. What is the difference?

Using json.merge my first entry "no target selected" will have to be an array as well?! I fear Maptool will not automatically make it an array if I use this code.
How can I create an array with the only information "no target selected"?

Code: Select all

[h: isVisible = json.merge("no target selected", getVisibleTokenNames("json"))]

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

Re: Adding information as the first entry of a json array

Post by aliasmask »


prestidigitator
Dragon
Posts: 317
Joined: Fri Apr 23, 2010 8:17 pm

Re: Adding information as the first entry of a json array

Post by prestidigitator »

Hekau Chasut wrote:Using json.merge my first entry "no target selected" will have to be an array as well?!
Yes
Hekau Chasut wrote:How can I create an array with the only information "no target selected"?
Like this:

Code: Select all

json.append("[]", "no target selected")
Like in my first example above.
"He knows not how to know who knows not also how to un-know." --Sir Richard Burton

Post Reply

Return to “Macros”