Adding information as the first entry of a json array
Moderators: dorpond, trevor, Azhrei, giliath, jay, Mr.Ice
-
- Cave Troll
- Posts: 54
- Joined: Fri Jul 02, 2010 4:53 am
Adding information as the first entry of a json array
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?
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?
Re: Adding information as the first entry of a json array
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 likeHaven't tested it, but I think that would work.
So it'd be something like
Code: Select all
[newarray = json.set("[]", newvalue)]
[listtoappend = json.toList(oldarray)]
[newarray = json.append(newarray, listtoappend)]
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).
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).
-
- Cave Troll
- Posts: 54
- Joined: Fri Jul 02, 2010 4:53 am
Re: Adding information as the first entry of a json array
I think this should work for me.
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.
Code: Select all
[h: isVisible = json.append("no target selected", getVisibleTokenNames())]
Do you think this will work? Otherwise I will try your code instead.
-
- Dragon
- Posts: 317
- Joined: Fri Apr 23, 2010 8:17 pm
Re: Adding information as the first entry of a json array
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
Re: Adding information as the first entry of a json array
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.Hekau Chasut wrote:I think this should work for me.
if "getVisibleTokenNames" in "json.append" is no problem, this should work just like one of the examples the wiki provides for json.appendCode: Select all
[h: isVisible = json.append("no target selected", getVisibleTokenNames())]
Do you think this will work? Otherwise I will try your code instead.
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).
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).
Re: Adding information as the first entry of a json array
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).
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).
Re: Adding information as the first entry of a json array
I don't expect this will work, nor would it give you what you are looking for:plothos wrote:Code: Select all
[h: isVisible=json.set("[]", "no target selected")] [h: isVisible=json.append(isVisible, getVisibleTokenNames())]
- 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 ]
[ d20 StatBlock Importer ] [ Batch Edit Macros ] [ Canned Speech UI ] [ Lib: Math ]
Re: Adding information as the first entry of a json array
Sorry, yeah, what I posted I should have tested.
This works, and has been tested.
But json.merge would probably be better and faster.
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)]
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).
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).
-
- Cave Troll
- Posts: 54
- Joined: Fri Jul 02, 2010 4:53 am
Re: Adding information as the first entry of a json array
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"?
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"))]
Re: Adding information as the first entry of a json array
- Wiki: json.merge() - append 2nd array to 1st array
- Wiki: json.union() - combine 2 arrays where values in second array are added to values in 1st array but not duplicated.
- Wiki: json.intersect() - Only the values that appear in both array are returned in new array
- Wiki: json.difference() - And my favorite, remove items from first array that appear in 2nd array.
Downloads:
- Notepad++ MapTool addon
- RPEdit details (v1.3)
- Coding Tips: Modularity and Design
- Videos: Macro Writing Tools
-
- Dragon
- Posts: 317
- Joined: Fri Apr 23, 2010 8:17 pm
Re: Adding information as the first entry of a json array
YesHekau Chasut wrote:Using json.merge my first entry "no target selected" will have to be an array as well?!
Like this:Hekau Chasut wrote:How can I create an array with the only information "no target selected"?
Code: Select all
json.append("[]", "no target selected")
"He knows not how to know who knows not also how to un-know." --Sir Richard Burton