Allegro.cc - Online Community

Allegro.cc Forums » Programming Questions » Displaying int as text.

Credits go to Ceagon Xylas for helping out!
This thread is locked; no one can reply to it. rss feed Print
Displaying int as text.
julian_boolean
Member #8,201
January 2007

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
Member #5,495
February 2005
avatar

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

julian_boolean
Member #8,201
January 2007

Thank you!

CGamesPlay
Member #2,559
July 2002
avatar

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

--
Tomasu: Every time you read this: hugging!

Ryan Patterson - <http://cgamesplay.com/>

julian_boolean
Member #8,201
January 2007

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
Member #5,313
December 2004
avatar

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
Member #8,201
January 2007

I just have something really simple like:

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

  total_exp = 0;
  exp_to_next_lvl = lvl * 100;
}

CGamesPlay
Member #2,559
July 2002
avatar

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.

--
Tomasu: Every time you read this: hugging!

Ryan Patterson - <http://cgamesplay.com/>

LennyLen
Member #5,313
December 2004
avatar

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
Member #8,201
January 2007

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
Member #2,559
July 2002
avatar

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++;
}

--
Tomasu: Every time you read this: hugging!

Ryan Patterson - <http://cgamesplay.com/>

julian_boolean
Member #8,201
January 2007

Thank you very much :)

LennyLen
Member #5,313
December 2004
avatar

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
Member #8,201
January 2007

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
Member #2,559
July 2002
avatar

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.

--
Tomasu: Every time you read this: hugging!

Ryan Patterson - <http://cgamesplay.com/>

julian_boolean
Member #8,201
January 2007

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
Member #2,604
August 2002
avatar

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.

---
Me make music: Triofobie
---
"We need Tobias and his awesome trombone, too." - Johan Halmén

Go to: