Script call check if a variable is even or odd

Hi everyone,

I've been wanting to make a simple conditional branch where I would check if a variable is even or odd.

At first I was thinking it wouldn't have been a big deal, as I could simply check if the variable can be divided by 2 and check the result.
Then I realized there's no basic option in the conditional branch for that (unless I missed something major), and I would have needed a script call.

I know nothing about scripting, but I was assuming someone might have had the same problem - and yet I found nothing, except for this thread, which I couldn't make it work in the slightest - partially because knowing next to zero scripting, so I have no idea if it's related or not and if it is how to rewrite the code suggested to have a variable check.

In short, I just need to know what script call code I can use in a conditional branch to quickly check "Is the number even? Do this. / It's not even? Then it's odd and do this."

thanks in advance!
The following line will return TRUE if the number is even, and FALSE if it's odd (replace N with the variable ID).

Code:
($gameVariables.value(N) % 2) == 0

Example:

Example.png
Thanks a bunch man, it works perfectly and it's short and simple!

I hope this thread will pop up in the future to people who are looking for this.
For future reference, % is the script version of the Mod (modulo) operator available in the Control Variables command. N mod 2 returns the remainder after dividing N by 2, i.e. it is 1 if N is odd, or 0 if N is even. So this is possible without a script. 🙂

A familiar example of modular arithmetic is telling the time, e.g. 3 hours after 11 am is 2 pm (11 + 3 mod 12).
For further information, that's a modulo operator (% = remainder division). It's equal to this :
1580565564864.png


Modulo are often underrated because mostly doesn't know its practical use. If I have to explain, it's used to create arithmetic cycle like : 0, 1, 2, 3, 0, 1, 2, 3, 0, 1, 2, 3, ...
Or in your case : 0, 1, 0, 1, 0, 1, 0, 1, ...
In practical it's used to make time system or the likes.

Original thread: https://forums.rpgmakerweb.com/threads/script-call-check-if-a-variable-is-even-or-odd.117821/