Howdy, Stranger!

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

[Harlowe] Basic Limit on Health

Newbie question.
I'm building in a simple health system for my character as he progresses through the story and at certain stages I deduct or add health points.

The trouble is, I don't want there to ever be negative numbers or a number over 100 ever reached. Therefore, I'd like the range or limit be 0 - 100 on the health.

My question is, how do I put that logic into the code?

Comments

  • Harlowe has no built in method to restrict the min/max range of a variable.

    You could create a basic Javascript function like the following, it takes four parameters: the current value of the variable, the amount you want to change by, the minimum allowed result, and the maximum allowed result.
    Add this to your Story Javascript area.
    if (! window.restrictedChange) {
    	window.restrictedChange = function(value, change, min, max) {
    		var result = value + change;
    		if (result < min)
    			result = min;
    		else if (result > max)
    			result = max;
    
    		return result;
    	};
    }
    

    The following tests show how to increase and decrease the value:
    Test increasing a value past the max allowed. {
    (set: $health to 80)
    (set: $health to restrictedChange($health, 25, 0, 100))}
    health: $health
    
    
    Test decreasing a value below the min allowed. {
    (set: $health to 20)
    (set: $health to restrictedChange($health, -25, 0, 100))}
    health: $health
    
  • Thanks! I'll do that. Appreciate the help!
  • Actually, in case anyone else finds this useful;

    I thought I'd try using some additional code to what I already had to make it work.
    It's probably ugly and inelegant but it appears to be working properly.

    I figured since when I deduct health points I could check if it was negative and then if it was, set health points to 0. And vice versa to change it to 100 instead of over 100.
    (set: $hp to $hp - 5)(if: $hp < 0)[(set: $hp to 0)]
    
    or
    
    (set: $hp to $hp + 10)(if: $hp > 100)[(set: $hp to 100)]
    
    

    It's only slightly more coding and I can just slide it in on the end of what I already have.
    This will work for my purposes.
  • A simpler way, that depends on when you need to check the HP. If you don't need to check the HP after a change until the next passage, then you could write your (if:) macro in a passage tagged as header. HP over 100 would be reduced every time you move to another passage.

    This depends on whether you need or not to check the HP before moving to a passage. If you do, you have to include your check right after the HP change. If you don't, you can forget about it and the next passage will run the check automatically from the header passage.
  • Thanks for mentioning that, Menti.

    As it turns out, I don't need to check it very often. Only when adding or subtracting from what it already is. So, in my case, not in very many passages.

    But that's useful to know for future reference! Thanks, again.
Sign In or Register to comment.