Script call check if a variable is even or odd

_Soysauce_

Villager
Member
Joined
Sep 12, 2018
Messages
19
Reaction score
5
First Language
English
Primarily Uses
RMMV
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!
 

TheNewSon

Veteran
Veteran
Joined
Aug 24, 2019
Messages
69
Reaction score
114
First Language
N/A
Primarily Uses
N/A
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
 
Last edited:

_Soysauce_

Villager
Member
Joined
Sep 12, 2018
Messages
19
Reaction score
5
First Language
English
Primarily Uses
RMMV
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.
 

caethyril

^_^
Veteran
Joined
Feb 21, 2018
Messages
1,654
Reaction score
1,106
First Language
EN
Primarily Uses
RMMV
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).
 

Kuro DCupu

Trust me, I'm a veteran RMer
Veteran
Joined
Jul 6, 2014
Messages
434
Reaction score
1,382
First Language
Bahasa
Primarily Uses
RMMV
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.
 

Users Who Are Viewing This Thread (Users: 0, Guests: 1)

Latest Threads

Latest Posts

Latest Profile Posts

It's raining non stop since yesterday. The sound of the rain is so soothing.
I really miss the old style disney animated movies. I guess the last one was The Princess and the Frog from 2009. Now I am trying to collect every single one I can get on BD or DVD.
What I've seen from the MZ tiles looks pretty inconsistent, bleh. Every plant seems to be drawn in a different style.
Recently posted a demo of my game online. Now I have no idea what to do with myself o_O What is the reason for my existence now...
I'm feeling a lot better this week. Some of the rules in my country for isolation changed a little, so I was able to see friends in person again, which was a huge relief. Thanks everyone for giving me some sympathy last week when I was having a rough time!

Forum statistics

Threads
99,287
Messages
963,880
Members
130,899
Latest member
Densuki
Top