[JS] scripts for controlling variables via calculation

Hello everyone, I am a new RPGmaker user with basic JS knowledge for web design, I'd need to learn how to get and set game variables and how to alter them via calculation.

The specific situation here is an in-game clock mechanic (based on steps, NOT on playtime) I'm trying to get to work:
once a certain amount of Seconds is added to a variable (let's say 420 Seconds ) I need JS to get how many minutes are in the Seconds variable value, pass the correct amount to the Minutes variable and reduce the Seconds value to display accordingly: here is what I thought should have worked (should explain better):
Code:
variables:

0007: time Seconds
0008: time Minutes
0011: temporary var (minutes to add)

Code:
$gameVariables.setValue(0011) = ( $gameVariables.value(0007) / 60 ) ;
$gameVariables.setValue(0008) += $gameVariables.value(0011) ;
$gameVariables.setValue(0007) -=  ( $gameVariables.value(0011) * 60 )
Obviously none of this works, any help would be really appreciated 🙂
Just so you know: this is all doable with event commands. 🙂

For scripting, setValue expects two arguments: the variable ID and the target value. For what you've described, I'd suggest something like this:
JavaScript:
let sec = $gameVariables.value(7);
let min = $gameVariables.value(8);
let add = Math.floor(sec / 60);
$gameVariables.setValue(7, sec - 60 * add);
$gameVariables.setValue(8, min + add);


Also, avoid writing integers with leading zeroes...JavaScript will read 0011 as octal-11, i.e. 9. 😅 More info here~
So yesterday evening I've been exploring the native event system further and I found out my needs could be met relatively easy with this:
Code:
  ◆Loop
    ◆If:time S ≥ 60
      ◆Control Variables:#0008 time M += 1
      ◆Control Variables:#0007 time S -= 60
      ◆
    :End
    ◆If:time S < 60
      ◆Break Loop
      ◆
    :End
    ◆
  :Repeat Above

nonetheless, thank you @caethyril for the correct JS syntax, it will come in handy later on in development!

..and about the integers and JS, I thank you even more, that stuff will be needed in real job life I sense 🤘 (altough I'm growing to ignore IE9 compatibility and fallbacks lately when web developing, but that's for another forum ).


The Thread might now be closed.
The Thread might now be closed.
if you want your topic to be closed, you need to report it - mods will not read every topic, but they will get every report

EDIT:
and usually there is no need for a loop because parallel processes automatically loop...
and usually there is no need for a loop because parallel processes automatically loop...

I repeat: I'm Newbie McNewboy with RPGmaker events, yet this is what I believe is happening:
Before Looping the sequence, the event would shift one and only one minute from the Seconds variable to the Minutes variable for each iteration of the common event(you are seeing a little fraction of the whole event up there).
I.E. this would result in a 1:121 time format (m:s) when the event runs adding 181 seconds to a starting 0:0 value

if you want your topic to be closed, you need to report it - mods will not read every topic, but they will get every report

Thanks for this as well @Andar , much needed, very forum! *doge meme here*
Yep, looks OK to me. Note that you could avoid the inner loop with a third variable, e.g.
Code:
◆Comment:Copy current seconds into temporary variable
◆Control Variables:#0011 temp = seconds
◆Comment:Convert to whole minutes (integer division, rounds down)
◆Control Variables:#0011 temp /= 60
◆Comment:Add that to the minutes variable
◆Control Variables:#0008 minutes += temp
◆Comment:Convert whole minutes to seconds
◆Control Variables:#0011 temp *= 60
◆Comment:Subtract that the from seconds variable
◆Control Variables:#0007 seconds -= temp
Happy RPG Making! 😂

This thread is being closed, due to being solved. If for some reason you would like this thread re-opened, please report this post and leave a message why. Thank you.

Original thread: https://forums.rpgmakerweb.com/threads/js-scripts-for-controlling-variables-via-calculation.128925/