Stats scale

Hello anybody!

The hero has stats: ENDurance and INTelligence.
maxHP depends on the END.
maxMP from INT.


<<set $player = {
END: 5,
INT: 10,

<<set $player.maxHP = (($player.END * 6))>>
<<set $player.maxMP = (($player.INT * 10))>>

When I start the game, it was works correctly: maxHP=30 & maxMP=100. But when I earn the level, and increase END & INT nothing happens (stats grow up BUT maxHP/MP isn’t changes).

Please, help me to work this formula or get another one.

Thank You!

The formulas are fine, but StoryInit only gets run once at the beginning, when the story is initialized. You just need to run them again upon level up. I don’t know SugarCube, so I don’t know the principled way to do subroutines, but if you need a workaround before someone actually knowledgeable replies, you could probably just copy and paste the two last lines.

Thanks! This is could been the temporarily solution!
IDK JavaScript, but I think it is way to get opt there.
Maybe anyone can suggest the better way with JS code?

For what you describe, you’d be better off just turning them into JavaScript functions in your JavaScript section, like this:

window.maxHP = function () {
	return State.variables.player.END * 6;  // maxHP = $player.END * 6

window.maxMP = function () {
	return State.variables.player.INT * 10;  // maxMP = $player.INT * 10

// Make sure "value" is within min and max values.
window.within = function (value, min, max) {
	if (value > max) {
		return max;
	} else if (value < min) {
		return min;
	return value;

and then in your code you could use those functions like this:

<<set $player.HP = within($player.HP + _additionalHP, 0,  maxHP())>>

and that would make sure that, after adding in _additionalHP, the player’s hitpoints remains within a range from 0 HP to the return value of maxHP(), inclusive.

Hope that helps! :slight_smile:

P.S. You probably didn’t do this in your actual code, but within your generic objects make sure you don’t have a comma after the last item, like you did in your sample above with “INT: 10,”. There are a couple of older browsers which don’t like that, IIRC.

Why not simply use Math.clamp() for clamping, rather than reinventing the wheel?

Can You show some examples how this method works, please?
I read documentation but I have no way how I can use it.

You simply use it in place of the custom within() function supplied by HiEv. For example:

<<set $player.HP = Math.clamp($player.HP + _hpModifier, 0, maxHP())>>
<<set $player.MP = Math.clamp($player.MP + _mpModifier, 0, maxMP())>>

That will clamp any changes to the player’s HP/MP to between 0 and their maximum value.

Also. Since you’d no longer be using the within function, you don’t need to define it within your JavaScript section either, so you could reduce the code they supplied to just the definitions of maxHP() and maxMP().

Ah, dang it, I thought there was a function for that, but I couldn’t find it in the MDN. I forgot that clamp() was a part of SugarCube.

Is that for leveling-up type stuff?

It’s for anything where you’ll be modifying a number value, but you don’t want it to go outside certain bounds.