Can UDF return a JSON array?

Thoughts, Help, Feature Requests, Bug Reports, Developing code for...

Moderators: dorpond, trevor, Azhrei

Forum rules
PLEASE don't post images of your entire desktop, attach entire campaign files when only a single file is needed, or generally act in some other anti-social behavior. :)
Post Reply
busterbluth
Cave Troll
Posts: 30
Joined: Sat Aug 17, 2019 3:49 pm

Can UDF return a JSON array?

Post by busterbluth »

I'm thinking the answer is yes, but here's why I'm asking:

I've got a superhero game where all the Powers, Skills, etc are in a JSON array of JSON objects, stored on the token in a property called Specs. This works well, but there's times when powers and ability scores can fluctuate (think of the Hulk, for example). I'd like the actual value of Specs to be a function call, where baseSpecs is defined at character creation and tempSpecs is populated on the fly as needed. The UDF would then read baseSpecs and tempSpecs and then add the rank values together as needed.

Is this do-able or am I on track to make a mess?

User avatar
aliasmask
RPTools Team
Posts: 9024
Joined: Tue Nov 10, 2009 6:11 pm
Location: Bay Area

Re: Can UDF return a JSON array?

Post by aliasmask »

Yes, just use [H: macro.return = mergeSpecs] at the end of your function where mergeSpecs can be any value including an array of objects.

busterbluth
Cave Troll
Posts: 30
Joined: Sat Aug 17, 2019 3:49 pm

Re: Can UDF return a JSON array?

Post by busterbluth »

Yeah, this works beautifully, but I need to add another bit of logic and am thinking that json.union and/or .merge may be useful.

I've got powers defined at character creation in a property named defSpecs. Then, powers gained or adjusted during play go into tmpSpecs. The keys are identical for all objects in both arrays. I need Specs (the result of the above mentioned UDF) to show me the net of these 2 JSON arrays.

Example: Parasite drains Invulnerability from Superman over 2 different rounds, say 5 ranks in round 1 and 3 in round 2. Supes is defined as having 22 ranks of Invulnerability. So his defSpecs array has Invulnerability with a Rank of 22, but his tmpSpecs array has two entries: Invulnerability with -5 ranks and Invulnerability with -3 ranks.

I'd like Specs to show the Invulnerability power with 17 ranks (22 minus 5 and minus 3). I know that JSON.merge will mash the permanent and temporary arrays together but will only show the Rank value for the last found object. In other words, .merge won't actually add the ranks.

My guess is that this needs to be broken down like this:

1) JSON.Union def and tmp into a new array (uniSpec), to get a list of unique powers
2) Do a nested foreach on uniSpec for both def and tmp; where the power name matches, add the ranks and update the object in uniSpec

But maybe there's a more efficient way to do this or there exists a pre-defined JSON function that will add a specific key for multiple JSON arrays?

User avatar
aliasmask
RPTools Team
Posts: 9024
Joined: Tue Nov 10, 2009 6:11 pm
Location: Bay Area

Re: Can UDF return a JSON array?

Post by aliasmask »

You'll want to use a json.path.read to find the max value with the same mod type. I have an example in discord here: https://discord.com/channels/2962308222 ... 1144419369

Post Reply

Return to “MapTool”