How do I make something happen each 100 on a variable?

0 favourites
  • 8 posts
From the Asset Store
This sound pack comes with 100 high quality, ready-to-use sound effects for all kinds of games.
  • (I'm keeping this simple, and only giving you the info you need to know to answer) So I want my player to level up each time they reach 100 exp, and I cant make it for like once 200, 300, and forever, so I need a way to make it EVERY TIME THE VARIABLE HAS CHANGED BY 100.


  • You're going to want to check if your experience value can be divided by 100 with a remainder of 0.

    For this, there is the modulo operator %. Modulo gives you the remainder of a division.


    200 % 100 = 0

    301 % 100 = 1

    950 % 100 = 50


    if experience % 100 = 0 then you know the experience is a multiple of 100.

    This won't work if they surpass the multiple of 100, going from 99 to 101 for example, so you'll have to do something a bit fancier like this:

    (Warning, some math and programming shenanigans)

    It boils down to this condition, which I will attempt to explain:

    floor( currentExp / expNeededToLevel ) > floor( oldExp / expNeededToLevel ) -> if true, level up!

    "floor()" rounds whatever is in parenthesis down to the nearest integer (whole number). So 0.9 would round to 0 -- and 1.1 would round to 1.

    "expNeededToLevel" is the amount of experience needed in order to level up (in your case this is 100).

    "oldExp" is the experience value BEFORE adding new experience.

    "currentExp" is the experience value AFTER adding new experience.

    ">" is the "greater than" operator and checks if the value on the left of it is greater than the value on the right of it, and says true (1) or false (0) about it.

    So if your character had 99 experience, then killed a bug and gained 2 experience, it would go like this:

    expNeededToLevel = 100

    oldExp = 99

    currentExp = 101 (after adding 2 for the bug)

    So if you remember the condition from above:

    floor( currentExp / expNeededToLevel ) > floor( oldExp / expNeededToLevel )

    If I plug in the values you get:

    floor(101 / 100) > floor(99 / 100)

    which reduces to:

    floor(1.01) > floor(0.99)

    after floor reduces to:

    1 > 0

    meaning "one is greater than zero" which is a true statement, so your character needs to level up!

    You can replace 100 with any number of experience you want.

    I hope this helps you.

  • Valhallen

    "floor( currentExp / expNeededToLevel ) > floor( oldExp / expNeededToLevel ) -> if true, level up!"

    When I compare you can't enter floor into the variable...

    can I get more explaination on floor? I know what it does, but how do I add it in?

    I get a dropdown box, so I cant enter in floor()

  • Try Construct 3

    Develop games in your browser. Powerful, performant & highly capable.

    Try Now Construct 3 users don't see these ads
  • I've haven't actually used Construct 2 yet, but I see in the documentation there is a System Condition called "Compare two values" which can take two expressions.

    An expression is something that needs to be evaluated before the condition is checked. You can type it right in, instead of a simple number.

    [quote:ooh7maok]Compare two values

    Compare any two expressions (which can either numbers or text) with each other. They can be compared as Equal, Not equal, Less, Less or equal, Greater or Greater or equal.

    You'll want to use the "Greater" comparison.

    I plan on testing out Construct 2 tonight, so I'll come back and add some more detail to this post if you haven't figured it out by then.

  • Hm, for now (until I get a response from you) I'll use change by 1, and lower the value to level up for each 30.

  • Valhallen Hi, I figured I could do the simple way, because my exp always changes by 5.

    But when the players XP is EXACTLY on 100, it will rapidly change the level by 1 because the statement of the player having a multiple of 100 is STILL true, so my player could hack.

  • You should make sure that you only check the condition one time when experience is added, rather than on tick or at an interval.

    Alternatively you could keep track of how much experience has been gained since the last level up. If that value becomes greater than 100, then level up and set it back to 0.

  • Its working now

    Thanks =)

Jump to:
Active Users
There are 1 visitors browsing this topic (0 users and 1 guests)