Scripting Helpers is winding down operations and is now read-only. More info→
Ad
Log in to vote
0

Values + Algorithms + Timing?

Asked by 10 years ago

So Basically , On my cookie clicker like game, I can buy an upgrade which automatically gives you cookies, but I want the cookies to go up appropriate to the amount of cookies/second . So when I buy it , I get 1 cookie a second , but I don't want this :

while wait(1) do
     integer.Value = integer.Value + auto.Value -- auto.Value being the amount per second.
end

so for example , if I have 2 cookies per second, it adds 1 every 0.5 seconds, and if I have 4 cookies/second, then it adds 1 every 0.25 a second.

My Problem ( and what I have got so far)

tim = auto.Value/1

while wait(tim) do
     integer.Value = integer.Value + 1
end

the problem is that 'tim' is not updating, so when I get the upgrade, it adds 20 individually , and not 1

If you understood my explanation then well done :)) And thanks

1 answer

Log in to vote
1
Answered by
BlueTaslem 18071 Moderation Voter Administrator Community Moderator Super Administrator
10 years ago

tim won't update on its own -- you only told it to be set at the beginning, so that's all it's doing.

You could update it in the loop:

while true do
    tim = 1 / auto.Value
    wait(tim)
    integer.Value = integer.Value + 1
end

which is of course equivalent to

while wait(1 / auto.Value) do
    integer.Value = integer.Value + 1
end

You did have written auto.Value / 1. You almost surely meant 1 / auto.Value (just be careful that auto.Value isn't ever 0).


You'll also have to be careful that auto.Value doesn't get bigger than 30, because wait will always wait for at least 1/30th of a second.


To combat all these issues, it's probably better to do something like this:

cache = 0
while true do
    increase = wait() * auto.Value
    -- wait() returns number of seconds waited
    -- auto.Value is number of cookies / second
    -- (delta seconds) * (cookies / second) = delta cookies
    cache = cache + increase -- Increase by fractional cookies
    floor = math.floor(cache) -- Get the whole number of cookies made so far
    integer.Value = integer.Value + floor -- As soon as possible, move
    cache = cache - floor -- cookies FROM cache TO `integer`
end

We work as quickly as possible, keeping track of "fractional" cookies (according to the rate) and whenever one whole "finishes", we give it to the player -- but we're allowed to give more than one at any moment in case the rate gets much larger.

0
Wow thanks, You cant learn this stuff from the wiki :) Bubbles5610 217 — 10y
Ad

Answer this question