MZ Number that only takes specific values

I want to have a variable lets say "myNumber" and i want it to take values between 0 and 99.

So in various functions i can do myNumber += 3, but it should automatically check if the variable is greater than 99 and if so return 99. I just dont want to use if blocks to keep the variable in check.

Does anyone have an idea?
CT Bolt's Objects Ex lets you setup a list in the parameters to execute code whenever variable(s) or switch(s) is changed
Well i want to do this in plain javascript. I tried to use propertys, but it always resulted in Maximum call stack exceeded.
1688497879222.png
Your setter looks off. I think you want something like this.

Code:
let myNumber = {
    val: 0
}

Object.defineProperty(myNumber, "getValue", {
    get : function () {
        return this.val;
    }
});

Object.defineProperty(myNumber, "setValue", {
    set : function (value) {
        if (value >= 100)
         this.val = 99;
        else if (value < 1)
         this.val = 0;
        else
         this.val = value;
    }
});
Your setter looks off. I think you want something like this.

Code:
let myNumber = {
    val: 0
}

Object.defineProperty(myNumber, "getValue", {
    get : function () {
        return this.val;
    }
});

Object.defineProperty(myNumber, "setValue", {
    set : function (value) {
        if (value >= 100)
         this.val = 99;
        else if (value < 1)
         this.val = 0;
        else
         this.val = value;
    }
});
Hmm looks better, but it seems to be not working correctly, because my values still go over 100

Edit: Im currently doing it like this. Not sure if its the correct way of using it.

JavaScript:
myNumber.val = 90;
console.log(myNumber.val);
myNumber.val += 5;
console.log(myNumber.val);
myNumber.val += 10;
console.log(myNumber.val);
Maybe needs brackets {} for what’s in each if / else statement?
Maybe needs brackets {} for what’s in each if / else statement?
No it think i added them already.

Edit: It seems the Setter doesnt even fire sadly. At least i dont get a message to the console.
Oooh I saw your edit. You are changing the value directly, if you want to use the setter and getter, then you gotta do:

myNumber.setValue(myNumber.getValue + 100).
Oooh I saw your edit. You are changing the value directly, if you want to use the setter and getter, then you gotta do:

myNumber.setValue(myNumber.getValue + 100).
It says, myNumber.setValue is not a function.
Ooh whoops, try:

myNumber.setValue = 100

Or

myNumber.setValue = myNumber.getValue + 100
You're close: you just need to make sure that the name of the property and the name of the variable that actually stores it are different. Otherwise, when you try to do myNumber.val = 90;, JavaScript checks the setter, sees that in order to set this value it needs to execute this.val = value;, and then it checks the setter for val, realises that in order to set it it needs to execute this.val = value; and so on until the maximum call stack is exceeded. This should work:
JavaScript:
let myNumber = {trueValue: 0}
Object.defineProperty(myNumber, "val", {
    get: function () {
        return this.trueValue;
    },
    set: function(newValue) {
        if (newValue > 99) this.trueValue = 99;
        else if (newValue < 0) this.trueValue = 0;
        else this.trueValue = newValue;
    }
});
I am trying a diffrent approach, as this is not exactly what i wanted.

Method:
Code:
Number.prototype.add = (val, min, max) => {
  return Math.min(Math.max(val, min), max);
};

So whenever i add something, it should keep the number in check, but the problem is i cant get access to the variable it is called on.

Im calling the function like this, but i need the value of var1 to add them together.
Code:
let var1 = 40;
  console.log(var1);
  var1.add(5);
  console.log(var1);

I tried using "this" inside the function, but "this" is somehow the entire Window and not as i was expecting, the value of var1.
Hi there!

An easy way to do that would be using the clamp method that RPG Maker code already has. It's kind of the one you created "add". Example:

JavaScript:
let myNumber = 0
myNumber += 100
myNumber = myNumber.clamp(0, 99)
// myNumber will result in 99

// For reference:
/**
 * Returns a number whose value is limited to the given range.
 *
 * @memberof JsExtensions
 * @param {number} min - The lower boundary.
 * @param {number} max - The upper boundary.
 * @returns {number} A number in the range (min, max).
 */
Number.prototype.clamp = function(min, max) {
    return Math.min(Math.max(this, min), max);
};
I am trying a diffrent approach, as this is not exactly what i wanted.
So, before we try debugging another thing only for it to turn out not useful: what kind of thing is exactly what you want?
So, before we try debugging another thing only for it to turn out not useful: what kind of thing is exactly what you want?
I want to make a variable that can only contain numbers between 0 and 99.

I have multiple variables and places, where i add or subtract on these variables, but they should never be above or below those number.

Thing is, i dont want to call a function after assigning each time, even though it wouldnt be that bad.
Also it should be capable of assigning the value as well as add or subtract.


let num1 = 50;
num1 += 5;
num1 -= 5;
I am trying a diffrent approach, as this is not exactly what i wanted.

Method:
Code:
Number.prototype.add = (val, min, max) => {
  return Math.min(Math.max(val, min), max);
};

So whenever i add something, it should keep the number in check, but the problem is i cant get access to the variable it is called on.

Im calling the function like this, but i need the value of var1 to add them together.
Code:
let var1 = 40;
  console.log(var1);
  var1.add(5);
  console.log(var1);

I tried using "this" inside the function, but "this" is somehow the entire Window and not as i was expecting, the value of var1.
I think you're misunderstanding slightly what "this" is. To put it in the simplest terms I can, it's basically the object that's currently in charge of the code. Like if you're running a function that belongs to a Game_Battler for example, "this" will refer to the individual battler that's currently running it.

If you're running this code from a window, then "this" will indeed refer to the instance of the window's class.

However, further closures within a function recontextualise what "this" refers to. That's slightly beyond the scope of what we're discussing here though so you shouldn't have to worry about understanding that part too much right now.
I want to make a variable that can only contain numbers between 0 and 99.

I have multiple variables and places, where i add or subtract on these variables, but they should never be above or below those number.

Thing is, i dont want to call a function after assigning each time, even though it wouldnt be that bad.
Also it should be capable of assigning the value as well as add or subtract.


let num1 = 50;
num1 += 5;
num1 -= 5;
The solution I've posted should do that - just replace myNumber with whatever context you need your variable to exist in (this should do the trick most of the time), and val with the name of your variable. If that's not sufficient, then please explain why.
I tried using "this" inside the function, but "this" is somehow the entire Window and not as i was expecting, the value of var1.
The reason this isn't acting the way you expect it to is because you're using an arrow function. Arrow functions do not bind their own this, and instead inherit the one from the parent function. If you want this to refer to the specific Number object, you will need to define the function using the function keyword instead of defining it as an arrow function.

Though that function has even worse issues to worry about, since you're also not actually doing any adding in the function, and you're also trying to use min and max parameters without supplying any min and max arguments.

The correct way to write/use that function would be something like this:
JavaScript:
Number.prototype.add = function(value, min, max) {
    let result = this + value;
    if (min !== undefined) {
        result = Math.max(result, min);
    }
    if (max !== undefined) {
        result = Math.min(result, max);
    }
    return result;
};

// Usage examples:
let butt = 40;
butt = butt.add(9000, 0, 99); // 99
butt = butt.add(-9000, 0, 99); // 0
butt = butt.add(9000); // 9000

Though I'm just telling you that for academic purposes. In this particular situation, you probably should do what @Mac15001900 is advising.

Original thread: https://forums.rpgmakerweb.com/threads/number-that-only-takes-specific-values.158961/