Jump to content
  • Advertisement
Sign in to follow this  
zike22

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.

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
Advertisement
maybe something like this?

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

Share this post


Link to post
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 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
Sign in to follow this  

  • Advertisement
×

Important Information

By using GameDev.net, you agree to our community Guidelines, Terms of Use, and Privacy Policy.

We are the game development community.

Whether you are an indie, hobbyist, AAA developer, or just trying to learn, GameDev.net is the place for you to learn, share, and connect with the games industry. Learn more About Us or sign up!

Sign me up!