Howdy, Stranger!

It looks like you're new here. If you want to get involved, click one of these buttons!

Way To "Cap" Variables (Solved)

edited July 2014 in Help! with 1.x
Hello, before I post my question I'd just like to say I've been a long time lurker on the Twine Forum and want to give my thank-you's to all the brilliant people here. So this is my first post.

Edit: I'm Using The Sugarcube Header

So, I have certain numerical statistics in my story, mainly stuff like $Player.Health and $Player.Hunger and so on. I don't know if there would be a more "efficient" way of going about this, but I've been using 0-100 as most stats "min/max". That is to say, my PassageDone passage in Sugarcube has a lot of stuff like this in it -

<<if $Player.Health gt 100>>
<<set $Player.Health = 100>>
<<endif>>
<<if $Player.Health lt 0>>
<<set $Player.Health = 0; $Player.Dead = true>>
<<endif>>

And when I say a lot, I mean lines and lines...
The good thing is, it works. But I can't help but feeling there must be a much more quick and efficient way to go about this, to prevent information overload when viewing the code, and potentially save some time in the future.

I've thought about using Sugarcube's built-in Widget system in some way, but just cant wrap my mind around how to go about doing so.

Thanks for reading this, and I'll be waiting for some advice. Your input wont be ignored!

Comments

  • Range clamping can be done via either <Number>.clamp() or Math.clamp().  For example:

    <<set $Player.Health to $Player.Health.clamp(0, 100)>>
    <<if $Player.Health is 0>><<set $Player.Dead to true>><</if>>
    Although, it would likely be better to do the clamping as part of the increase/decrease operation.

    To put it all together in a widget, you could do something like this:

    <<widget modHealth>>
    <<set $Player.Health to Math.clamp($Player.Health + $args[0], 0, 100)>>
    <<if $Player.Health is 0>><<set $Player.Dead to true>><</if>>
    <</widget>>
    Usage:

    <<modHealth +5>> /% add 5 Health %/
    <<modHealth -7>> /% subtract 7 Health %/
  • Thanks! That's exactly what I needed. I don't know whether or not it would be appropriate to start another post or not, but I've read through the SugarCube references and tried out a lot of the features, but there is one thing in particular I'm having trouble understanding, and that is the initPRNG() random number generator. I'm using a lot of random(0, 10) type of stuff in my story and was wondering what the deal is with initPRNG. Thanks again.
  • It's pretty much what it says on the tin.  The normal pseudo-random number generator (PRNG) used is the one shipped with JavaScript (in the Math library, Math.random()).  While most browser implementations aren't terrible, they also don't allow any control over the seed (every time the browser reloads the page, the PRNG gets a new seed).  This has implications for games that use the PRNG for various things.  In particular, going back within the history, even if the player didn't change anything, could alter state.

    For those games that need a seedable PRNG, to ensure that state based on the PRNG doesn't start shifting on the player just for moving around the history, SugarCube includes a seedable PRNG.  Using History.initPRNG() initializes that PRNG and integrates its state into the game state, so for the life of that instance of the game the same seed will be used (and, since its state is stored as a part of the general game state, it also persists over saves).
Sign In or Register to comment.