#How to dynamically set the target of a json.set?

1 messages · Page 1 of 1 (latest)

worthy junco
#

The token has three properties: Primary, Secondary and Tertiary. Each of those properties is a JSON object, containing information about the token's weapons.
I am making a macro that allows for easy editing of those properties.

Below is my code:

[h: new_name = "Chester-12"]

[h: selected_gun = json.set("", "name", new_name)]

The intended function is for the json.set to affect the Primary property - or whatever selected_gun is set to -, but... no worky, obviously - it just turns selected_gun into a JSON object of its own.

I've also tried:

``` but still no luck - I have no clue what happens in that scenario.

How would I need to set it up so that the json.set applies to whatever the actual value of selected_gun is?
Failing that: is there any other elegant way to do this?

I know I could just use some if statements - if selected_gun == "Primary", then Primary = json.set(...) and so on -, or a switch roll option achieving much the same, but that doesn't *feel* right, surely there's a proper way to do this?

(please ping with any responses)
lusty totem
#

So you have a property called "Primary", "Secondary", "Tertiary", and "selected_gun" on your token, you want the value of "selected_gun" to point to one of the other properties, so that when you run json.set("", "name", new_name") the property that gets assigned this value is the property pointed by "selected_gun".

To do that instead you should use [h: setProperty(selected_gun, json.set( "", "name", new_name)) ]

little plank
#

There's quite a couple of issues with your code
selected_gun="Primary"
does not retrieve the property Primary, it sets it to a string value containing "Primary". You want to set it to Primary without quotes or use getProperty("Primary")
selected_gun=json.set("","name",new_name)
creates a new json instead of modifying an existing json because of the "" first parameter
You want to replace that with selected_gun=json.set(selected_gun,"name",new_name)

and if you want that to update the token property you need to set assign it back to the token property, either by using the property name shortcut Primary=selected_gun or by using setProperty()

worthy junco