- Home /
MMO Like Experience System
I am trying to create a MMO like leveling system that has a bar across the screen that as you level fill up then when you level it clears and starts to fill all over again. I have made one that works for one level but I cant seem to figure out how to make one for multiple levels that take different experice to reach.
Any one know where I could find a tutorial on this or somthing close that I could mod to work. I have some good basic knowlege of C# and am studying hard in it to learn all I can but at the time beeing im now sure how to do this and I realy need it for a class project we are working on.
Answer by Dracorat · Jun 21, 2013 at 05:05 PM
The steps are:
Identify the experience needed for each level. Identify which level you're in based upon these thresholds. Subtract the top of the last level from your current xp amount to get the amount "in to this level" you are. Subtract the top of the last level from the top of this level to get the amount "of xp required this level" Divide "amount I'm in this level" by "amount required" to get "percent in this level".
Draw bar PERCENT wide. Where percent is the amount you just calculated and you multiply that percent by the width of a 100% bar. (If you are 20% in and the bar is 200 px wide, it would be 0.20 * 200 = 40 pixels wide)
void Main()
{
XP xp = new XP(1300);
Console.WriteLine("Our character starts at {0} xp and is level {1}.", xp.GetCurrentXp(), xp.GetCurrentLevel());
Console.WriteLine("Now, we add 2000 xp.");
xp.AddXp(2000);
Console.WriteLine("Now, our character has {0} xp and is level {1}.", xp.GetCurrentXp(), xp.GetCurrentLevel());
Console.WriteLine("Our character is {0}% in to his next level", xp.GetPercentInToLevel() * 100f);
}
public class XP {
private int[] xpTargets = new int[] {0, 1000, 3000, 6000, 10000};
private int currentXp = 0;
public XP() { }
public XP(int startingXp) { this.SetXp(startingXp); }
public int GetCurrentXp(){
return this.currentXp;
}
public int GetCurrentLevel(){
for(int i = 0; i < (this.xpTargets.Length - 1) ; i++){ // Have to minus one since we're checking against "the next item" and there is no "next item" at the end of the list
if(this.currentXp == this.xpTargets[i] || (this.currentXp > this.xpTargets[i] && this.currentXp < this.xpTargets[i + 1])){
return (i + 1); // Since i is 0-based
}
}
//If we're here, they're at the max level
return this.xpTargets.Length;
}
public float GetPercentInToLevel(){
int currentLevel = this.GetCurrentLevel();
if(currentLevel == this.xpTargets.Length){
// At max level, so 100%
return 1.0f;
}
decimal xpInToLevel = (decimal)currentXp - (decimal)this.xpTargets[currentLevel - 1];
decimal xpTopEnd = (decimal)this.xpTargets[currentLevel] - (decimal)this.xpTargets[currentLevel - 1];
float percent = (float)xpInToLevel / (float)xpTopEnd;
return percent;
}
public void SetXp(int newXp) {
this.currentXp = newXp;
}
public bool AddXp(int xp){
int startingLevel = this.GetCurrentLevel();
this.currentXp += xp;
if(this.GetCurrentLevel() == startingLevel) {
return false; // We didn't level up with this xp
}
return true; // We leveled up.
}
}
Output:
Our character starts at 1300 xp and is level 2.
Now, we add 2000 xp.
Now, our character has 3300 xp and is level 3.
Our character is 10% in to his next level
How can I modify this so that xp is 0 at each level up but at the same time keep my level?
Your answer
![](https://koobas.hobune.stream/wayback/20220613104633im_/https://answers.unity.com/themes/thub/images/avi.jpg)
Follow this Question
Related Questions
Multiple Cars not working 1 Answer
new user very easy c# question regarding syntax 1 Answer
Distribute terrain in zones 3 Answers
Health Help D: 2 Answers
Creating a timer for combat (C#) 1 Answer