A friendly reminder to wrap your scripts inside a function or it will clash with other scripts
Hey everybody. I just solved an issue where my scripts worked just fine, but when running together some global variables actually override each other and crashed the script that launched earliest.
I didn't know this could happen, it can also happen with scripts that are completely unrelated to each other, which means the people who will use your scripts may encounter issues you never will simply because they have other scripts installed that lead to the same issue.
What solved it for me was making sure the entire thing is wrapped inside a function that launches itself, like this:
(function(){
// your script goes here
}();In my case my script is a whole folder of files to make it easier for me to work, so just make sure all your includes or evalFiles are inside this function, like so:
(function(){
#include "helpers.js"
// your script goes here
}();If you are making a dockable ScriptUIPanel, in that case you need to reference the document, you can do so by passing it as an argument to the wrapper function like so:
(function(DOM){
#include "helpers.js"
var windowPanel = DOM instanceof Panel ? DOM : new Window("palette", "My Script", undefined, {resizeable: true});
});(In case this is confusing to you, think of DOM as a variable which we assign the document as its value (by passing 'this' at the end of the wrapper function). Then we can use it to declare a window that can be dockable when the script is in the ScriptsUI Panels folder)
Please share some information in the comments if you experienced this before as I was banging my head against the wall trying to find a solution and a Google search brought up pretty much nothing, so your comments or information may help other people find this post in the future. Thank you!
