Variable Add limit?

Is there a way to limit the "add" number in variable.
Screenshot 2024-09-04 212937.png

This variable is counting the days passed in the game. Can I make it to add to specific number and then starts from 1 again? (I have 28 days in the season)
Yeah, it's pretty easy.

After you add 1 to your variable, just run a conditional check to see if it's 28 or not. If it is, reset it back to 1.
You just use Conditional Branch to see if it's greater than 28, make it be 1.

There are many threads about time systems you can search for if you need help with the logic.
you can use 1 variable or 2, another way is a plugin that clamps the number
like DK_VariableRange which clamps them, so you cannot bypass them.

which is maybe less control if you want to increase it overtime.

so there are simple ways like the 2 above which is the easiest.
I think the conditional method is the most straight forward,
But you can use the modulo (mod) operator.
Modulo uses the remainder of a division as it's output.

Edit
Oh god, I realized something I overlooked when considering the modulo operation that makes this post pointless! If I hadn't prematurely posted this on accident I would have just canceled posting, alas I fat-fingered the screen and accidentally posted it before I intended to.
Well, guess I am comitting to this now.

You can use the mod operator to also do this. I still think the conditional way is probably the best and most straight forward but maybe you can apply this idea later.

So every in game hour, increment your day/night variable by 1.
Then do a conditional.
If day/night >= 27
Control variable: day/night %27 (the % represents modulo)



You could also do this with 2 variables: an hour variable that's constantly ticking up forever and ever and a time of day variable that you will mod using the value of the hour variable.

Anyways, because of my mistake this is all pretty pointless, so uh...I don't know. I hope you learned something today.
Mods are cool. The nuddy mods are my favorite.
But you can use the modulo (mod) operator.
didn't know this one, will play with it as it come in handy of a 12-24h clock 🙂
Then do a conditional.
If day/night >= 27
Control variable: day/night %27 (the % represents modulo)
you don't even need the conditional branch if you count from zero

just make the modulo directly, it will always have the correct result no matter if the number is higher or lower than the limit.
however you need to keep in mind that when the number is exactly on the limit, the result will be zero.
which would only be correct if you start your counter with zero as well and use a +1 in the display.
But you can use the modulo (mod) operator.
Agreed - mod is awesome for this.

Something like this could work without a conditional branch:
JavaScript:
◆Control Variables:#0004 += 1
◆Control Variables:#0004 = Math.max(1, $gameVariables.value(4) % 29)

Original thread: https://forums.rpgmakerweb.com/threads/variable-add-limit.171676/