[JS] scripts for controlling variables via calculation

Status
Not open for further replies.

PaganoCristo

Warper
Member
Joined
Oct 20, 2020
Messages
4
Reaction score
1
First Language
Italian
Primarily Uses
RMMZ
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 :)
 

caethyril

^_^
Veteran
Joined
Feb 21, 2018
Messages
2,072
Reaction score
1,493
First Language
EN
Primarily Uses
RMMZ
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. :kaoswt2: More info here~
 

PaganoCristo

Warper
Member
Joined
Oct 20, 2020
Messages
4
Reaction score
1
First Language
Italian
Primarily Uses
RMMZ
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 :rock-right: (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.
 

Andar

Veteran
Veteran
Joined
Mar 5, 2013
Messages
31,334
Reaction score
7,656
First Language
German
Primarily Uses
RMMV
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...
 

PaganoCristo

Warper
Member
Joined
Oct 20, 2020
Messages
4
Reaction score
1
First Language
Italian
Primarily Uses
RMMZ
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*
 

caethyril

^_^
Veteran
Joined
Feb 21, 2018
Messages
2,072
Reaction score
1,493
First Language
EN
Primarily Uses
RMMZ
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! :kaojoy:
 

slimmmeiske2

Little Red Riding Hood
Global Mod
Joined
Sep 6, 2012
Messages
7,829
Reaction score
5,221
First Language
Dutch
Primarily Uses
RMXP

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.

 
Status
Not open for further replies.

Users Who Are Viewing This Thread (Users: 0, Guests: 1)

Latest Threads

Latest Posts

Latest Profile Posts

So which forum do I use if I want to showcase graphics? I've been using AML forum, but I'm not sure it's correct.
I am trying to get my game published with Aldorlea Games so that I make sales from their website and the game being on Steam. They have said that their published games rarely make less than a few hundred dollars.
DoubleXP wrote on Tonbi's profile.
Hello Wondering if you take commissions?
I should make an itch.io page one of these days...Maybe for my next large update.

Forum statistics

Threads
105,780
Messages
1,016,413
Members
137,475
Latest member
pedroko005
Top