RPTools.net

Discussion and Support

Skip to content

It is currently Sun Dec 10, 2017 9:23 pm 






Reply to topic  [ 24 posts ]  Go to page Previous  1, 2

Previous topic | Next topic 

  Print view

Author Message
User avatar  Offline
Site Admin
 
Joined: Mon Jun 12, 2006 12:20 pm
Posts: 12103
Location: Tampa, FL
 Post subject: Re: [MapTool] JSON Tutorials
PostPosted: Wed Oct 20, 2010 12:47 pm 
Initializing an empty JSON and appending to a JSON Array are both covered on the wiki page for Wiki: json.set(). Check out the examples, then compare them to the syntax described at the top of the page...


Top
 Profile  
 
 Offline
Kobold
 
Joined: Sun Oct 17, 2010 9:38 am
Posts: 7
 Post subject: Re: [MapTool] JSON Tutorials
PostPosted: Wed Oct 20, 2010 3:42 pm 
I must be missing something. I don't see anything on that page that describes what you're saying.

There's three examples, and they all put data into the initial array.

Maybe I'm just not understanding the way the documentation is written. Also, that page doesn't mention json.append at all. I guess I don't need to use it?

edit:

Okay. I think I figured it out, via trial and error.
Code:
[h:allDice = '[]']


That initializes an empty array. The documentation said "You can use an empty string ("") to represent a new JSON Array or JSON Object.", but when I did that, I got a new array where the first item was an empty string.

Then, I can make sequential calls like:
Code:
[h:allDice=json.append(allDice,"some value")]


to add values as I loop through my loop.

Sorry for all the confusion. It's a wiki, I'm guessing it's okay if I go add a line or two to clarify the whole.. 'how to predeclare and initialize an empty array' thing?


Top
 Profile  
 
User avatar  Offline
Deity
 
Joined: Tue Jul 01, 2008 6:48 pm
Posts: 6237
 Post subject: Re: [MapTool] JSON Tutorials
PostPosted: Thu Oct 21, 2010 2:25 pm 
It is okay to edit the wiki. Basically, when "pre-declaring" or initializing a JSON array without adding anything to it when you create it, you can say:

Code:
[h:array = ""]


or

Code:
[h:array = "[]"]


The preceding two examples just say "there is a variable 'array' with that value." Later, when you use the json.append() function, it will see those values and say "ah, okay, you intend those to be blank JSON arrays and all is well with the world."

If you want to initialize it and add things to it all at once, you can instead do:

Code:
[h:array = json.append("", 1, 2, 3, "a", 9, "q", "meatball")]


or

Code:
[array = json.append("[]", 1, 2, 3, "a", 9, "q", "meatball")]


In the immediately preceding two examples, both the "" and the "[]" tell the function to create a new JSON array and at the same time, add those 7 elements to it.

_________________

What I'm Working On

MapTool Tutorials:
Introduction to Tokens
Introduction to Properties
Introduction to Macro Writing
Introduction to Light and Sight


Top
 Profile  
 
 Offline
Kobold
 
Joined: Sun Oct 17, 2010 9:38 am
Posts: 7
 Post subject: Re: [MapTool] JSON Tutorials
PostPosted: Sun Oct 24, 2010 8:21 am 
Rumble wrote:
It is okay to edit the wiki. Basically, when "pre-declaring" or initializing a JSON array without adding anything to it when you create it, you can say:

Code:
[h:array = ""]


or

Code:
[h:array = "[]"]


The preceding two examples just say "there is a variable 'array' with that value." Later, when you use the json.append() function, it will see those values and say "ah, okay, you intend those to be blank JSON arrays and all is well with the world."


Thank you! .. Yes, that's what I figured out, by trial and error. Is there any difference between them at all?


Top
 Profile  
 
User avatar  Offline
Deity
 
Joined: Tue Jul 01, 2008 6:48 pm
Posts: 6237
 Post subject: Re: [MapTool] JSON Tutorials
PostPosted: Sun Oct 24, 2010 11:01 am 
Raphael diSanto wrote:
Rumble wrote:
It is okay to edit the wiki. Basically, when "pre-declaring" or initializing a JSON array without adding anything to it when you create it, you can say:

Code:
[h:array = ""]


or

Code:
[h:array = "[]"]


The preceding two examples just say "there is a variable 'array' with that value." Later, when you use the json.append() function, it will see those values and say "ah, okay, you intend those to be blank JSON arrays and all is well with the world."


Thank you! .. Yes, that's what I figured out, by trial and error. Is there any difference between them at all?




I can think of one offhand: if you want to see what type of JSON thing it is - an array or an object - the double quotes won't come back as a known JSON type, while "[]" will.

That is, there are some functions like Wiki: json.type() that tell you whether a particular variable is a JSON Array, a JSON Object, or an "unknown" type (e.g., not JSON anything) - this is sometimes important in various macro techniques. So, i you use, for instance:

