Getting vars from 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
EpsilonShadow
Kobold
Posts: 6
Joined: Mon Feb 15, 2016 7:26 am

Getting vars from json array

Post by EpsilonShadow »

I'm working on inventory manager for my game. Characters inventory is in properties named "Inventory" and "Storage". First one is for what characters have on them and second one for all things they left home or on the ground. Right now I need a way to move items from one property to another. I've used input() function, but stumbled on a problem. I dont know how to make variables from json arrays or strings.
Here is what I have so far:

Code: Select all

[h: textVar = json.append("", "")]
[h: valueList = json.append("", "")]
[h,FOREACH(itemName, Inventory), code: { 
                          [textVar = json.append(textVar, itemName+"Check|0|"+itemName+"|CHECK")]
                          [valueList = json.append(valueList, itemName+"Check")] } ]

[h: input(json.toList(textVar, "##")) ]
It creates two arrays. One with code for input, and other with variables that input changes. My idea is to delete items from one property and add to another using generated vars from the input, so players could take items from their "storage" or live there.

PS. Sorry for my English aaaand my coding. I've never coded anything before.

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

Re: Getting vars from json array

Post by aliasmask »

What I would do is use a json object. The key would be the variable name to refer to your data and the value will be your inventory information. This could be just plain text, a string prop or even another json object.

In order to create a key to be a valid variable name, but still be recognizable and converted from the inventory name you will need to filter the item name. For example:

Code: Select all

[H: inventoryName = "Greataxe +1"]
[H: varName = "item." + replace(lower(inventoryName),"[^a-z0-1_.]","") + ".uid" + d1000000000]
What I did is strip out all the characters that don't show up in a variable name and create a unique id so you can have multiple greataxe +1's. The "item." prefix could be there to help you sort and could be there to help identify the category of the item. You would need to save the original name in the data. If you can avoid ";" and quotes in your data using a string prop will work, but to avoid the hassle I would just use another json object for your data. It is assumed that an inventory item will start with a letter, but if not adding the prefix helps prevent naming weirdness.

The next step is putting in your data.

Code: Select all

[H: inventoryText = "A very large axe, made for hurting things, badly."]
[H: item = json.set("{}","flavorText",inventoryText,"name",inventoryName,"damage","1d12")]
[H: inventory = json.set("{}",varName,item)]
You could sort your data.

Code: Select all

[H: sortedInventory = json.sort(inventory,"a","name")]
You could make all the data from an item a local variable.

Code: Select all

[H: item = json.get(inventory,varName)]
[H: item.strprop = json.toStrProp(item)]
[H: varsFromStrProp(item.strprop)]

EpsilonShadow
Kobold
Posts: 6
Joined: Mon Feb 15, 2016 7:26 am

Re: Getting vars from json array

Post by EpsilonShadow »

That's not solving my problem.
Idea is:
1- player have inventory and storage.
2- player want to take something from storage or put something in there, so he press "storage management" button in campaign macros window.
3- window/dialogue pop up with list of what he have and checkboxes near items.
4- player click checkboxes next to items he want to take with him or put away.
5- window closes and items are transfered.

I have a problem with last part. I need to generate lists of items for many other needs. There is more than several hundred items and I dont want to code vars for every last one of them. I want to use FOREACH() to make macros lighter and right now trying to use input() function. Is it possible to use key from json object as value for input line? Maybe it's better to use dialog() roll option, instead of input()?

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

Re: Getting vars from json array

Post by aliasmask »

Perhaps you're looking for Wiki: switchToken(). Sounded like you didn't know how to create a dynamic variable name. What you need to do is getProperty from one player, change current token then setProperty to other player. The macro will need to be on a library token to handle the permissions. By default, one player can't modify another player's token.

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

Re: Getting vars from json array

Post by wolph42 »

could you give us an example perhaps in semi code of what it is what you wish to achieve. Be concrete so e.g.
token "John" has properties "StuffOnMe" and "StuffAtHome".
StuffOnMe: {coat:1, gold:20, etc.}
StuffAtHome: {hat:2, copper:100}
now I want to let the token owner move 1 hat from "home" to "me" in an interactive way (e.g. through input).
like that.

Post Reply

Return to “Macros”