Frostorm

[]D[][]V[][]D
Veteran
Joined
Feb 22, 2016
Messages
2,370
Reaction score
1,985
First Language
English
Primarily Uses
RMMV
Ever since I learned about "let", I was wondering if it would be good practice to use it instead of "var" in the context of Notetags and Lunatic Mode. It doesn't really matter, right? Like, there's no real difference in such use cases, is there?
 

TheoAllen

Self-proclaimed jack of all trades
Veteran
Joined
Mar 16, 2012
Messages
6,619
Reaction score
8,595
First Language
Indonesian
Primarily Uses
RMVXA
It probably does matter, or maybe not. Using VAR you might end up populating a global variable, while LET is locally scoped. Depending on how the code is executed, it probably doesn't matter.

Either way, try to get used to using LET instead of VAR.
 

gstv87

Veteran
Veteran
Joined
Oct 20, 2015
Messages
2,570
Reaction score
1,615
First Language
Spanish
Primarily Uses
RMVXA
it's not the same thing.
VAR must take priority over LET, because you cant operate between undefined variables in an explicit environment.

this probably doesn't happen anymore in modern languages, but in old systems that was the case: VAR was the definition, LET was an instruction.
nowadays, it's assumed that any line involving variables with no instruction is an operation.

that actually ties to the fundamental structure of all languages: data structures, instructions, control structures, procedures and objects.
instructions are easy to spot: it's anything usually colored blue in the form of a block, they start with a key word, and end in ;, }, or "end"
control structures are larger blocks and contain several instructions (fixed loops, open loops, single conditionals, case conditionals, etc)
procedures are collections of control structures and instructions, usually returning a value, or returning to a previous line after going out of a main procedure.
objects are larger structures containing all of them, usually saved to files.

"but, what are instructions that *don't* have an instruction, such as straight operations between variables?" you might say.
.....that's exactly what LET was used for.... it used to be an instruction.
 

Frostorm

[]D[][]V[][]D
Veteran
Joined
Feb 22, 2016
Messages
2,370
Reaction score
1,985
First Language
English
Primarily Uses
RMMV
@gstv87 So are you advising against using LET? I get they aren't the same, even though there are situations where they can be interchangeable. Just wondering what's considered good practice when using them in Notetags/Lunatic Mode. I mean, the notetags themselves, and thus their contents, are already self-contained right?
 

Arthran

Veteran
Veteran
Joined
Jun 25, 2021
Messages
97
Reaction score
70
First Language
English
Primarily Uses
RMMZ
I can't say that I've ever actually looked at exactly how the plugins implement Lunatic Mode, but I would imagine that they're probably just plugging the code from your note tag into an eval() call. If that's the case, then the answer to your question depends on whether or not the plugin is running in strict mode (i.e. it has 'use strict'; either as the first statement of the plugin or as the first statement in an enclosing function or IIFE).

If the plugin is using strict mode, then it won't matter whether or not you use var or let in your note tag. But if the plugin isn't using strict mode, then the variables that you declare (using var) in your note tag will be scoped to the function that is processing that note tag. In most cases, this probably won't matter, but if that particular function happens to be using a variable with the same name, then it can cause problems. So using let is safer if the plugin isn't using strict mode, which I believe includes Yanfly's plugins (IIRC).

TLDR: Usually won't matter, but it's best to play it safe and use let.
 

Soulrender

Jack Of All Trades
Veteran
Joined
Jan 12, 2019
Messages
1,399
Reaction score
849
First Language
Poland
Primarily Uses
RMMV
The main difference is scoping rules. Variables declared by var keyword are scoped to the immediate function body (hence the function scope) while let variables are scoped to the immediate enclosing block denoted by { } (hence the block scope).

JavaScript:
function run() {
  var foo = "Foo";
  let bar = "Bar";

  console.log(foo, bar); // Foo Bar

  {
    var moo = "Mooo"
    let baz = "Bazz";
    console.log(moo, baz); // Mooo Bazz
  }

  console.log(moo); // Mooo
  console.log(baz); // ReferenceError
}

run();

The reason why let keyword was introduced to the language was function scope is confusing and was one of the main sources of bugs in JavaScript.
Example:
JavaScript:
var funcs = [];
// let's create 3 functions
for (var i = 0; i < 3; i++) {
  // and store them in funcs
  funcs[i] = function() {
    // each should log its value.
    console.log("My value: " + i);
  };
}
for (var j = 0; j < 3; j++) {
  // and now let's run each one to see
  funcs[j]();
}
My value: 3 was output to console each time funcs[j](); was invoked since anonymous functions were bound to the same variable. People had to create immediately invoked functions to capture correct values from the loops but that was also hairy. While variables declared with var keyword are hoisted (initialized with undefined before the code is run) which means they are accessible in their enclosing scope even before they are declared:
JavaScript:
function run() {
  console.log(foo); // undefined
  var foo = "Foo";
  console.log(foo); // Foo
}

run();
let variables are not initialized until their definition is evaluated. Accessing them before the initialization results in a ReferenceError. The variable is said to be in "temporal dead zone" from the start of the block until the initialization is processed.
JavaScript:
function checkHoisting() {
  console.log(foo); // ReferenceError
  let foo = "Foo";
  console.log(foo); // Foo
}

checkHoisting();

It's worth to mention, at the top level, let, unlike var, does not create a property on the global object and rememeber, In strict mode, var will let you re-declare the same variable in the same scope while let raises a SyntaxError.
 

gstv87

Veteran
Veteran
Joined
Oct 20, 2015
Messages
2,570
Reaction score
1,615
First Language
Spanish
Primarily Uses
RMVXA
In strict mode, var will let you re-declare the same variable in the same scope while let raises a SyntaxError.
this.
that's what I meant, specifically applied to JS.

that's what would have happened in old languages.
 

Latest Profile Posts

Been playing through Skies of Arcadia again, and it really makes me wanna see someone try to implement how ship battles were done in the game into RPG Maker (or really just in general).
split.png
split view of both versions of the pocket city you traverse through. there's differences like areas only being accessible in one version of Autumn Bay. or events that only happen in one or the other.
theortically if there were a developer who spied on people's game playthroughs and changed things in real time to make it harder for them, what might be said game developer's motivation?
Hi everyone! Enjoying this nice long weekend. How are you all doing? :)
I just successfully tested having events check if specific actors are in a particular party slot and then changing image to match. This will let me make scenes take into account who is in the party at the time and arrange them accordingly - this is important since I'll have extra actors beyond current party. The script used is the same from MV and is compatible with my current core plugin setup in MZ. Thank goodness.

Forum statistics

Threads
118,433
Messages
1,115,917
Members
155,391
Latest member
dndrself
Top