Could use some help setting up a javascript function

Twine Version: 2.314
Story Format: Sugarcube 2.341

Hey all, any Javascript experts out there? I need a function that will do some probability checks and return the updated value if they pass. The function will be passed two arguments, a random number and the player skill level, both are integers. I want there to be a random chance the player gains a skill point, but the chances are reduced if they have a higher skill level (harder to improve if you’re already very good right?). This will be a workhorse function used 100+ times. I don’t think there is any way to easily do this with Sugarcube macros, short of writing a lot of code every time I want to use it.

So let’s assume the two variables passed are iSkill and iRandom, both will be 0-100 in value.

The JS code will look like below, but I need help declaring the function in Twine and how to call it, thanks.

if (iSkill > 80 && iRandom > 95) {
return (iSkill + 1);
}
else if (iSkill > 60 && iRandom > 85) {
return (iSkill + 1);
}
else if (iSkill > 40 && iRandom > 60) {
return (iSkill + 1);
}
else if (iRandom > 55) {
return (iSkill + 1);
else {
return (iSkill);
}

1 Like

I don’t know how to work with twine, but I’d just like to point out that your logic will always return iSkill + 1.

Thanks will work on that

1 Like

If you want to use that in Twine/SugarCube, then you’d put this in your JavaScript section:

setup.skillup = function (iSkill, iRandom) {
	if (iSkill > 80 && iRandom > 95) {
		return iSkill + 1;
	} else if (iSkill > 60 && iRandom > 85) {
		return iSkill + 1;
	} else if (iSkill > 40 && iRandom > 60) {
		return iSkill + 1;
	} else if (iRandom > 55) {
		return iSkill + 1;
	}
	return iSkill;
};

That uses the SugarCube setup object, because it’s available to the passage code.

Then you can call it by doing:

<<set $skill = setup.skillup($skill, _random)>>

Though, you could probably simplify that by creating the randomized value within the function:

setup.skillup = function (iSkill) {
	var iRandom = random(1, 100);
	if (iSkill > 80 && iRandom > 95) {
		return iSkill + 1;
	} else if (iSkill > 60 && iRandom > 85) {
		return iSkill + 1;
	} else if (iSkill > 40 && iRandom > 60) {
		return iSkill + 1;
	} else if (iRandom > 55) {
		return iSkill + 1;
	}
	return iSkill;
};

with that you’d only need to do:

<<set $skill = setup.skillup($skill)>>

Alternately, you could prevent mistakes where you accidentally set the wrong skill this way:

setup.skillup = function (skillVar) {
	var iRandom = random(1, 100);
	var iSkill = State.getVar(skillVar);
	if (iSkill > 80 && iRandom > 95) {
		iSkill += 1;
	} else if (iSkill > 60 && iRandom > 85) {
		iSkill += 1;
	} else if (iSkill > 40 && iRandom > 60) {
		iSkill += 1;
	} else if (iRandom > 55) {
		iSkill += 1;
	}
	State.setVar(skillVar, iSkill);
};

That uses the State.getVar() and State.setVar() methods to get and set the SugarCube variables.

Now you’d only need to do:

<<run setup.skillup("$skill")>>

And, finally, if you wanted to do it as a widget, then you could put the following code into a “widget” tagged passage:

<<widget "skillup">>
	<<set _iRandom = random(1, 100)>>
	<<set _iSkill = State.getVar($args[0])>>
	<<if _iSkill > 80 && _iRandom > 95>>
		<<set _iSkill += 1>>
	<<elseif _iSkill > 60 && _iRandom > 85>>
		<<set _iSkill += 1>>
	<<elseif _iSkill > 40 && _iRandom > 60>>
		<<set _iSkill += 1>>
	<<elseif _iRandom > 55>>
		<<set _iSkill += 1>>
	<</if>>
	<<run State.setVar($args[0], _iSkill)>>
<</widget>>

and then you’d call that by doing:

<<skillup "$skill">>

Make sure you put the variable name in quotes like I did in that example, because its name needs to be passed as a string for that to work.

Enjoy! :slight_smile:

1 Like

Thank you so much HiEv :crazy_face: