How to set a maximum amount of a variable?

Papillon

Veteran
Veteran
Joined
Feb 22, 2017
Messages
60
Reaction score
8
First Language
English
Primarily Uses
RMMV
I'm trying to design a custom resource through basic Javascript that relies on using a custom variable. I can't quite figure out how to put a limit on the amount you can generate though.
I've tried
Code:
var Skill = $gameVariables.value(1).clamp(0,100);
but that runs into an issue where if the amount generated goes over 100 (i.e. Skill is at 99 but if 2 is added it goes up to 101) it stays at that value unless it's depleted.

Is there a simple solution to this?
 

Ossra

Formerly Exhydra
Veteran
Joined
Aug 21, 2013
Messages
1,076
Reaction score
850
First Language
English
Primarily Uses
RMMV
There should be a check of the current value of variable 2 where ever you are adding or subtracting to the value. By the time a value is added, it is a bit too late for the clamp function.
 

Papillon

Veteran
Veteran
Joined
Feb 22, 2017
Messages
60
Reaction score
8
First Language
English
Primarily Uses
RMMV
There should be a check of the current value of variable 2 where ever you are adding or subtracting to the value. By the time a value is added, it is a bit too late for the clamp function.
Sorry to ask, but could you give me an example? I can't quite work it out myself.
 

Ossra

Formerly Exhydra
Veteran
Joined
Aug 21, 2013
Messages
1,076
Reaction score
850
First Language
English
Primarily Uses
RMMV
Perhaps something along the lines of the following :

Code:
// When the Variable is being set ...
// Add or Subtract from Variable ...
tmpValue = $gameVariables.value(1) - 150;
// Check and Clamp value ...
tmpValue = tmpValue.clamp(0, 100);
// Set Variable with Clamp'ed value ...
$gameVariables.setValue(1, tmpValue);

// When you are retrieving the Variable ...
var skill = $gameVariables.value(1);
// The value is already Clamp'ed and will never go over 100 or go below 0.
 

Papillon

Veteran
Veteran
Joined
Feb 22, 2017
Messages
60
Reaction score
8
First Language
English
Primarily Uses
RMMV
Perhaps something along the lines of the following :
Works absolute perfect!
Thank you so much. You've saved me a sleepless night.
 

Tea's Jams

I'm human
Global Mod
Joined
Mar 28, 2017
Messages
1,038
Reaction score
2,559
First Language
English
Primarily Uses
RMMZ
Also, Yanfly just released or is going to release soon a plugin to give variables ranges. If I recall correctly.
 

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

Latest Threads

Latest Profile Posts

rux
Just 80% of my motivation on game making lmao, anyone got tips on how to not be unmotivated?
I've signed up for a job training program for people with Asperger's Syndrome. I went to a "test run" today so they could see how well I did with simple jobs, and I amazed them. Most new people do it in 30+ minutes, while I finished in a mere 15 minutes. Gives me time to work on a screenplay for Studio MDHR...
People,I have seen that Someone from China/Chile and London is doing a Vaccine Against COVID-19.
Doing anything that yield good result in the long run is like sowing seeds. With proper care, they will grow and bear fruits. I'm excited for tomorrow. :D

Forum statistics

Threads
105,455
Messages
1,013,841
Members
137,105
Latest member
twilightprincess
Top