Code:
[h:someVariable = ""]
[r:json.type(someVariable)]


it returns UNKNOWN (even though you could use "" when creating a JSON array, the json.type function doesn't recognize it as one otherwise).

As a result, I try to always use "[]" to designate an empty JSON Array, just to keep things clear in my own head.

_________________

What I'm Working On

MapTool Tutorials:
Introduction to Tokens
Introduction to Properties
Introduction to Macro Writing
Introduction to Light and Sight


Top
 Profile  
 
User avatar  Offline
Dragon
 
Joined: Mon Feb 09, 2009 10:53 am
Posts: 419
 Post subject: Re: [MapTool] JSON Tutorials
PostPosted: Sat Dec 25, 2010 9:31 pm 
I'm bringing to life this topic again, first to thank for the past help, primarily from Rumble; and second, for some more help request.

Short question: how can I build a dialog with a switch field that picks its options from the keys of a json object (given that thay are variable in number and name, since the json object is a token property and its content varies from token to token)?

Explanation: the json obect mentioned above is a storage for weapons; each key is a weapon name, and each corresponding value is a nested json object containing all the properties of the weapon. I'm trying to build a macro (applied to selected token) that displays a dialog where the user can choose from the list of weapons stored in the already mentioned json object/property, and I can't figure out how to have the appropriate number of entries (with their appropriate names, obviously) in the dialog for the selected token.
After the choice is made and the operation confirmed, a series of commands shall be executed (these are the same for any option chosen, but of course all the final output changes accordingly to the values of the chosen weapon.

_________________
"There are many ways my Son, to find where the souls of Demons remain...
But it takes only one second of despair and of doubt until, at last, your Soul they will gain..."


Top
 Profile  
 
 Offline
Kobold
 
Joined: Fri Aug 22, 2014 3:36 am
Posts: 15
Location: Ancona, Italy
 Post subject: Re: [MapTool] JSON Tutorials
PostPosted: Fri Apr 03, 2015 5:26 am 
As long as English is not my first language It's really hard for me to get into JSON things... so I wonder if a little example based on what I need could help me understand all the whole thing. That's what I need to do.

In my game I have a few different races that have different bonuses when you create a character.

Let's say for example that you have:

human
Str +5
Cos +2
Dex +3

dwarf
Str +3
Cos +5
Dex +2

that's what i tried (and does not work)
Code:
[h: human = json.set("{}", "Str", 5, "Cos", 2, "Dex", 3)]
[h: dwarf = json.set("{}", "Str", 3, "Cos", 5, "Dex", 2)]

[h: input("race|human,dwarf|Choose your race|LIST| VALUE=STRING")] 

<h1><b>[r: race]</b></h1>
<h2>Modifier</h2><br>

Str: [r: json.get(race, "Str")] <br>
Cos: [r: json.get(race, "Cos")] <br>
Dex: [r: json.get(race, "Dex")] <br>


There is obviously something I missunderstood. When I choose my race Json.get don't recognise "race" as a variable... I guess it search for an array named "race" that doesn't exist. Can anyone lead me to the correct syntax to achieve this? So I can make the "bigger work" on my own... withouth bothering you all :D


Top
 Profile  
 
User avatar  Offline
Dragon
 
Joined: Mon May 31, 2010 6:03 am
Posts: 515
Location: Blagnac, France
 Post subject: Re: [MapTool] JSON Tutorials
PostPosted: Fri Apr 03, 2015 9:33 am 
The reason is your "race" variable is defined in the Wiki: input() instruction as the receiving variable, meaning it's just a string, as implied by the VALUE=STRING. Thus you're trying to do a Wiki: json.get() on a string variable, not a JSON one.

Maybe something along:
Code:
Str: [r: json.get(eval(race), "Str")]
…could do the trick?


Top
 Profile  
 
 Offline
Kobold
 
Joined: Fri Aug 22, 2014 3:36 am
Posts: 15
Location: Ancona, Italy
 Post subject: Re: [MapTool] JSON Tutorials
PostPosted: Sat Apr 04, 2015 4:58 am 
JML wrote:
…could do the trick?


Thank you so much... I know something new... this way work perfectly... I apreciate the help ^_^


Top
 Profile  
 
Display posts from previous:  Sort by  
Reply to topic  [ 24 posts ]  Go to page Previous  1, 2

You cannot post new topics in this forum
You cannot reply to topics in this forum
You cannot edit your posts in this forum
You cannot delete your posts in this forum
You cannot post attachments in this forum

Search for:

Who is online

In total there are 2 users online :: 0 registered, 0 hidden and 2 guests (based on users active over the past 5 minutes)
Most users ever online was 243 on Sun Nov 04, 2012 6:14 am

Users browsing this forum: No registered users and 2 guests





Powered by phpBB © 2000, 2002, 2005, 2007 phpBB Group

Style based on Andreas08 by Andreas Viklund

Style by Elizabeth Shulman