Need help with making a dynamic timer
I'm making an idle game similar to Cookie Clicker and I've run into a problem with my timer.
How the game works:
- You click on an object to increase your score.
- Once your score is high enough, you can buy an upgrade.
- The upgrade then adds to your score so you don't have to click as much.
The way the upgrade works is as follows:
- I have 4 variables on the upgrade actor: cost (integer), quantity (integer), rate (real), and bonus (integer).
- The cost is how much score the upgrade needs to be purchased and is increased by 33% every time you purchase the upgrade. The cost is then deducted from the score.
- The quantity is increased by one every time you purchase the upgrade (duh).
- The rate is how long, in seconds, it takes for the upgrade to add to the score.
- The bonus is how much the upgrade adds to your score.
The way I have my timer set up is as follows (I'm going to pseudo-code this):
If self.quantity > 0
Every ( self.rate / self.quantity ) seconds
game.score + self.bonus
What I'm trying to do is have a dynamic timer so that the score updates in "real time" instead of a fixed interval.
So the first upgrade adds 1 to the score every 10 seconds. When you have two of the first upgrade, you would be getting plus 2 score per 10 seconds, or 1 score per 5 seconds. So I figured by doing the simple equation of (rate/quantity), in this case the rate is 10 seconds and the quantity let's say is 2, we should have every 5 seconds do game.score + self.bonus. This way the score doesn't add 2 every 10 seconds, but adds 1 every 5 seconds. If we had 3 for the quantity, 1 would be added to the score every 3.333- seconds, and so on.
Hopefully you can see what I mean about a dynamic timer.
I've tried setting a base time of .1 seconds and setting the bonus of the upgrade to .01 so that the score updates every tenth of a second and mathematically it still should add 1 every 10 seconds, but that doesn't want to work.
So to sum this up for those thinking TL;DR
I want a dynamic timer that adjusts the timer rate to update my score in real time instead of in fixed increments. Is this even possible or am I just overlooking something?
Thanks!
Comments
My head hurts maybe this thread might help you http://forums.gamesalad.com/discussion/44707/timers-are-for-chumps-gs-optimization-tips/p1
Thanks for the quick response. I read through the entire post and, although very insightful, didn't quite do what I need.
I think the only reason my timer isn't working is because the timer isn't updating with the new attribute value.
Perhaps this will help?
http://forums.gamesalad.com/discussion/comment/490049/#Comment_490049
You can also force a timer to "update" by wrapping it in a rule that's on/off based on a boolean, and change the boolean when you want the timer to be updated.
i.e.
And when you update the timer, just change game.Reset to true.
That did the trick. Thank you very much!