using a health bar for a exp bar

hey guys, i have a health bar script here and have been trying to use it for a exp bar, it works however it doesnt go from a to b like i would like it to (like Wows or Aions).

function OnGUI () {
	GUI.Box(Rect(Loc.x,Loc.y,XCord /2 /(EXPToLevel / EXP), 20), EXP + "/" + EXPToLevel); 
	}

i guess its happening becuase im dividng it by the screen? or i’ve noticed if i remove the /2 then it wont grow in size according to the EXP and EXPToLevel but its rather small how would i make it longer? thanks

ive kind of fixed, however it stops at a certain postion which im guessing is 1200 in coords.

im trying to think of a way to to make it so the highest value is the end of the bar and ofcourse the lowest is the low end. heres my full script:

var StartLevel = 1;

var Level = 1;
var SetLevel = 1;

var ExpMultiplyer = 1.25;

var EXP = 1;

var MaxLevel = 10;

var EXPToLevel = 1000;
var EXPToLevelB = 1000;

var LevelChanged = false;

var EXPCalc = 0.0;

var Loc : Vector2 = Vector2 (100,100);
var XCord = 1200;




function Start () {
PlayerPrefs.GetInt("Level",Level);
PlayerPrefs.GetInt("EXP", EXP);
Level = PlayerPrefs.GetInt("Level");
EXP = PlayerPrefs.GetInt("EXP");
EXPCalc = Level * ExpMultiplyer / 2;
EXPToLevel = EXPCalc * EXPToLevelB;
}


function Update () {
if(LevelChanged) {
PlayerPrefs.SetInt("Level",Level);
PlayerPrefs.SetInt("EXP",EXP);
LevelChanged = false;
	}
	
if(Input.GetKeyDown("l")) {
	EXP += 101;
}
if(Input.GetKeyDown("r")) {
RunReset ();
}
if(EXP >= EXPToLevel) {
	OnLevelUp ();
	print("levelUP");
	}
}

function OnGUI () {
	GUI.Box(Rect(Loc.x,Loc.y,XCord /2 /(EXPToLevel / EXP), 20), EXP + "/" + EXPToLevel); 
	}
	

function OnLevelUp () {
EXPCalc = Level * ExpMultiplyer / 2;
EXP -= EXPToLevel;
EXPToLevel = EXPCalc * EXPToLevelB;
Level ++;
LevelChanged = true;
}

function RunReset () {
Level = 1;
EXP = 1;
EXPToLevel = 1000;
LevelChanged = true;
}

I’m not sure what you’re aiming for with your width calculation; if XCord is meant to be the width of the bar when full, why divide that by 2? I’m assuming EXP represents the player’s current experience points and EXPToLevel represents the number of experience points they need to reach the next level. If you want a bar that goes from 0% to 100% as the player progresses towards the next experience level, you’ll also need to know how many experience points were required for the previous level.

var EXPToLevelUp : int  = EXPToLevel - EXPToLastLevel; // number of exp. pts. required this level
var EXPGained : int = EXP - EXPToLastLevel; // number of exp pts. gained since last level-up
var EXPPercent : float = (float) EXPGained / (float) EXPToLevelUp; // % progress through current level
EXPPercent = Mathf.Clamp(EXPPercent, 0f, 1f); // make sure % is between 0.0 and 1.0

var BarWidth : int = (int) (XCord * EXPPercent);

Some further explanation: I’m assuming EXP doesn’t reset to 0 when the player levels up; rather that EXPToLevel gets increased, and EXP continues to increase from where it is. Hence you need to know what EXPToLevel was for the level the player is currently at, so you can figure out the number of experience points the player needs to gain between reaching his current level and reaching the next level. Then you can calculate a percentage and apply that to your bar width.

well it automatically calcs the amount, when you level up it will automatically inrease amount for next level up, and say its 1000 exp to level up and you have 1200 it will keep the 200 remainder and add it onto the next level. Im just slightly confused because if you try the script you will notice it does go up like an exp bar but it stops at a certain point. ill try to draw a diagram below.

If the player starts as Level 1 at 0 EXP, advances to Level 2 at 500 EXP, and advances to Level 3 at 1000 EXP, what should the bar look like when the player has 499 EXP? when he has 501 EXP? 999 EXP?

I’m assuming what you want is for the bar to go from empty to full as EXP goes from 0 to 499; should empty again when the player levels up, and go from empty to full as the player progresses from 500 EXP to 999 EXP; and so on. If that’s not what you want, ignore my last reply and describe the behaviour you want in more detail.

If that is what you want, then as I said before you’ll need to know the EXP values represented by both ends of the bar in order to calculate what percentage of the bar to fill in.

It occurs to me from your diagram that you might be resetting EXP to zero every time the player levels up. I specifically said in my previous reply that I assumed that was not the case, but if it is: EXPToLastLevel would always be 0, and EXPPercent would then be just (EXP / EXPToLevel).

The point is, you need to calculate what percentage of the total width of the bar to draw for any value of EXP. If I misunderstood what the bar was representing my math might not be what you need, but the principle is correct. Calculate a percentage, verify you have that calculation correct, then use that percentage to scale the bar.

well, im not totally understanding you. at the moment to bar works fine it grows depending on when you get experiance, and will never go to 0 again on level up unless you get the exact amount to level up. that part of the script is fine, the problem is there is a dead zone on the bar that will not grow when near 100% like 95%-100% of the bar will not move…so 950/1000 exp the bar will not move at this point, but it will everywhere else. its probally becuase its getting capped by the Xcord but i want to make the size of the bar relative to 0-100% of the exp… i cant really explain it.

The point is, your math doesn’t make sense. Plug the numbers you have into the calculations you’re using, and you will quickly see that the results don’t make sense. You want a bar that is P% of W pixels wide, where P% is the percentage of the way through the level, and W is the total width of the bar when full.

Break it down:

  • Try to draw a fixed, W-pixel wide bar
  • Try to draw a bar P% of W pixels wide, with P set to a fixed number
  • Vary the value of P manually (i.e. try P = 0.0, P = 0.5, P = 1.0, etc)
  • Once the bar is behaving correctly for any value of P between 0 and 1:
  • Try to calculate P based on EXP and ExpToLevel

If you separate out the drawing of the bar from the calculation of P, you’ll be able to concentrate on the two distinct aspects of the problem and work out problems with each independently. You’ll also be able to see more clearly what’s going on when you log the values of intermediate calculations, since they’ll only be describing one aspect of the problem at a time.

Go through that exercise, keep the above explanations handy, and I’m sure you’ll figure it out pretty quickly.

just to clarify, have you tryed the script yet? =) you may be able to see what my problem is straight up

Laurie already told you how to debug it yourself.

I’ve also explained what’s wrong with the script, and what needs to be changed. Trying the script wont change what the problems are or what needs to be done to get it working as intended. What I outlined is a process to follow in order to understand those problems and changes.