- Home /
xp math equation help
i want to make the same formula runescape has for leveling up in my code but i am unsure of how to put the equation in javascript here is my current code
#pragma strict
var gain : boolean = false;
var i = 10;
var xp = 0;
var lvl = 1;
var x = 0;
var lvlup : boolean = false;
var item : GameObject;
var spawn : Transform;
var w = 0;
function Start () {
}
function Update ()
{
//runescape math equation here
x = w;
if(gain)
{
xp += i;
gain = false;
}
if(xp >= x)
{
lvlup = true;
}
if(lvlup)
{
lvl += 1;
xp = xp - x;
lvlup = false;
}
}
function OnGUI()
{
if(GUI.Button(Rect(0, 0, 100, 100), "xp"))
{
Instantiate(item, spawn.position, spawn.rotation);
}
GUI.Box(Rect(0, 0, 100, 50), xp.ToString());
}
function OnTriggerEnter(other : Collider)
{
if(other.tag == "xpgain")
{
gain = true;
}
}
Do you really need that complex of an equation??!! This is extra computation for little bonus. If you were in a paid project and I would be paying you, I would tell you to keep that once all the core of the game is ready. But reality is I am not paying you so you can use it...
i like the equation it uses its good for my game granted i will change it by a small value but im trying to figure out how to plug that into my script in few lines as possible
Answer by zharik86 · Jun 12, 2014 at 06:25 AM
If you need to write a formula in your script, here(I wtrite on Java):
function getNextExp(int curLvl): float {
var nextExp: float = 1; // I initializate this value, because in case curLvl = 1, your formula don't work
var tempic: float = 0;
for(var i: int = 1; i <= curLvl -1; i++) {
tempic = tempic + Mathf.Floor(i + 300*Mathf.Pow(2, i/7));
}
tempic = Mathf.Floor(tempic / 4);
if (tempic > 0) {
nextExp = tempic;
}
return nextExp;
}
I hope it to you will help. But it is possible, for computation of XP it is better to use parabolic or logarithmic functions.
Your answer
![](https://koobas.hobune.stream/wayback/20220613151338im_/https://answers.unity.com/themes/thub/images/avi.jpg)