Displaying int as text.
julian_boolean

I'm doing some testing and just trying to check to see if my functions are working as they should but I'm not sure how I can display my results with textout using an int.

void cPlayer::gain_exp()
{
  set_totalexp(get_totalexp() + 10);    
}

// Elsewhere

  player = new cPlayer();

  if (key[KEY_A])
    player->gain_exp();

Ceagon Xylas
int i=32;
textprintf_ex(...,"The value of 'i' is: %i",i);

julian_boolean

Thank you!

CGamesPlay

There are many more options to format the number, and to use different types (like floats or strings). Read about them here:
printf

julian_boolean

I have another problem that's sorta unrelated to the topic but I'm going to just stick it in here.

void cStats::calc_lvl()
{
  if (total_exp >= exp_to_next_lvl)
    if (lvl != max_lvl)
      lvl++;
}

This function is being put into my game loop somewhere, and somewhere else I'm increasing total_exp using a different function. The problem is that once I reach the first exp_to_next_lvl (which happens to be 100) it won't stop incrementing until it reaches max_lvl, which is 20. It's suppose to increment lvl once after total_exp matches exp_to_next_lvl, obviously, but it's just not doing that.

LennyLen
Quote:

This function is being put into my game loop somewhere, and somewhere else I'm increasing total_exp using a different function. The problem is that once I reach the first exp_to_next_lvl (which happens to be 100) it won't stop incrementing until it reaches max_lvl, which is 20. It's suppose to increment lvl once after total_exp matches exp_to_next_lvl, obviously, but it's just not doing that.

You need to reset total_exp to 0 for one thing. How you change exp_to_next_lvl m,ay also be suspect. You haven't posted enough code though.

edit: Actually, you may not need to reset total_exp to 0. It depens on what you mean by exp_to_next_lvl.

julian_boolean

I just have something really simple like:

cStats::cStats()
{
  lvl = 1;
  max_lvl = 20;

  total_exp = 0;
  exp_to_next_lvl = lvl * 100;
}

CGamesPlay

That part gets executed for the first level, when the class is created, and not again. You have to set exp_to_next_lvl to the amount of exp_to_the_next_level when it changes. Actually, you should make a function ExpToNextLevel(), that returns the amount needed.

LennyLen

So exp_to_next_lvl is only calculated once then (when the constructor is called)? exp_to_next_lvl will always be 100.

edit: A pox on CGamesPlay. :P

julian_boolean
int get_exp_to_next_lvl() { return exp_to_next_lvl; }

...

void cStats::calc_lvl()
{
  if (total_exp >= exp_to_next_lvl)
    if (lvl != max_lvl)
    {
      lvl++;
      get_exp_to_next_lvl();
    }
}

Hmm?

CGamesPlay
int get_exp_to_next_lvl() {
    // Calculate how many experience before the player levels up
    return lvl * 100;
}

void cStats::calc_lvl()
{
  if (total_exp >= get_exp_to_next_lvl() && lvl != max_lvl)
    lvl++;
}

julian_boolean

Thank you very much :)

LennyLen

You don't even need a get_exp_to_next_lvl() function.

void cStats::calc_lvl() {
    if (total_exp >= exp_to_next_lvl && lvl != max_lvl) {
        lvl++;
        exp_to_next_lvl = lvl * 100;
    }
}

julian_boolean

Hmm.. One more thing. I want to make it so it's not possible to gain experience past max_experience (1000) but nothing I've tried seems to work.

// Inside the calc_lvl function

if (total_exp >= max_exp)
  return;

// Or maybe this?

if (total_exp >= max_exp)
  total_exp = max_exp;

CGamesPlay
Quote:

You don't even need a get_exp_to_next_lvl() function.

Of course not, but I think it makes more sense to have one than to keep a variable with it around all the time. Keeping it as a variable means you have to keep it synchronized with the current level... just a waste of time.

[append]
The second one, julian_boolean, but use = instead of ==. == is for comparing things.

julian_boolean

Sorry that was just a typo. It's still going beyond 1000. Well but then it switches back to 1000, but it looks kinda buggy that way.

Tobias Dammers

max it whenever you add anything to exp. You should have an add_exp() function somewhere that checks for <= 1000. std::min() is a useful friend here:

void cCharacter::add_exp(int xp_added) {
  xp = std::min(xp + xp_added, max_xp);
}

Somm'n' like that.

Thread #591679. Printed from Allegro.cc