0 votes
by (220 points)
So lets say the player is level 1 and the exp cap to reach level 2 is 100, for level 3 is another 200, 4 is 300, 5 is 500 etc. so is there a way to link level($lvl) to exp($exp) in that way?

2 Answers

0 votes
by (44.7k points)

You could create your level stats as an array on the SugarCube setup object in your StoryInit passage like this:

<<set setup.levels = [0, 100, 200, 300, 500, Infinity]>>

(The last level there should always be set to "Infinity".)

Then create a function like this in your JavaScript section:

window.getLevel = function(exp) {
	return setup.levels.findIndex(function(element) {
		return element > exp;

(See the .findIndex() method for details.)

Once you've done those two things, you can get the current level at any time just by doing:

<<set $level = getLevel($exp)>>

If you store the level in the $level variable, then you could check for level-ups like this:

<<if $level != getLevel($exp)>>
	<<set $level = getLevel($exp)>>
	/* put level-up code here */

Hope that helps!  :-)

0 votes
by (510 points)

Hi, in my project i use simple system on variable.

I can configure it and change for how i want.

In first, create player in "StoryInit":

/*---Players Stats start---*/
<<set $player to {
		name: "",
		level: 1,
		strength: 1,
		perception: 1,
		endurance: 1,
		charisma: 1,
		intelligence: 1,
		agility: 1,
		skillpoints: 7,
/*---Players Stats end---*/

Then, create XP and level formulas:

/*---Players Level start---*/

<<set $pcNextLvl to ($player.level+1)>>

<<set $pcExpMod to ($player.intelligence*0.03)>> /*modificator for xp scaling*/

<<set $XPBase to 5>> /*for this variable can change MaxEXP */

<<set $XPBumpBase to ($XPBase*1.5)>> /*for this variable can change MaxEXP */

<<set $CurEXP to 0>>  /* Current EXP for the game start */

/*---Players Level end---*/

And then, you can put this code in PassageHeader (for calculeting you MaxEXP in eve passage:

<<set $MaxEXP to Math.trunc((($XPBase*$pcNextLvl)+$XPBumpBase)*(1-$pcExpMod))>>  /* Maximum EXP */

Then, you just use LVL-UP system you want (scaling stats, adding new, and other).

For example, after winning battle i use this :

/* after battle win*/
<<set $CurEXP to ($CurEXP + $expDrop)>>
<<set $battleMode to 0>>
<<if $CurEXP >= $MaxEXP>>
	<<include LevelUp>>


/*in PassageHeader*/

<<if $CurEXP >= $MaxEXP>>
	<<include LevelUp>>

And in "LevelUp":

<<set $player.level to ($player.level++)>>
<<set $CurEXP to $CurEXP-$MaxEXP>>
<<set $CurHP to $MaxHP>>

We adding 1 level to current, then we reset player EXP to new, and set health to max (restore it).

You can make other things with this code.

Adding stats like +1 to Strenght, or not restore health.