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,837
Reaction score
1,297
First Language
EN
Primarily Uses
RMMZ
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
460
Reaction score
1,435
First Language
Indonesia
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 Profile Posts

I just like this website:
lol
I streamed Moonfall on 9/25/20
and have started a page to showcase my vids/stream :kaoluv:

Please subscribe and follow, it's free, it's quick and one sub means so much to me, it would really make my day~ :wub

And now to face my worst enemy... adding color :kaocry:
Power has been restored FINALLY to our home in Lake Charles. Hurricane Laura tried to wreck our lives and disrupt my work on the commercial project i have going with Cloud 9 but the key word is TRIED!!! Slowly moving from the shelter to home over the next week or so. I WILL NOT QUIT!
I am happy because the RPG Maker MZ character comes with extra bust and full body art, but I am disappointed because the quality of the character's PIxel art has decreased.
Only My Opinion that Pixel art MV is better.

Forum statistics

Threads
103,228
Messages
998,180
Members
134,716
Latest member
stevendkelven
Top