MV Variable Max

I need help getting a "variable max" setup, b/c JS is not my forte (but I do know scripting / coding, to an extent). I found some code snippets, but can't figure out how to make them work with my system. Let me explain...

I have a Fatigue System setup where I'm using a custom graphic on the HUD to track how fatigued a person is. That works great. The problem is that when I use the code snippets below to try & create a max for my "fatigue" cap, it never works.

My fpCurrent is var 19, & my fpMax is var 20. The custom graphic uses the fpMax to determine where the bar should stop, while fpCurrent is the bar that moves; this is the var I'm trying to create a max for. The graphic works as expected, but it is the variable ITSELF that will not stop at the max I set; which I've confirmed by linking [v19] to a dialogue box to see what value it has.

The code snippets I found are:
Code:
// Add or Subtract from Variable ...
tmpValue = $gameVariables.value(1) - 50;

// 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 fpCurrent = $gameVariables.value(1);

I've got everything setup as follows:
1) A global event that starts with the game, which starts tracking the fpCurrent (or var 19), incl. the Check & Clamp value, via switch;
2) An event trigger (for testing) that increases [v19] by +60 (additive; to check if the max cap works; it hasn't yet);
3) An event trigger (for testing) that decreases [v19] by -30 (subtractive)

So far... the HUD bar works just fine, but I can't see to find the right combination of placement for the code snippets above to make [var 19] stop at 100.

Any help would be appreciated. TIA.

**Edit / Addendum**
I HAVE been trying to adjust the variable by default methods (in the RMMV engine) vs using the script snippets above; so maybe that's the prob? So, I'll be playing with that option while members review this post.
I'm kind of confused about those snippets and where they are and what you're doing with them.

Is your max a variable, or is it 100?

If you want to add a value (X) to variable and cap it at 100, you would simply do:
Code:
$gameVariables.setValue(19, Math.min($gameVariables.value(19)+X, 100));

Or you could do it in a few lines of event commands:
Control Variables -> 19 -> +X
Conditional Branch -> variable 19 > 100
Control Variables -> 19 -> 100
if it's just 1 bar, you can also look into Galv_VariableBar which you can
use your own images for it and as many bars as you like.

a simple var and max var and it can do the rest 🙂

it has many purposes, so maybe it can have something you miss
as well 🙂
if it's just 1 bar, you can also look into Galv_VariableBar which you can
use your own images for it and as many bars as you like.
Actually... that IS what I use, lol... & yeah, it's a GREAT little plugin. But getting it to work with a max variable has been the problem.

Or you could do it in a few lines of event commands:
Control Variables -> 19 -> +X
Conditional Branch -> variable 19 > 100
Control Variables -> 19 -> 100
That was the first thing I tried. The problem was that if [v19] had a value of 80, & I added another 30, it would go over the "100 max" in the condition I'd set. Not sure how or why. I can type out how I had the cond. branch if you want to see.

It is because the cond. branch didn't work why I started looking for a way to code a max variable, instead of using a conditional branch. I will try your code suggestion while you review this reply. As to the reference of where I got the code snippet, I found it HERE.

Let me try & simplify my need (sry... I tried to do that in the original post):
  • I use THIS plugin to create my custom graphic on my HUD, to show the player how much fatigue they're party currently has;
  • I have the "current fatigue" (or the variable that gets adjusted up or down) set to variable #19 (aka v19);
  • I have the "max fatigue" (which only serves as a variable for the HUD graphic) set to variable #20 (aka v20);
  • Since v20 is needed only for the plugin's function (e.g. for Galv's plugin), all I need to get the game to function how I want is to setup v19 to have a range of 0-100 ~ where it can NOT ever drop below 0 or go above 100;
  • The end goal is a HUD graphic that shows how much fatigue the party currently has, & a custom variable setup to serve as the "current fatigue", which cannot be less than 0, & not more than 100.
I think the problem is that you need to apply the clamp after each time
the variable is modified.

Using the clamp function is a one-time thing and does not set "hard limits" for the variables that persist from then on and last forever.
Actually... that IS what I use, lol... & yeah, it's a GREAT little plugin. But getting it to work with a max variable has been the problem.


That was the first thing I tried. The problem was that if [v19] had a value of 80, & I added another 30, it would go over the "100 max" in the condition I'd set. Not sure how or why. I can type out how I had the cond. branch if you want to see.

It is because the cond. branch didn't work why I started looking for a way to code a max variable, instead of using a conditional branch. I will try your code suggestion while you review this reply. As to the reference of where I got the code snippet, I found it HERE.

Let me try & simplify my need (sry... I tried to do that in the original post):
  • I use THIS plugin to create my custom graphic on my HUD, to show the player how much fatigue they're party currently has;
  • I have the "current fatigue" (or the variable that gets adjusted up or down) set to variable #19 (aka v19);
  • I have the "max fatigue" (which only serves as a variable for the HUD graphic) set to variable #20 (aka v20);
  • Since v20 is needed only for the plugin's function (e.g. for Galv's plugin), all I need to get the game to function how I want is to setup v19 to have a range of 0-100 ~ where it can NOT ever drop below 0 or go above 100;
  • The end goal is a HUD graphic that shows how much fatigue the party currently has, & a custom variable setup to serve as the "current fatigue", which cannot be less than 0, & not more than 100.
If you're able to use code in the HUD settings, can you set your max to

$gameVariables.value(20).clamp(0, 100)

?
Thanks for the suggestions. I'll try again tomorrow & get back with you.
if you use Galv_VariableBar, how exactly did you use it?
when I use it, it does what it does though.

because you need to set the max variable before the plugin Command,
I use it for a timer and 3 other things and it stops at max value and wont
go over it as well.
I haven't yet tried anyone's suggestions (going to soon), but I had a thought this morning. I don't know Javascript very well, but I can understand what it is doing when I read through a script... I just can't recreate it very well. That said, is there a short, simple little plugin that someone could type up that would allow a person (who knows a little JS) to create their own array of variables & ranges to suit their needs? Put another way... is there anyone who could create a plugin "template" that I could use & modify to suit the variable numbers &/or ranges that I need?

If not, I'll play with the suggestions until I can find one that suits my needs; then post the results to help others who stumble onto this thread, of course.



**Edit**
To answer your question, ShadowDragon, Galv's plugin isn't the issue. I have that setup the way I want. And yes, I understand that the var max is the priority here. That's why I'm picking the expert's brains (e.g. "you guys")... trying to figure it out :LZSwink:.

Also... I didn't realize I'd need a variable "cap" or limit on my custom resource until I had already setup Galv's plugin & setup other parts of my game mechanics. As a general rule, I try not to bother the RPG Maker community (e.g. you guys) unless I tinker with something for a few days, trying to figure it out on my own. I'm comfortable enough with other forms of scripting (mainly C#) & game engines to usually find a way to make something work myself, but I do have my weaknesses... & JS is one of them (ironic, huh?).
As usual, I overcomplicated the matter. I WOULD still be interested in trying to get a little "plugin" for setting various variable limits (for the convenience of using the built-in "control variable" option), but @ATT_Turan had the answer.

Further below are screenshots of how I have the test-scene setup, & getting a custom resource (such as "Fatigue"; e.g. what I'm using) to have a variable "cap" & HUD graphic. To use this idea in your own game (for any who come across this post & want to see the resolution), just follow the instruction on how to use Galv's Variable Bar (incl. in his plugin), then use the solution posted by @ATT_Turan above in a script call of the trigger of your choice.

To make sure the game doesn't exceed the upper limit you want on your resource (in my case it's 100), you'd use the script call provided by @ATT_Turan, & make sure to change the "19" to whatever variable you have assigned to your custom resource:
Code:
$gameVariables.setValue(19, Math.min($gameVariables.value(19)+X, 100));

And... Unless I'm mistaken, (correct me if I'm wrong, "RPG community"), to make sure you don't exceed the LOWER limit you want your resource to have when making negative value changes (in my case, it's 0), you'd use the following script call:
Code:
$gameVariables.setValue(19, Math.max($gameVariables.value(19)-X, 0));

@AAT_Turan's suggestion in use:
Fatigue Add.png


Checking to see if the 60 fatigue I added through the script worked as expected:
Fatigue 60.png


