How do I display percentage of games the player has won?
Greetings!
I’m fairly new to GS and am enjoying it!
Question:
I want to display a stat to the player letting them know what percentage of games they’ve won.
I’ve set up a real game attribute that adds 1 each time they win the game. (Win)
I’ve set up a second real game attribute that adds 1 for each time they play the game (Total)
I’m trying to use a display text behavior to display the average the player has won.
I’ve done: (Win/Total) *100
This works, but only once the player has lost a game. At which, point it begins to correctly display the percentage of games won. Before that, when no games have been lost, it only displays “nan.”
I’m not sure why this is, as it should be displaying 100. (For example, if the player has played and won 5 games, the calculation would be (Win 5/Total 5) *100=100. It should display 100, but does not.
Any thoughts?
Thanks so much!
Best Answer
-
tatiang Posts: 11,949
Your equation is correct:
Win percentage = wins/total*100
But when the total is 0, the right side of the equation is 0/0*100 or 0/0. You can't divide by zero (how many times does 0 go into 72? Zero... nope, 72... nope... etc.) so most calculators show an error message such as "nan" (Not A Number).
Your Display Text rule should look like this instead:
When total>0
Display Text wins/total*100.."%"
Otherwise
Display Text 0.."%"I just noticed that you said it's not working UNTIL the player loses a game. Are you adding one to the total each time the player plays a game? After the first game, the wins should be 1 (or 0) and the total should be 1, so the equation should equal 1/1*100 (or 0/1*100), which would result in 100% (or 0%) being displayed.
New to GameSalad? (FAQs) | Tutorials | Templates | Greenleaf Games | Educator & Certified GameSalad User
Answers
Yes, of course! Thank you so much for this! Solved the problem!