Counting in-game money
pHghost
London, UKMember Posts: 2,342
I have coins in my scene, which the player needs to pick up in order to buy in-game items. I have come across a really strange bug. Right now I only have one cent (0.01) coins, so the amount rises 0.01, 0.02, ..., 0.56, ..., 3.85 etc.
Once the player collects enough coins, the count suddenly goes from 4.41 to 4.4299999999999, then continues 4.4399999999999 and so on.
Any idea why this is happening? Is there any way to "floor" numbers not to integers, but to real numbers with just two decimals?
This is the function for adding a coin:
when 'coin' overlaps with 'player':
destroy actor
change attribute: 'game.coinCount' to: 'game.coinCount'+0.01
The coin display is:
Display text: "money:$"..game.coinCount
UPDATE: I let the game run and the numbers snap back to 'normal' somewhere round 11 dollars.
Once the player collects enough coins, the count suddenly goes from 4.41 to 4.4299999999999, then continues 4.4399999999999 and so on.
Any idea why this is happening? Is there any way to "floor" numbers not to integers, but to real numbers with just two decimals?
This is the function for adding a coin:
when 'coin' overlaps with 'player':
destroy actor
change attribute: 'game.coinCount' to: 'game.coinCount'+0.01
The coin display is:
Display text: "money:$"..game.coinCount
UPDATE: I let the game run and the numbers snap back to 'normal' somewhere round 11 dollars.
Comments
if coin collide with actor
change attribute game.coincount to game.coincount+1
destroy coin
GSINVENTION FREE TEMPLATES
You can use prec(game.coinCount,2) to round to two decimals.
If you want to try @CodeMonster's suggestion, just divide game.coinCount by 100 when displaying it.
New to GameSalad? (FAQs) | Tutorials | Templates | Greenleaf Games | Educator & Certified GameSalad User
@CodeMonster
The reason I am adding 0.01 is because it is the equivalent of one cent, but the main currency is dollars.
UPDATE: Still happening when attribute is changed before destroy.
New to GameSalad? (FAQs) | Tutorials | Templates | Greenleaf Games | Educator & Certified GameSalad User
That solved the issue. Perfect. Here is a link to the file though, you migh find something lurking under the surface that needs attention:
https://dl.dropboxusercontent.com/u/715828/Testing it.zip
I simplified the file and added more and faster spawners so that you only have to wait a few seconds for the coinCount attribute to reach 4.4299999999999.
New to GameSalad? (FAQs) | Tutorials | Templates | Greenleaf Games | Educator & Certified GameSalad User
Thanks.
UPDATE: 'Feedback' button in Creator, got it.
The issue you are reporting is one that our Development team is aware of. Unfortunately, this is a demonstration of a limitation on how the engine (and display text) stores floating point calculations. The workaround you suggested: prec(game.coinCount, 2)+0.01 is definitely a valid way to keep your calculations limited to two decimal points.
Apologies for any inconvenience caused.