Roll20 uses cookies to improve your experience on our site. Cookies enable you to enjoy certain features, social sharing functionality, and tailor message and display ads to your interests on our site and others. They also help us understand how our site is being used. By continuing to use our site, you consent to our use of cookies. Update your cookie preferences .
×
Create a free account

New to Javascript, What am I Doing Wrong?

1594320887

Edited 1594322880
Hi everyone, I'm just starting off writing some APIs for myself.  I am trying to write a simple function that has two arguments and involves: Evaluating the first string argument, If that string is "Arty" or "Mor", return a number that is the second argument, or If the argument is not "Arty" or "Mor" return zero.  Eventually, the first argument will be from a selected token's attribute and the second the value of one of the selected token's bars.  The second argument, a number, would launch The Aaron's TokenMod to activate an aura around the token.  For now, I'm just trying to get the second argument or a zero to appear in a window alert box: function fArty(strType, intRange ) { if (strType == 'Arty' || strType == "Mor") { return window.alert(intRange); } else { return window.alert(0); } } Nothing happens when I enter it and press send: No errors are thrown in the text chat tab, but I was expecting a dialog/message box to open with 3 in it.  I've been using the W3 tutorial to get a primer on javascript.   The syntax is not setting off any errors on the API Scripts page.  The three scripts shown from the Script Library work just fine when I launch the game.  I have, of course, noticed that when there are errors detected on the API Scripts page in my syntax that there is an error message on the page and when I do launch the game, none of the Script Library works.  This is pretty similar to the MS VBA I'm most used to using. Can someone point out what I hope is not a fundamental error or misunderstanding on my part? Thanks in advance, -- Tim
1594323784
The Aaron
Roll20 Production Team
API Scripter
Window is an object created by browsers. It does not exist in the API sandbox. (Neither does the DOM). &nbsp;API scripts have more in common with Node Modules than browser scripts.&nbsp; You can use the log() function to output text to the API Console Log: function fArty(strType, intRange ) { if (strType == 'Arty' || strType == "Mor") { log(intRange); } else { log(0); } } Note that nothing is calling that function. &nbsp;You'd need to have an event handler that picks up a chat command or similar to invoke it. Here are some Roll20 specific Forum links you might find helpful for learning about writing API scripts: <a href="https://app.roll20.net/forum/post/6605115/namespaces-novice-seeks-help-exploring-the-revealing-module-pattern" rel="nofollow">https://app.roll20.net/forum/post/6605115/namespaces-novice-seeks-help-exploring-the-revealing-module-pattern</a> <a href="https://app.roll20.net/forum/post/6584105/creating-an-object-that-holds-specific-character-dot-id-and-character-name/?pagenum=1" rel="nofollow">https://app.roll20.net/forum/post/6584105/creating-an-object-that-holds-specific-character-dot-id-and-character-name/?pagenum=1</a> <a href="https://app.roll20.net/forum/post/6237754/slug%7D" rel="nofollow">https://app.roll20.net/forum/post/6237754/slug%7D</a>
I had a hard time finding the updated API documentation when I first started working with the API. This helped me a lot:&nbsp; <a href="https://roll20.zendesk.com/hc/en-us/articles/360037256714-API#api-guides-and-documentation-0-0" rel="nofollow">https://roll20.zendesk.com/hc/en-us/articles/360037256714-API#api-guides-and-documentation-0-0</a>
I can provide no help, I am just insanely interested in what you are showing. What game system are you trying to implement? (tabletop wargames are another obsession of mine) Tim M. said: Hi everyone, I'm just starting off writing some APIs for myself.&nbsp; I am trying to write a simple function that has two arguments and involves: Evaluating the first string argument, If that string is "Arty" or "Mor", return a number that is the second argument, or If the argument is not "Arty" or "Mor" return zero.&nbsp; Eventually, the first argument will be from a selected token's attribute and the second the value of one of the selected token's bars.&nbsp; The second argument, a number, would launch The Aaron's TokenMod to activate an aura around the token.&nbsp; For now, I'm just trying to get the second argument or a zero to appear in a window alert box: function fArty(strType, intRange ) { if (strType == 'Arty' || strType == "Mor") { return window.alert(intRange); } else { return window.alert(0); } } Nothing happens when I enter it and press send: No errors are thrown in the text chat tab, but I was expecting a dialog/message box to open with 3 in it.&nbsp; I've been using the W3 tutorial to get a primer on javascript.&nbsp;&nbsp; The syntax is not setting off any errors on the API Scripts page.&nbsp; The three scripts shown from the Script Library work just fine when I launch the game.&nbsp; I have, of course, noticed that when there are errors detected on the API Scripts page in my syntax that there is an error message on the page and when I do launch the game, none of the Script Library works.&nbsp; This is pretty similar to the MS VBA I'm most used to using. Can someone point out what I hope is not a fundamental error or misunderstanding on my part? Thanks in advance, -- Tim
Joshua W. said: I had a hard time finding the updated API documentation when I first started working with the API. This helped me a lot:&nbsp; <a href="https://roll20.zendesk.com/hc/en-us/articles/360037256714-API#api-guides-and-documentation-0-0" rel="nofollow">https://roll20.zendesk.com/hc/en-us/articles/360037256714-API#api-guides-and-documentation-0-0</a> Thanks so much Joshua.&nbsp; You know, I'd actually found that before, but had ignored the list of links it had.&nbsp; I had just gone through the text following and dismissed it as "OK, but where is the stuff on objects, methods, and such"&nbsp; LOL&nbsp; I googled and found the objects page with some difficulty.&nbsp; When you posted it again up there, I went through and had a eureka moment - the links I had previously ignored were actually what I desperately needed!&nbsp; So I really appreciate your comment for setting off the "ah ha" inside me.&nbsp; I tend to be slow in that regard.&nbsp; :) All the best.
Cheers! Those zendesk articles are pretty buried for new users of Roll20. They should really post a more obvious link to them in the old wiki articles.
1594488181

