Public Group

# How do i just add one??

This topic is 4618 days old which is more than the 365 day threshold we allow for new replies. Please post a new topic.

## Recommended Posts

if (exp == exp_cap){ level+=1;}
if (level == 1){ exp_cap+=50;}

Works for the begining, but then while the characters level is 1 it constantly adds 50 to exp_cap. I just want to add it once, those line of codes are located in the while loop. What can i do to just add 50 once, remove it form the while loop?? Thanks

##### Share on other sites
maybe something like this?

if (exp == exp_cap) { level+=1; if (level == 1)   exp_cap+=50;}

##### Share on other sites
I think you mean

if (exp == exp_cap){     level+=1;    if (level == 1) {        exp_cap+=50;    }}

So at level one, exp_cap goes up by 50, after that exp_cap stays the same.

##### Share on other sites
You have to solve the problem in a more stable way.
If you do it like that I think that it will be plagued with problems.

Consider exp is equal to 1 less than exp_cap. What will happen if the user earns 2 experience points?

typedef unsigned int experience_t;typedef unsigned int level_t;struct Player{   experience_t xp;   level_t      level;};experience_t xp_per_level[] = {   50,   150,   400,   1000};const int xp_per_level_size = sizeof(xp_per_level) / sizeof(Experience);level_t level_from_xp(experience_t xp){   int level=0;   for(; level<xp_per_level_size; ++i)   {      if(xp < xp_per_level[level])         break;   }   return level+1; //+1 because level starts at 1 but our index starts at 0}

Then when the player earns XP you can do something like:
earn_xp(Player* player, experience_t xp_earned){   experience_t new_xp = player->xp + xp_earned;   level_t new_level   = level_from_xp(new_xp);   //additional checks here   //If everything is OK   if(player->level != new_level)   {//Player has gained (or lost!) a level      //Do something about it   }   player->xp    = new_xp;   player->level = new_level;}

[edit: speaking of stable, here's a level_from_xp() that might actually work]

[Edited by - Shannon Barber on March 2, 2006 7:58:21 PM]

1. 1
Rutin
46
2. 2
3. 3
4. 4
5. 5
JoeJ
19

• 11
• 15
• 9
• 10
• 13
• ### Forum Statistics

• Total Topics
633004
• Total Posts
3009837
• ### Who's Online (See full list)

There are no registered users currently online

×