2 Replies - 261 Views - Last Post: 05 February 2013 - 03:28 PM Rate Topic: -----

#1 optix212  Icon User is offline

  • D.I.C Regular
  • member icon

Reputation: 18
  • View blog
  • Posts: 453
  • Joined: 10-October 09

Making a counter for every 5 increments.

Posted 05 February 2013 - 03:25 PM

Okay, I'm currently making a game, and the player has a level, and skills. What I want is for every 5 levels that the player gains, he gains a talent point that will effect one of his skills. Now I know that I could achieve this by doing a bunch of if statements, but I don't think that is the most efficient way to do it.

Here's an example off the top of my head:

if(level == 5){
talentpoints++;
}
if(level == 10){
talentpoints++;
}



I could easily implement it this way, but it seems redundant and kind of messy to say the least, and would also mean that there would be an eventual cap to talent points. What kind of counter could I implement to resolve this issue and make it more efficient?

Is This A Good Question/Topic? 0
  • +

Replies To: Making a counter for every 5 increments.

#2 Ryano121  Icon User is offline

  • D.I.C Lover
  • member icon

Reputation: 1362
  • View blog
  • Posts: 3,002
  • Joined: 30-January 11

Re: Making a counter for every 5 increments.

Posted 05 February 2013 - 03:27 PM

The modulus operator(%) will be your friend here. It returns the remainder after the division of its two operands. In your case

if(level % 5 == 0) { // meaning if level is a multiple of 5 as the remainder is zero after division
  // go up a talent point
}

Was This Post Helpful? 4
  • +
  • -

#3 optix212  Icon User is offline

  • D.I.C Regular
  • member icon

Reputation: 18
  • View blog
  • Posts: 453
  • Joined: 10-October 09

Re: Making a counter for every 5 increments.

Posted 05 February 2013 - 03:28 PM

AHH! Thank you so much sir!
Was This Post Helpful? 0
  • +
  • -

Page 1 of 1