_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

Aspiring Game Designer
Veteran
Joined
Aug 24, 2019
Messages
75
Reaction score
125
First Language
Portuguese
Primarily Uses
RMMV
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
2,476
Reaction score
1,868
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
480
Reaction score
1,476
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.
 

Latest Threads

Latest Posts

Latest Profile Posts

I just did some math and... apparently what seemed to be a little fun gimmick turned out to be an 80+ actors project :kaoswt2:
Yay! the original character contest 3 pack is here!
Both MV and MZ looks very good, congratulation for both RMW and Tkool participant!
I definitely can add these characters to use for my game project.

Steam link
The worst part of game development has to be marketing. Ugh...
May be starting a new job in a month or so. The thought is both exciting and terrifying.
Not like a rollercoaster though. Rollercoasters are just terrifying.
"Merchants don't sell you a product. They sell you something RIGHT NOW. That's all they're selling, the product of having what you want RIGHT NOW and not having to wait for it."

Forum statistics

Threads
112,476
Messages
1,068,550
Members
146,143
Latest member
marquez19marissa
Top