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

1 ACCEPTED SOLUTION

Accepted Solutions

Hello Eric,

 

Thank you for the reply back. I tried the formula below but it is erroring out. It says there is an "expected operator error. Saying that something is missing to connect the pieces but I am not seeing the error in the formula. The entire Set(...) is underlined in red. I will attach a screenshot. I appreciate your help and I like your idea of the variable piece. I tried to reorganize the calculation part thinking maybe PA did not like the - sign, but i ran a test calculation and that isn't an issue. I tried cutting out the 2nd variable piece, Health, to see if it was that but still got the red line of shame.

 

Set(HealthChange, 35 * MonsterA_Def1_damage_counter - 10 * MonsterA_Atk1_damage_counter - 25 * MonsterA_Atk2_damage_counter);


Set(Health, If(100 - HealthChange >= 100, 100,100 - HealthChange))

View solution in original post

11 REPLIES 11
Super User
Super User

For debugging, I'd recommend moving the formula you use to calculate the change to a new variable, HealthChange in this example. Then it's easier to see what's going on.

Set(HealthChange, 10*Atk1_damage_counter - 25*Atk2_damage_counter + 35 * heatlth_counter);
Set(Health, If(100 - HealthChange >= 100, 100, 100 - HealthChange))

---
If this answered your question, please click "Accept Solution". If this helped, please Thumbs Up. 

Hello Eric,

 

Thank you for the reply back. I tried the formula below but it is erroring out. It says there is an "expected operator error. Saying that something is missing to connect the pieces but I am not seeing the error in the formula. The entire Set(...) is underlined in red. I will attach a screenshot. I appreciate your help and I like your idea of the variable piece. I tried to reorganize the calculation part thinking maybe PA did not like the - sign, but i ran a test calculation and that isn't an issue. I tried cutting out the 2nd variable piece, Health, to see if it was that but still got the red line of shame.

 

Set(HealthChange, 35 * MonsterA_Def1_damage_counter - 10 * MonsterA_Atk1_damage_counter - 25 * MonsterA_Atk2_damage_counter);


Set(Health, If(100 - HealthChange >= 100, 100,100 - HealthChange))

View solution in original post

That's odd. Here is the test code I'm using and it all seems to be working.

Set(Health,100);
Set(MonsterA_Def1_damage_counter, .45);
Set(MonsterA_Atk1_damage_counter, .35);
Set(MonsterA_Atk2_damage_counter, .25);
Set(HealthChange, 35 * MonsterA_Def1_damage_counter - 10 * MonsterA_Atk1_damage_counter - 25 * MonsterA_Atk2_damage_counter);
Set(Health, If(100 - HealthChange >= 100, 100,100 - HealthChange))

 What does it say when you hover your mouse over the bold red lines under HealthChange?

You will want to leverage the Min and Max functions to accomplish this.

 

For example, I have buttons and their On Select functions

  • I have a button which will increment a variable, by 20
    • UpdateContext({locCounter: Min(locCounter + 20,100)}) - recommended to use local variable
    • Set(gblCounter, Min(gblCounter + 20,100))
  • I have a button which will decrement a variable, by 15
    • UpdateContext({locCounter: Max(locCounter -15,0)}) - recommended to use local variable
    • Set(gblCounter, Max(gblCounter -15,0))

 

The Min function returns the smallest of the included values. This would limit the variable from exceeding 100.

The Max function returns the largest of the included values. This would limit the variable from going below 0.

 

You will just need to update your local, or global, variable accordingly.

It's weird, I type in just the first variable: 

Set( Health,100) and it gives the error red underline. 

Error text is "Behavior function in a non-behavior property. You can't use this property to change values elsewhere in the app."

I checked the app to see if I had "Health" named anywhere in the app and I do not. I even tried "cows" and still the error message. Everything you have seems right and The variable setup is right, just not sure why it hates me. 😞

You appear to have the Text property of the control selected. 🙂

Set is a function, and needs to be invoked by an OnSelect, or OrChange, etc.

Ok, did not know this and I think I may have complicated the issue. I had the variable trying to operate inside a text box. The variables do not work there. When I put it in a button, the variable worked. So now I need to get the updated Health Points to stay updated based on what worked the last turn. I am attaching a screenshot of what my screen looks like if that helps. I am hoping the steps from before will work in the buttons.

 

I appreciate the help. 

To track things, in order, you'll need the collection to show the records (which you have).

 

For the buttons at the top

  • OnSelect functions to increment the variable as needed)
  • Apply the same logic noted above about Min/Max to keep the variable from going out of range

For the counters (squares) are the bottom

  • Text values, which show the current values
  • CountIf( Collection, Success = True)

For the running HP

  • Text value, showing the variable being manipulated by the buttons at the top

 

You will need to account for scenarios where the variable gets updated to 0 (i.e. Death), to prompt the app to reset the collection and variables as needed.

@GarethPrisk 

 

Can you put an If statement in the UpdateContext piece? I use a random number to determine if the attack is successful and if the counter changes. So, I have an 80% chance that the first attack will work. At the moment, I have a Random number that is created. If the random number is 0.80 or lower, I have a statement that says "Success" or if over 0.80, its a miss and my counter does not increase. 

 

So, the question is, with the formula you provided earlier, can an if statement work in the UpdateContext to account for a hit/miss?

 

Thank you again!

Helpful resources

Announcements
New Badges

New Solution Badges!

Check out our new profile badges recognizing authored solutions!

New Power Super Users

Congratulations!

We are excited to announce the Power Apps Super Users!

Power Apps Community Call

Power Apps Community Call: February

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

Microsoft Ignite

Microsoft Ignite

Join digitally, March 2–4, 2021 to explore new tech that's ready to implement. Experience the keynote in mixed reality through AltspaceVR!

Top Solution Authors
Top Kudoed Authors
Users online (40,251)