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

Constants Available to the whole script?

1367804543
Konrad J.
Pro
API Scripter
Can this be done?  Right now I'm just creating some variable and putting them in each function that needs them so I can change values relatively easily.  Would be nicer to have one central area to put them though. THanks!
1367832081
Alex L.
Pro
Sheet Author
You just define them at the top of the script outside any function.
Alex is correct. An example might be: //At the top of your script. var MAX_DISTANCE = 50; // <snip>, later on down the script on("change:graphic", function(obj) {     //You can do something with MAX_DISTANCE here... }); In addition, remember that *all* your scripts run in the same context (scope), so you if you define MAX_DISTANCE at the top of one script, you can use it any script. Likewise if you overwrite MAX_DISTANCE with a different value in one script, it will affect all of them.
1367847706
Alex L.
Pro
Sheet Author
Riley D. said: Alex is correct. An example might be: //At the top of your script. var MAX_DISTANCE = 50; // <snip>, later on down the script on("change:graphic", function(obj) {     //You can do something with MAX_DISTANCE here... }); In addition, remember that *all* your scripts run in the same context (scope), so you if you define MAX_DISTANCE at the top of one script, you can use it any script. Likewise if you overwrite MAX_DISTANCE with a different value in one script, it will affect all of them. As long as the script you define it in is before the one you use it in.
1367858593
Konrad J.
Pro
API Scripter
Riley D. said: Alex is correct. An example might be: //At the top of your script. var MAX_DISTANCE = 50; // <snip>, later on down the script on("change:graphic", function(obj) {     //You can do something with MAX_DISTANCE here... }); In addition, remember that *all* your scripts run in the same context (scope), so you if you define MAX_DISTANCE at the top of one script, you can use it any script. Likewise if you overwrite MAX_DISTANCE with a different value in one script, it will affect all of them. Doh!  I was putting them inside the main calling event thinking all the other functions, etc. would see the variables.  Glad its so simple! :)
1367858680
Konrad J.
Pro
API Scripter
Alex L. said: Riley D. said: Alex is correct. An example might be: //At the top of your script. var MAX_DISTANCE = 50; // <snip>, later on down the script on("change:graphic", function(obj) {     //You can do something with MAX_DISTANCE here... }); In addition, remember that *all* your scripts run in the same context (scope), so you if you define MAX_DISTANCE at the top of one script, you can use it any script. Likewise if you overwrite MAX_DISTANCE with a different value in one script, it will affect all of them. As long as the script you define it in is before the one you use it in. So if your very first script has simply a bunch of variables then all your scripts will be able to use them?  Kinda like an include file I guess.
Yeah, once I have a better handle on all this, I'm gonna make my first script be the include file and put all kinds of variables and functions in there.
1367859982
Alex L.
Pro
Sheet Author
Coming from an OOP background I have 6 include scripts each with a "class" (javascript doesn't have classes but has things like them) in it and I have one main script that has all the events in it and makes calls to the classes.
1367861173
Alex L.
Pro
Sheet Author
Don't like to double post but just to be clear there are no constants in javascript I'm not sure if you wanted true constants or not but I thought I would point that out. A constant is a variable that is set once and can't be changed if you want to signify to other programers that something is a constant in javascript its best to make the name all caps ie: var MYCONSTANT = 42; This way they should know its expected to never change.
1367861323
Konrad J.
Pro
API Scripter
Alex L. said: Don't like to double post but just to be clear there are no constants in javascript I'm not sure if you wanted true constants or not but I thought I would point that out. A constant is a variable that is set once and can't be changed if you want to signify to other programers that something is a constant in javascript its best to make the name all caps ie: var MYCONSTANT = 42; This way they should know its expected to never change. I found that out in my research googling.  :)  Only variables and make any that are pseudo constants all Caps!  Gotcha. Thanks
1367867168
Konrad J.
Pro
API Scripter
Riley D. said: Alex is correct. An example might be: //At the top of your script. var MAX_DISTANCE = 50; // <snip>, later on down the script on("change:graphic", function(obj) {     //You can do something with MAX_DISTANCE here... }); In addition, remember that *all* your scripts run in the same context (scope), so you if you define MAX_DISTANCE at the top of one script, you can use it any script. Likewise if you overwrite MAX_DISTANCE with a different value in one script, it will affect all of them. I guess this could be a potential problem when users start really sharing scripts.  One script could use the same "Constant" name as another.  Also when taking commands from the chat window two scripts might use the same commands and the user doesn't know.
Typically in Javascript this is solved by namespacing. I would recommend a similar approach here. So at the top of your scripts put: var konrad = konrad || {}; And then you can define things for example: konrad.MAX_DISTANCE = 50; The idea is that you pick only a single variable (in this case "konrad") that "pollutes" the global namespace, and that way as long as no one else uses "kondrad" you're fine. You don't have to worry about any of the "sub-variables" under konrad. I imagine very complex scripts will even take advantage of an "export"/closure-based protection as well, e.g.: var konrad = konrad || {}; (function() { //Do stuff in here, only modifying/access konrad if you need to work outside this particular script. })();
1367872856
Konrad J.
Pro
API Scripter
ahha!  I see, very nice.  I will do that for sure!  I love it.  Learning lots.  I was thinking for commands from the chat window I would put a few letters in from of the command to denote the script, but don't want the command to get too long. :) !cwsdr killercart Car Wars Steep Drift Right killerkart So as long as no one starts their commands with cw I should be alright. Thanks again Riley  You have been very helpful with my beginner questions! PS: I think I'll change it to Vehicle Combat.  We don't want a letter from Steve Jackson's laywers.  He is very strict with his copyrights, although somehow Vassal gets away with it for years.  Another VTT tried to put out an artpack to help use car wars with his VTT and the lawyers said no way.  I couldn't understand that one in the least actually. Sorry for the digression to another topic! :)