I have a question. Right now, within my javascript sheet I am using Setup.XXX = function() {}; for basically everything. Even for functions that only exist to be called upon by other Setup functions I use the Setup feature.
My guess is that this is insanely stupid. If it is, how do I fix this and why is it insanely stupid?
For context: I have no formal education in coding so I just do whatever works and am happy afterwards. Since I noticed that using Setup. is the magic fix to most of my problems I use it as if I was a magician.
I will assume you actually mean setup, with a lower case s, as letter case is important.
The content of a Twine 2.x application project’s Story JavaScript area is executed in a Private scoped context, which is why you need to use a method like the setup special variable to make any variable/function defined within that context available outside of that context.
But functions defined purely within a Private scoped context can be used by other functions within the same Private scoped context.
eg. in the following example the log() function can be used by the setup.doit() function because it was defined in the same context…
const log = function (msg) {
console.log('logging: msg: ' + msg);
};
setup.doit = function () {
log('doit called');
};
The following Passage content demonstrates the availability of each of the above functions.
Welcome to the story
<<run setup.doit()>> /* this will work */
<<run log()>> /* this will throw a 'not defined' error */
So you only need to add functions that need to be available throughout the rest of the project to the setup special variable.
Alright thank you for the explanation! I do use setup everywhere with a capital S, and it does still work - as long as I use it everywhere with a capital S.
So setup is key if I want to use a function from the javascript section in a passage.
But just to make sure: Should i go through all my functions where setup is not needed and “de-setup” them, or does overusing setup have no impact on the games performance?
I have always put all my sugarcube functions on the setup object with no problems. This is generally preferable to putting them on a global scope like window which may conflict with other programs. Though there may be very rare cases you need to put them on window.