(solved) How to get an json-object list as 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
Mastyrial
Kobold
Posts: 14
Joined: Fri Mar 09, 2012 10:24 am

(solved) How to get an json-object list as token property?

Post by Mastyrial »

This is what I have
In my campaign, I have a token-property named "listOfObjects", which is supposed to contain one or more json-objects.
Example of the value of listOfObjects:
id=Dark Knight; x=1;y=2
The idea is to have a token's name and x/y-positions of that token.

My macro (for one single json object):

Code: Select all

[h: string = getProperty("ListOfObjects", "tokenWithListOfObjects" ) ]
[h: myJson = json.fromStrProp(string)]
[r:json.get(myJson, "id")]<br>
[r:json.get(myJson, "x")]<br>
[r:json.get(myJson, "y")]<br>
Result:
Dark Knight
1
2


That's fine: I can read the token's name and his x- and y-positions.

My Problem
Now I want to expand this to have more than one json-object in my listOfObjects.
But I do not get the syntax right. I tried several variations with brackets [ ], {}, with and without quotation marks, with id:Dark Knight and id=Dark Knight, with "," instead of ";" asf. Nothing worked so far - and the wiki has confused me more on this subject than it helped.

So, how does a list of json objects of the type [id=<String/Tokenname>, x=<number>, y=<number>] look like? Is it possible to read a list of json objects from a token-property-string?
My goal is to work on this list with a foreach-loop and send every token on this list to the given x- and y-position.

Greetings, Mastyrial
Last edited by Mastyrial on Sun Jan 25, 2015 5:28 am, edited 1 time in total.

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

Re: How to get an json-object list as token property?

Post by wolph42 »

its not entirely clear what you want. apparently you wnat nested json objects? but you want to construct them by building a string? or by using the proper json functions?

overall, its does not really matter. What really matters is if you could explain *what* it is you want to achieve. that helps us understand and then we can tell you whether nested json objects are the way to go, or that you should e.g. use strprops, or arrays, or lists, etc.

Mastyrial
Kobold
Posts: 14
Joined: Fri Mar 09, 2012 10:24 am

Re: How to get an json-object list as token property?

Post by Mastyrial »

Sorry for the unclear description.

I imagine something like this as a token-property value (written by hand):
{id=Token1, x=1, y=1}, {id=Token2, x=10, y=5}, {id=Token7, x=-4, y=80}

I want to read this list and send each token to it's location:
Token1 => send to 1,1
Token2 => send to 10, 5
Token7 => send to -4,80

I have thought that a json-object would fit for this? That I could use a foreach on each item of this list?

Mastyrial
Kobold
Posts: 14
Joined: Fri Mar 09, 2012 10:24 am

Re: (solved) How to get an json-object list as token propert

Post by Mastyrial »

I found a way to make it work: Using regular expressions.
Not the most elegant way, I guess, but it works.


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

Re: (solved) How to get an json-object list as token propert

Post by aliasmask »

You're talking about having an array of objects. If you want to create it manually, you have to use the correct syntax. But I recommend building your data using the MT functions.

Syntax

Code: Select all

[{"key1":"value1","key2":1},{"key1":"value2","key2":2}]
Using json functions:

Code: Select all

[H: setProperty(propName,json.append("",
    json.set("{}","key1","value1","key2",1),
    json.set("{}","key1","value2","key2",2)
))]
I usually have a macro to build my data if it's a one time thing and call it something like initData.

Mastyrial
Kobold
Posts: 14
Joined: Fri Mar 09, 2012 10:24 am

Re: (solved) How to get an json-object list as token propert

Post by Mastyrial »

Thank you both.
I'll try that the next time I extend my "click button to fill a room with all needed tokens/objects" kind of map.

Greetings, Mastyrial


Post Reply

Return to “Macros”