# Checking if a variable is even or odd

#### ShiningPhoibe

##### Veteran
Hello,
I have a time system in my game. It includes seasons, which are stored in a variable. I'd like to make an event happen on even seasons (2, 4, 6...). Is it possible to set such a condition? If it is, how could I do it?

#### Trihan

##### Speedy Scripter
Modulus is your friend here. An even number mod 2 will equal 0, and an odd number mod 2 will equal 1. So just check whether your number mod 2 is 0; if so it's even, if not it's odd. % is the modulus operator in Javascript.

#### ShiningPhoibe

##### Veteran
Modulus is your friend here. An even number mod 2 will equal 0, and an odd number mod 2 will equal 1. So just check whether your number mod 2 is 0; if so it's even, if not it's odd. % is the modulus operator in Javascript.
That was quick! Thank you.
Edit: I don't know JavaScript. I tried to use Script and wrote \v[212] % 2; and then \$gameVariables.setValue(129, \v[212] % 2); but it didn't work. I know I must have done it wrong but I don't know how to get it right.

Last edited:

#### caethyril

##### ^_^
This will return 1 if game variable 212 is odd, 0 if it is even:
JavaScript:
``\$gameVariables.value(212) % 2``

However, you don't need scripting here: Control Variables has a Mod operation. E.g.
Code:
``````◆Comment：Copy value to temporary "check" variable
◆Control Variables：#0001 check = Season
◆Comment：Apply "Mod 2"
◆Control Variables：#0001 check %= 2
◆If：check = 0
◆Text：None, Window, Bottom
：    ：Even!
◆
：Else
◆Text：None, Window, Bottom
：    ：Odd!
◆
：End``````

#### Winshifter

##### Veteran
That was quick! Thank you.
Edit: I don't know JavaScript. I tried to use Script and wrote \v[212] % 2; and then \$gameVariables.setValue(129, \v[212] % 2); but it didn't work. I know I must have done it wrong but I don't know how to get it right.

\$gameVariables.value(1) % 2 === 0 where the number in parenthesis is the id of the variable.

#### ShiningPhoibe

##### Veteran
This will return 1 if game variable 212 is odd, 0 if it is even:
JavaScript:
``\$gameVariables.value(212) % 2``

However, you don't need scripting here: Control Variables has a Mod operation. E.g.
Code:
``````◆Comment：Copy value to temporary "check" variable[/INDENT]
[INDENT]◆Control Variables：#0001 check = Season[/INDENT]
[INDENT]◆Comment：Apply "Mod 2"[/INDENT]
[INDENT]◆Control Variables：#0001 check %= 2[/INDENT]
[INDENT]◆If：check = 0[/INDENT]
[INDENT]  ◆Text：None, Window, Bottom[/INDENT]
[INDENT]  ：    ：Even![/INDENT]
[INDENT]  ◆[/INDENT]
[INDENT]：Else[/INDENT]
[INDENT]  ◆Text：None, Window, Bottom[/INDENT]
[INDENT]  ：    ：Odd![/INDENT]
[INDENT]  ◆[/INDENT]
[INDENT]：End``````
Thanks!

### Latest Profile Posts

My ex put my heart upon a self he said don't give me no lines and keep my hands to myself :,(
An Old... old Harold | RPG Maker News #71

Did a test on myself and I was negative on COVID. I was sitting near my aunt the whole time while doing stuff.
Colored!
RPG Maker Games Critique with Studio Blue starts now! Join us and follow along live as we explore A Thief's Voyage by walldeaf.