Branch Condition with divisible number

Hello at everybody, how can i do the correct branch condition? I explain the problem: in my game, there is a variable “Score” and it increments 1, 2, 3, 4 and so on during the game. Basically, the character gains a coin when Score reaches 20, 40, 60, 80, 100, 120 etc etc. Of course i need an automatic system so that when Score reaches a number divisible by 20, the variable “Coin” increments of one unit.

Do you have some idea for the right branch condition?

1 Like

if Total Coin % 20 == 0 AFTER you add the coin, then you know you’ve reached a multiple of 20 at that moment.
% is the modulo operator which gives the remainder of a division operation. If the remainder is 0 then you know you have an exact multiple of the number you’re dividing into.There is a blueprint node for that.

You need to change your graph so the ++ happens before the branch, too.

You have a few unnecessary and contradicting nodes going on in your graph. But luckily, what you’re trying to achieve is very straightforward. And you shouldn’t need to use a Branch node at all!

I recommend just simply using a Divide node to divide your score variable by 20, and then using a Floor node to round the result down to the nearest whole number. You’ll be left with an integer that represents how many multiples of 20 are present in your player’s score.

Written another way, that’s:

[Score variable] --> Divide by 20 --> Floor --> Update your coins variable

For example, if the player has a score of 57…

…57 divided by 20 equals 2.85, which floored equals 2. So with 57 points, the player has 2 coins.

If I’m mixing up coins and score, that’s fine — it doesn’t change the math at all. In that case, 57 coins means the player would have score of 2. Whichever you prefer.

I hope that’s clear without a Blueprint screenshot — I don’t have access to UE at the moment.

Anyway, best of luck!

Ah I got score and coins mixed up. Whoops

There’s no reason to do anything of the sort the_batch is suggesting, mightyenigma is right, it’s the most basic modulo operation:

2 Likes

Ah! I see now that the goal was to update the score as coins are collected. Before, I thought the original poster was asking a question like, “Given n coins, how do I calculate what the score is?”

That’s where my suggestion came from. In pseudocode, the answer would be:

score = floor( coins / 20 )

Anyway, given the original poster’s intent, your suggestion is definitely better.

Thanks at everybody. Anyway i followed the example of mightyenigma and it works. Thanks a lot!!!