0 votes
by (370 points)
Hi i was using the latest sugarcube to right a game.I know how to use storycaption and how to create stats for my character:

<<set $time to new Date(2018, 1, 1, 7, 0, 0)>>
<<set $Health to 100>>

But during the work i suddenly wonder "is there some kind of limit for the stats?"For example Health limit is 100 and when it reaches 100 it can't increase anymore.

Can someone give me the answer?

1 Answer

0 votes
by (8.9k points)

You can use the Math.clamp method to fix a number within a certain range.

The following line of code checks whether $Health is less than 0 or higher than 100.  If it's less than 0, it sets $Health to 0.  If it's higher than 100, it sets $Health to 100.

<<set $Health = Math.clamp($Health, 0, 100)>>

 

by (370 points)
A P P R E C I A T E ! ! !
by (370 points)
edited by
I tried putting the limit from 0 to any big random number,for example 9999999999999999.It works fine as hell
by (8.9k points)

I don't know how to do that with Math.clamp.  This code is a bit more primitive, but would work.  It checks whether $Money is less than 0, and if so sets it to 0.

<<if $money lt 0>>
  <<set $money to 0>>
<</if>>

Someone else might be able to give you some more elegant code though, I'm still a coding noob.  :-)

...