Checking if a variable is even or odd

ShiningPhoibe

Veteran
Veteran
Joined
May 12, 2020
Messages
89
Reaction score
33
First Language
French
Primarily Uses
RMMZ
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?
Thanks for reading.
 

Trihan

Speedy Scripter
Veteran
Joined
Apr 12, 2012
Messages
3,474
Reaction score
2,600
First Language
English
Primarily Uses
RMMZ
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
Veteran
Joined
May 12, 2020
Messages
89
Reaction score
33
First Language
French
Primarily Uses
RMMZ
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

^_^
Veteran
Joined
Feb 21, 2018
Messages
2,476
Reaction score
1,868
First Language
EN
Primarily Uses
RMMZ
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
Veteran
Joined
Feb 24, 2017
Messages
73
Reaction score
53
First Language
Spanish
Primarily Uses
RMMV
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
Veteran
Joined
May 12, 2020
Messages
89
Reaction score
33
First Language
French
Primarily Uses
RMMZ
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 Threads

Latest Posts

Latest Profile Posts

For a long time I always consider XP soundtracks as the best one.
Just listened to some of MZ tracks from the free version.
It's... Quite mindblowing. xD
Only listened to the battles, towns, and fields.
But I'm sure the rest are equally good. :D
What do you guys want to see for the new Jump into Javascript? A continuation of the rpg_objects, or would you prefer a "lesson 0" where I go back to the absolute basics of Javascript and cover the fundamentals of the language and its construction?
Hey RPG members!!

How do you finish projects? Some new game developers start off strong but end up losing momentum. What keeps you motivated while working on new projects?
Feeling wee sleepy.. ZzzZ
So many threads to move to the right place. My post count is awesome, but 95% of it is due to moving things.

Forum statistics

Threads
112,452
Messages
1,068,379
Members
146,106
Latest member
sleymour
Top