Edited 1594489140
Tom H. said: I can provide no help, I am just insanely interested in what you are showing. What game system are you trying to implement? Hi Tom, I have a blurb on my profile; I'm mainly a 72/76 scale modeller (not the best, mind you) who runs wargames with his figures and models.&nbsp; I'm developing something that could be scaled up or down in map scale and unit size to run a WWII armour/infantry campaign.&nbsp; My elementary school and later other friends and I have been at this since the 70s.&nbsp; Now that we are scattered all over Canada, we've had a ball with internet based campaign previously (like playing Diplomacy and resolving stand-offs with a wargame) in the past and another of my buddies ran a US Civil War campaign in the early 2000s that went well.&nbsp; Usually though when people are visiting their families here in our home city, we try to arrange a game between old and new friends.&nbsp; So I'm working on this to recreate the great fun we've had with the other on line campaigns, but with more direct involvement than previously.&nbsp; Here's a few pics from past games.&nbsp; Please forgive my vanity: &nbsp; &nbsp;&nbsp; What I am working on currently is geared to running games in Crossfire with home brew armour rules. CF is a somewhat unique WWII infantry game where a stand of figures is a squad.&nbsp; So the tokens represent companies, platoons, and occasionally squads/sections or individual vehicles. However, as I mentioned above, it could be scalable to any unit size and any set of rules, including modern, horse and musket, ancients, etc.&nbsp; I'm very much in the beginning stages, but the idea is both sides have the same map and their own tokens are blue, spotted enemy are red (standard map thing I did in my own service a century ago, I'm a bit anal about that and don't want one side to be red, the other blue or purple, though that would make things easier!).&nbsp; Hex scale is 2 km across with the standard 70 px.&nbsp; I haven't worked out a way to limit token movement and don't think I really need to do that. So both sides plot their moves using the free hand tool or polygon tool.&nbsp; I copy the named tokens (I use a button to turn off names using The Aaron's tokenmod as a macro and a few other things) onto a GM map, work out where and when contacts take place (there will be a morning, afternoon and night turn for each day), send alerts to each side so that they may choose to divert current moves, if possible (easier to do if radio equipped, of course and depending upon user side, radio issue scale - for example Allied/German armour platoons and individual tanks will have radios, but only Soviet armour companies and higher will have have them).&nbsp; This lets me figure out what and where games need to take place and when - can they be fought in the current campaign turn or the next, what artillery batteries or mortar platoons might be in range to provide indirect fire in the game. Finally, for each side, I then copy the friendly units and any enemy they can see.&nbsp; Enemy units are changed to red and with no name and no control by the players.&nbsp; That is pasted back into a new truecopy map (so both sides can track turns - I'll be archiving previous maps) and see what is going on.&nbsp; Some units like recce units and mortar/artillery observers, depending on type - vehicle versus dismounted troops - and terrain will not be revealed to the other side.&nbsp; Sounds complicated, but it's fairly simple. This is a screen shot of part of the map and sample of the tokens in the journal tab.&nbsp; I made the tokens using Corel Draw because the stuff I found and downloaded from online were not as transparent or as sharp as I liked, but mainly because I thought it would be fun to do.&nbsp; I wanted to have something look Squad Leaderish (the Avalon Hill board game), but didn't quite manage it.&nbsp; And the map is not based on anything except an area from "Imagination Land", LOL The armoured recce platoon I have selected shows number of subunits (in this case number of armoured cars or light tanks) as 3, which is also showing as a status bar below.&nbsp; The centre circle is the move rate of 18:4.&nbsp; That's 18 hexes total with a cost of 1 to enter a road and 5 to go cross country.&nbsp; The cost to go cross country is high because these are 6 wheeled armoured cars (I actually know from experience troop leading Cougars that 6 wheeled armoured cars are garbage cross country).&nbsp; Infantry at the moment are 4:2.&nbsp; There are extra move points costs to go up a hill contour (on road or not) or to enter a woods hex off road.&nbsp; I may need to adjust the movement upward as, according to Frank Chadwick (Author of Command Decision - never played, have the original rules), "you can never have too much movement".&nbsp; The third circle gives an idea of what type of equipment the platoon has, in this case, BA-10 armoured cars.&nbsp; For longer names, I'll use a shorter abbreviation.&nbsp; The players and GM will be expected to give more details about the units in their respective unit status documents.&nbsp; This is a GM view, so the Germans won't see the Soviets and visa versa.&nbsp; Here's a sample of the Soviet status: The Purpose of my Original Question with the function fArty (yes, I know what that looks like, LOL) For indirect fire mortars and artillery, that third circle will not indicate equipment, but will show range, in km they can fire in support (as long as there is a forward observation officer - FOO - for the artillery battery or a mortar fire controller - MFC - for the mortar platoon).&nbsp; That is what I am working on in m opening question for this topic - script to identify if the unit type is mortar (mor) or artillery (arty) and then make visible a circular aura with diameter equal to the range from the token's third circle. I also want to do something similar with zone of control, ZOC, a term from hex based board wargames which will highlight area around a unit through which enemy may not pass without engaging the unit.&nbsp; ZOC will be based on the type of unit (infantry, armour, etc) and the strength of the unit.&nbsp; Strength is the first circle on the left, in this case, 3. Anyway, that's where I am ATM.&nbsp; Slow going as I'm involved in the D&amp;D campaign plus real life work and home renovations. Sorry for the long blurb, -- Tim
That's awesome! Glad to see some other use cases for Roll20. Not sure if it will help in regards to the ZoC idea, but TheAaron has a script called GMAura . It does what you're describing, but only the GM can see it. I'm sure you could alter the script and get it to do what you're looking for!
Joshua W. said: That's awesome! Glad to see some other use cases for Roll20. Not sure if it will help in regards to the ZoC idea, but TheAaron has a script called GMAura . It does what you're describing, but only the GM can see it. I'm sure you could alter the script and get it to do what you're looking for! Thank you, Joshua.&nbsp; That might be useful to me as I'd need it to adjudicate things on the GM map.&nbsp; On the other hand, the players may want to use it to see where they might have gaps in their lines.&nbsp; I'd have to see if The Aaron has his stuff on Github and whether he's OK with users modifying it for ther own needs.&nbsp; But that's a little ways off;for the moment, I'm trying to figure out&nbsp; how to do something like my original script above with Aaron's corrections.&nbsp; I think I have it working ATM, but I need to work with events and what's available, especially, instead&nbsp; of the log() sending results to the console, sending results to the text chat tab or pop out text chat.&nbsp; Still trying to figure out the fine points of API calls, !, / and other issues.
1594502661
The Aaron
Roll20 Production Team
API Scripter
Yup: <a href="http://github.com/shdwjk/Roll20API" rel="nofollow">http://github.com/shdwjk/Roll20API</a> &nbsp; I have a bunch of little one-offs out on the forums. Feel free to mod them however you like.&nbsp;
The Aaron said: Yup: <a href="http://github.com/shdwjk/Roll20API" rel="nofollow">http://github.com/shdwjk/Roll20API</a> &nbsp; I have a bunch of little one-offs out on the forums. Feel free to mod them however you like.&nbsp; Very generous, thank you.