cancel
Showing results for 
Search instead for 
Did you mean: 
Reply
Frequent Visitor

Calculate max value to not fo over certain number

Hello,

 

I am working on a game idea and using PowerApps that goes into a collection. That may not be the best way but it has been working so far. I have a set value that can decrease through each turn, but I don't want it to exceed 100 or go below 0.

 

Example: 

1st entry: Start at 100, lose 20 points so the new total is 80.

2nd entry: Now at 80, nothing happened so stays there.

3rd entry: Still at 80, but lost 20 points, so now at 60.

4th entry: Now at 60, but gained 30. no problems so far.

5th entry: Now at 90, but gained 30. BUT, I don't want the total to be 120 but the max number at 100.

 

Here is the code I was trying to use:

If((100 - 10*Atk1_damage_counter - 25*Atk2_damage_counter + 35 * heatlth_counter) >100,
100,
100 - 10*Atk1_damage_counter - 25*Atk2_damage_counter + 35 * health_counter)

 

Hopefully this makes sense. 

cwsmith43

11 REPLIES 11

UpdateContext({locTurnRandom: Rand()});
UpdateContext(
    {
        locTurnOutcome: If(
            locTurnRandom <= 0.8,
            "Success",
            "Miss"
        ),
        locCurrentHP: If(
            locTurnRandom <= 0.8,
            Max(
                0,
                locCurrentHP - 20
            )
        )
    }
)

Check out the code above. You can definitely use a Rand() variable as an IF evaluation. The catch is that you need to keep updating the Rand() variable, otherwise it will remain static. This example sets the Rand() variable first, then uses that updated variable in the subsequent update to either result in a success/decrement in HP, or a Miss and no change to HP.

UpdateContext({locTurnRandom: Rand()});
UpdateContext({locTurnSuccess: locTurnRandom <= 0.8});
UpdateContext(
    {
        locTurnOutcome: If(
            locTurnSuccess,
            "Success",
            "Miss"
        ),
        locCurrentHP: If(
            locTurnSuccess,
            Max(
                0,
                locCurrentHP - 20
            ),
            locCurrentHP
        )
    }
)

A few improvements.

  • Deferred the success outcome to its own variable - allows you to simply update the threshold once, and converts it to a Boolean output, which makes IF statements simpler to read
  • Added the locCurrentHP with an else value, otherwise it will null the value

Helpful resources

Announcements
secondImage

Experience what’s next for Power Apps

Join us for an in-depth look at the new Power Apps features and capabilities at the free Microsoft Business Applications Launch Event.

Power Apps Community Call

Power Apps Community Call: February

Did you miss the call? Check out the Power Apps Community Call here.

New Power Super Users

Congratulations!

We are excited to announce the Power Apps Super Users!

New Badges

New Solution Badges!

Check out our new profile badges recognizing authored solutions!

Top Kudoed Authors
Users online (36,333)