RMMV Hey kids, don't forget that "=" and "==" are different.

NeptuneTron

The Salticid Scholar of the North
Veteran
Joined
Jan 13, 2018
Messages
78
Reaction score
47
First Language
English
Primarily Uses
RMMV
If you're just getting into JavaScript, you might start getting fancy with your "Conditional Branches" and start using scripting instead of just the default values. When you do, it's really important to remember that "=" is the assignment operator, while "==" is the equality operator. "==" asks if two things are equal, while "=" overwrites the value on the assigned on the left with whatever value is on the right.

As an example, here's what took me literally hours to figure out this evening, because I forgot this simple fact.

1623661676941.png
This is bad. It forces the length of variable 312 (which is an array, though that's not really important) to become 0. It's bad for 2 reasons.
1. This isn't a question, so there isn't a chance for it to evaluate as true, which means that anything inside the if statement is never going to be done. In this case, that means an infinite loop, which is always just fantastic.
2. It overwrites whatever was in variable 312 with 0. In this case, since it's setting the length of variable 312 to zero, it's actually setting the value of 312 to [ ], removing all contents and making a mess of my evening that took hours of debugging and me being an idiot to sort out.

Here's what it was supposed to be
1623661974251.png
Now, it's actually checking the condition properly, and not overwriting the information with whatever garbage I accidentally turned it into.

Don't be like me kids. Always remember that "=" and "==" are different.
 

NeptuneTron

The Salticid Scholar of the North
Veteran
Joined
Jan 13, 2018
Messages
78
Reaction score
47
First Language
English
Primarily Uses
RMMV
BONUS TIP:

If you're having issues debugging your code, try including some kind of heading with your debug messages, so that it's clear what part of your code you're actually in.

Repeating console.log($gameVariables.value(312)) after every single line makes it really difficult to which particular line is causing the problem. If you include a heading or some other information to easily differentiate between steps, you'll find your life much easier.
 

Tiamat-86

old jrpg gamer
Veteran
Joined
Dec 5, 2017
Messages
899
Reaction score
386
First Language
english
Primarily Uses
RMMV
this level of detail explaining javascript if what i need.
if you could work together with Trihan and what they've already made, adding this level of detail in explaining all aspects of javascript it would be totally next level is helping a scripting full on noob like me understand what im actually looking at.
 

Nolonar

Veteran
Veteran
Joined
Feb 18, 2018
Messages
262
Reaction score
367
First Language
French, German
Primarily Uses
RMMZ
1. This isn't a question, so there isn't a chance for it to evaluate as true, which means that anything inside the if statement is never going to be done. In this case, that means an infinite loop, which is always just fantastic.
Actually, it's both an assignment and a question. If you hadn't set the length to 0, but to 1 (or any other number) instead, you would've always entered the conditional.

One cool thing about JavaScript (and most C-inspired languages), is that assignments also return a value. For example, you can do something like:
Code:
x = y = 0;
instead of:
Code:
x = 0;
y = 0;

You can also do something like:
Code:
if (this._isEnabled = result) {
   // do something
}
instead of:
Code:
this._isEnabled = result;
if (this._isEnabled) {
    // do something
}

I don't recommend using the second shortcut, because it's hard to tell if that's what you intended, or if it's a typo.

Also, JavaScript has two equality operators, == and ===. I recommend only using === and avoiding == if you can (https://stackoverflow.com/q/359494/1169228).
 

ATT_Turan

Forewarner of the Black Wind
Veteran
Joined
Jul 2, 2014
Messages
1,765
Reaction score
902
First Language
English
Primarily Uses
RMMV
this level of detail explaining javascript if what i need.
if you could work together with Trihan and what they've already made, adding this level of detail in explaining all aspects of javascript it would be totally next level is helping a scripting full on noob like me understand what im actually looking at.
Have you tried any tutorials? Forget RPG Maker, just JavaScript tutorials. There are plenty that start at the beginner level.

Sites like javascript.info and w3schools.com have beginner tutorials, so they definitely cover very basic information like this. javascript.info is text-based, so you need to experiment on your own, but w3schools has code boxes on the page for you to type the examples into and see how they work.

These are just the first two things that come up when I Google "JavaScript tutorial."
 

Latest Threads

Latest Posts

Latest Profile Posts

Stream will be live with some Dark Souls 3, followed by a session of the Interactive Text Adventure! Feel free to drop by!
I THINK I have a slightly better grasp on drawing my maps now... sorta. I still need to figure out how to draw a city of sorts. I've only got the abstract going so far.
One day, I'll not have this lingering cough and we'll have another make a game stream.
A music I create while practicing E Major scale. :D

Forum statistics

Threads
115,192
Messages
1,087,992
Members
149,771
Latest member
magenz
Top