Howdy, Stranger!

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

[SOLVED]Max/minimum number?

edited August 2014 in Help! with 1.x
I'm trying to do a sort of Health system and I want it so it can't go above 4 how would I go about doing this? Thanks.

Comments

  • What story format are you using?

    Clamping health (e.g. $health) to a specific range (minmax) will have to be done whenever it's modified.

    If you're using one of the vanilla story formats, you'll have to do that by hand.  For example, and assuming that the allowable range is 04 (0 being "bad", 4 being perfectly healthy):

    /% Add 1 to $health. %/
    <<set $health += 1>>

    /% Clamp the value of $health to the desired range (04), if necessary. %/
    <<if $health lt 0>><<set $health to 0>><<elseif $health gt 4>><<set $health to 4>><<endif>>
    If you're using SugarCube, you can use Math.clamp().  For example, again assuming that the allowable range is 04:

    /% Subtract 1 from $health and clamp to the desired range (04). %/
    <<set $health = Math.clamp($health - 1, 0, 4)>>
    For the sake of convenience, and so as to not repeat yourself, you're probably better off wrapping up whatever you end up doing as a pseudo-macro (vanilla story formats) or widget (SugarCube).

    Alternatively, you could use a fairly simple function instead.  I don't know if you're one of the people who are allergic to JavaScript.


    [EDIT] Updated an incorrect value.
  • TheMadExile wrote:

    What story format are you using?

    Clamping health (e.g. $health) to a specific range (minmax) will have to be done whenever it's modified.

    If you're using one of the vanilla story formats, you'll have to do that by hand.  For example, and assuming that the allowable range is 04 (0 being "bad", 4 being perfectly healthy):

    /% Add 1 to $health. %/
    <<set $health += 1>>

    /% Clamp the value of $health to the desired range (04), if necessary. %/
    <<if $health lt 0>><<set $health to 0>><<elseif $health gt 4>><<set $health to 30>><<endif>>
    If you're using SugarCube, you can use Math.clamp().  For example, again assuming that the allowable range is 04:

    /% Subtract 1 from $health and clamp to the desired range (04). %/
    <<set $health = Math.clamp($health - 1, 0, 4)>>
    For the sake of convenience, and so as to not repeat yourself, you're probably better off wrapping up whatever you end up doing as a pseudo-macro (vanilla story formats) or widget (SugarCube).

    Alternatively, you could use a fairly simple function instead.  I don't know if you're one of the people who are allergic to JavaScript.


    Thanks for these and I would like to see the java script as I don't believe I am allergic to it :). I tend to change between formats to what suits my game better although I haven't tried Sugarcube and till now.
  • You could do it something like this.

    For vanilla story formats:

    window.modifyHealth = function (change) {
    var shv = state.history[0].variables;
    shv["health"] += change;
    if (shv["health"] < 0) {
    shv["health"] = 0;
    } else if (shv["health"] > 4) {
    shv["health"] = 4;
    }
    return shv["health"];
    };
    For SugarCube:

    window.modifyHealth = function (change) {
    var sav = state.active.variables;
    return sav["health"] = Math.clamp(sav["health"] + change, 0, 4);
    };
    Strictly speaking, since they both modify the $variable ($health in these examples) directly, there's no need to return it as well.  However, doing so hurts nothing and could occasionally be useful.


    Anyway.  Usage is exactly the same with either version and would go something like this:

    /% Add 2 to health. %/
    <<set modifyHealth(2)>>

    /% Subtract 2 from health. %/
    <<set modifyHealth(-2)>>

    [EDIT] Fixed both examples, per reply #5.
  • TheMadExile wrote:

    You could do it something like this.

    For vanilla story formats:

    window.modifyHealth = function (change) {
    var health = state.history[0].variables["health"]; // equivalent to $health
    health += change;
    if (health < 0) {
    health = 0;
    } else if (health > 4) {
    health = 4;
    }
    return health;
    };
    For SugarCube:

    window.modifyHealth = function (change) {
    var health = state.active.variables["health"]; // equivalent to $health
    return health = Math.clamp(health + change, 0, 4);
    };
    Strictly speaking, since they both modify the $variable ($health in these examples) directly, there's no need to return it as well.  However, doing so hurts nothing and could occasionally be useful.


    Anyway.  Usage is exactly the same with either version and would go something like this:

    /% Add 2 to health. %/
    <<set modifyHealth(2)>>

    /% Subtract 2 from health. %/
    <<set modifyHealth(-2)>>


    None of these seem to be working for me, with the java script one nothing happens to the number (I may be doing it wrong, i'm in sugar cane and I made a new script passage and put it in there and then the modify one in the passage I needed it). With the other sugarcane one it doesn't cap the limit for me.
  • If you're using Sugarcane, then you need to use the vanilla story format version and put it in a script tagged passage.  I also seem to have gotten a wee bit too aggressive with my caching and tried to use a scalar as a reference type (oops).  Try these fixed versions:

    For vanilla story formats:

    window.modifyHealth = function (change) {
    var shv = state.history[0].variables;
    shv["health"] += change;
    if (shv["health"] < 0) {
    shv["health"] = 0;
    } else if (shv["health"] > 4) {
    shv["health"] = 4;
    }
    return shv["health"];
    };
    For SugarCube:

    window.modifyHealth = function (change) {
    var sav = state.active.variables;
    return sav["health"] = Math.clamp(sav["health"] + change, 0, 4);
    };

    As far as the "other sugarcane one", there's a 30 where there should be a 4 in the second &lt;&lt;set&gt;&gt; in that example.  Here's a fixed version:

    /% Clamp the value of $health to the desired range (04), if necessary. %/
    <<if $health lt 0>><<set $health to 0>><<elseif $health gt 4>><<set $health to 4>><<endif>>
    I copy-pasted that from a post of mine from someplace else (answering the same question), and missed changing one of the original 30s to 4.


    PS: You don't really need to quote the entirety of a post when replying.
  • Yeah I know lol I don't know why I did. The script appears to work perfectly now thank you for your help. I need to know one more thing, how would I go about randomising the numbers? Similar to :
    <<set $health -= either (1, 2, 3)>>
    . I've tried 'either' but it tells me  'bad expression: modifyMoney either (.5, 1, .5)' but without 'either' it just does .5.
  • Based on the error message, you were omitting necessary parenthesis (i.e. parenthesis after a function's name are not optional).  Try this:

    <<set modifyHealth(either(0.5, 0.5, 1))>>
  • Yup it works! Thankyou so much for your help :D.
Sign In or Register to comment.