Counting in-game money

pHghostpHghost London, UKMember Posts: 2,342
edited November 2013 in Working with GS (Mac)
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.

Comments

  • CodeMonsterCodeMonster ACT, AustraliaMember Posts: 1,078
    why change 'game.coinCount' to game.countcount +0,01?? why not change it to just +1 thats what i always do, if you do this your problem should be fine. so eg:
    if coin collide with actor
    change attribute game.coincount to game.coincount+1
    destroy coin
  • tatiangtatiang Member, Sous Chef, PRO, Senior Sous-Chef Posts: 11,949
    edited November 2013
    The only thing I can imagine is that you're getting a glitch because you're trying to change an attribute AFTER destroying the actor. Move the destroy actor below and see if that fixes it. I've certainly seen this issue with interpolate but not with change attribute.

    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

  • pHghostpHghost London, UKMember Posts: 2,342
    edited November 2013
    Thanks for the suggestions, I'll try it out and see what it does.

    @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.
  • tatiangtatiang Member, Sous Chef, PRO, Senior Sous-Chef Posts: 11,949
    @pHghost Can you send me a link to download your file? I'd like to take a look since I can't seem to recreate the issue.

    New to GameSalad? (FAQs)   |   Tutorials   |   Templates   |   Greenleaf Games   |   Educator & Certified GameSalad User

  • pHghostpHghost London, UKMember Posts: 2,342
    change attribute: 'game.coinCount' to: prec(game.coinCount, 2)+0.01

    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
  • tatiangtatiang Member, Sous Chef, PRO, Senior Sous-Chef Posts: 11,949
    Thanks for the file. I see what you mean. I think you should submit it to GameSalad as a bug since your rules look exactly right and there's no good reason for that glitch to happen.

    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

  • pHghostpHghost London, UKMember Posts: 2,342
    edited November 2013
    How do I submit to GameSalad? I'll probably do that.

    Thanks.

    UPDATE: 'Feedback' button in Creator, got it. ;)
  • pHghostpHghost London, UKMember Posts: 2,342
    In case someone runs into a similar problem, this is a reply from the customer support team -- the issue seems to be related to calculations with 0.1 increments, as I understand 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.
Sign In or Register to comment.