Sign in to follow this  

How do i just add one??

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

If you intended to correct an error in the post then please contact us.

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 this post


Link to post
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]

Share this post


Link to post
Share on other sites

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

If you intended to correct an error in the post then please contact us.

Create an account or sign in to comment

You need to be a member in order to leave a comment

Create an account

Sign up for a new account in our community. It's easy!

Register a new account

Sign in

Already have an account? Sign in here.

Sign In Now

Sign in to follow this