One way to do it would be to use a custom function.
You'd need to call the function any time that the value changes.
Although you could write a function that changes the value for you and then limits things as part of that.
(bahh,
bobdickgus beat me to it).
First, the easy to read version:
Python:
init python:
def clamp1(parm_n, minn, maxn):
if parm_n < minn:
return minn
elif parm_n > maxn:
return maxn
else:
return parm_n
default numvar1 = 0
label start:
scene black with fade
$ numvar1 = 150
$ numvar1 = clamp1(numvar1, -100, 100)
"Testing 01 : [numvar1]."
$ numvar1 = -999
$ numvar1 = clamp1(numvar1, -100, 100)
"Testing 02 : [numvar1]."
$ numvar1 = 75
$ numvar1 = clamp1(numvar1, -100, 100)
"Testing 03 : [numvar1]."
return
Now, the better (but slightly less easy to follow) version:
Python:
init python:
def clamp2(parm_n, minn, maxn):
return max(min(maxn,parm_ n), minn)
default numvar1 = 0
label start:
scene black with fade
$ numvar1 = 150
$ numvar1 = clamp2(numvar1, -100, 100)
"Testing 01b : [numvar1]."
$ numvar1 = -999
$ numvar1 = clamp2(numvar1, -100, 100)
"Testing 02b : [numvar1]."
$ numvar1 = 75
$ numvar1 = clamp2(numvar1, -100, 100)
"Testing 03b : [numvar1]."
return
Exactly the same results, but making better use of python inbuilt functions.
Next, an example that doesn't require you to type
-100, 100
everywhere.
This example assumes that different variables (like love, corruption, hate, etc) will have different high/low limits. So each clamp has it's own function.
Python:
define love_maximum = 80
define love_minimum = -80
init python:
def clamp_love(parm_n):
return max(min(store.love_maximum, parm_n), store.love_minimum)
default numvar1 = 0
label start:
scene black with fade
$ numvar1 = 150
$ numvar1 += 10
$ numvar1 = clamp_love(numvar1)
"Testing 01c : [numvar1]."
$ numvar1 = -999
$ numvar1 -= 10
$ numvar1 = clamp_love(numvar1)
"Testing 02c : [numvar1]."
$ numvar1 = 75
$ numvar1 = clamp_love(numvar1)
"Testing 03c : [numvar1]."
return
Just a note to explain that I'm using
default
to create variables where the value will probably change while the game is running and
define
to create variables that won't. Variables created with
default
are included in the save files, whereas variables created with
define
aren't. Which means if I wanted to change the upper limit to 120 instead of 80, I could (without someone loading a save file which still overwrote the value with 80).
Both sets of variables are held with a standard storage space called
store
. It's just how RenPy does things, that you need to know when it comes to creating your own functions.
Honestly, the static values for the upper and lower limits could just be hardcoded into the function without using
define ...
, but I'd regard it as better practice to do it this way.
Obviously, what you code would really look like would be more like:
Python:
menu:
"Pick something"
"Nice thing":
$ love_points +=1
$ love_points = clamp_love(love_points)
"Bad thing":
$ love_points -=1
$ love_points = clamp_love(love_points)
"Testing 04 : [love_points]."
Even then, if it's one function per variable - you don't need to really pass the value to the function and can merge the value change and the limit checks together into one function...
Something like:
Python:
init python:
def update_love_points(parm_n):
store.love_points += parm_n
store.love_points = max(min(25, store.love_points), -25)
default love_points = 0
label start:
scene black with face
menu:
"Pick something"
"Nice thing":
$ update_love_points( +100 )
"Bad thing":
$ update_love_points( -100 )
"Testing 04 : [love_points]."
return
Whilst I don't like hardcoding values like -25 and +25, I figure I can at least leave the choice to you.
Just mix and match the parts of the examples you are comfortable with.
The original python solution on google was using a simple parameter
(n)
. But I was worried about the possible problems in a game that has a character definition which also uses
n
. It'd probably be fine, but to avoid all possible conflicts, I changed the parameter value to
(parm_n)
instead.