Checking to see if the "100" var cap / limit worked as expect (after adding another 60):
Fatigue 100.png


Indeed it all worked as expected. Thanks @ATT_Turan.

Also... thanks for your suggestions, @ShadowDragon & @Trihan.

Unless someone wants to type up a "variable max" plugin template for me to play with (which I may ask for in a new thread, if you guys would pref.), this matter is resolved & the admins may lock it.
And... Unless I'm mistaken, (correct me if I'm wrong, "RPG community"), to make sure you don't exceed the LOWER limit you want your resource to have when making negative value changes (in my case, it's 0), you'd use the following script call:
Code:
$gameVariables.setValue(19, Math.max($gameVariables.value(19)-X, 0));
Correct.
Unless someone wants to type up a "variable max" plugin template for me to play with (which I may ask for in a new thread, if you guys would pref.), this matter is resolved & the admins may lock it.
I'm not sure what this would entail or what its purpose would be. As you've seen, this is one simple line of code when you're adding or subtracting, I don't get what a person would need an entire plugin for.
@ATT_Turan
I'm not sure what this would entail or what its purpose would be. As you've seen, this is one simple line of code when you're adding or subtracting, I don't get what a person would need an entire plugin for.
True... but my thinking on it would be so that a person could use the RPG engine's default "variable control" for variable adjustments. Indeed using the script call is very easy, but a plugin for this is just a muse I had.
my thinking on it would be so that a person could use the RPG engine's default "variable control" for variable adjustments.
They can, I gave you the event code in my first post 😉

I get what you're saying, and it's doable, but I don't think it makes a lot of sense to design a plugin where you can make a list saying "This variable has that cap and this variable has that cap and..." and make it interface with the code for the Control Variables command.

It would take...as little, if not potentially less time for the user to just put in those event commands.
Thanks for elaborating on that. This way, should anyone come across this thread in the future & be thinking the same thing I did, they can both get their answer to the type of script they need AND an answer to the plugin muse, lol.

Unless anyone wants to comment further on this, the admins can lock this thread. Thanks again to everyone who gave their input on this topic.
As usual, I overcomplicated the matter. I WOULD still be interested in trying to get a little "plugin" for setting various variable limits (for the convenience of using the built-in "control variable" option), but @ATT_Turan had the answer.

Further below are screenshots of how I have the test-scene setup, & getting a custom resource (such as "Fatigue"; e.g. what I'm using) to have a variable "cap" & HUD graphic. To use this idea in your own game (for any who come across this post & want to see the resolution), just follow the instruction on how to use Galv's Variable Bar (incl. in his plugin), then use the solution posted by @ATT_Turan above in a script call of the trigger of your choice.

To make sure the game doesn't exceed the upper limit you want on your resource (in my case it's 100), you'd use the script call provided by @ATT_Turan, & make sure to change the "19" to whatever variable you have assigned to your custom resource:
Code:
$gameVariables.setValue(19, Math.min($gameVariables.value(19)+X, 100));

And... Unless I'm mistaken, (correct me if I'm wrong, "RPG community"), to make sure you don't exceed the LOWER limit you want your resource to have when making negative value changes (in my case, it's 0), you'd use the following script call:
Code:
$gameVariables.setValue(19, Math.max($gameVariables.value(19)-X, 0));

@AAT_Turan's suggestion in use:
View attachment 223273

Checking to see if the 60 fatigue I added through the script worked as expected:
View attachment 223274

Checking to see if the "100" var cap / limit worked as expect (after adding another 60):
View attachment 223275

Indeed it all worked as expected. Thanks @ATT_Turan.

Also... thanks for your suggestions, @ShadowDragon & @Trihan.

Unless someone wants to type up a "variable max" plugin template for me to play with (which I may ask for in a new thread, if you guys would pref.), this matter is resolved & the admins may lock it.
Just an aside for clarification, .clamp does the same thing as your two .max and .min calls, it's just a wrapper to make the process shorter:

JavaScript:
Number.prototype.clamp = function(min, max) {
    return Math.min(Math.max(this, min), max);
};

Original thread: https://forums.rpgmakerweb.com/threads/variable-max.146760/