Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

[hump.timer] Subject values are not precise to targets after timer has completed #124

Closed
htv04 opened this issue Apr 8, 2021 · 6 comments

Comments

@htv04
Copy link

htv04 commented Apr 8, 2021

I don't know if this is the proper way to check if a timer is in progress, but I often check whether a value of a subject is set to a specific value to see whether a timer involving the subject's value is in progress. For example:

if fade[1] == 1 and key == "enter" then -- If fade isn't equal to 1, pressing enter won't do anything
    Timer.tween(5, fade, {0}, "linear")
elseif fade[1] == 0 and key == "enter" then -- If fade isn't equal to 0, pressing enter won't do anything
    Timer.tween(5, fade, {1}, "linear")
end

Problem is, since Lua's numbers use double-precision, sometimes fade[1] is not exactly equal to 1 or 0 after the Timer.tween operations have completed. Therefore, in order for the above example to work properly, I would need to add math.floor to each instance of fade[1]:

if math.floor(fade[1]) == 1 and key == "enter" then -- If fade isn't equal to 1, pressing enter won't do anything
    Timer.tween(5, fade, {0}, "linear")
elseif math.floor(fade[1]) == 0 and key == "enter" then -- If fade isn't equal to 0, pressing enter won't do anything
    Timer.tween(5, fade, {1}, "linear")
end

This shouldn't be needed; the final values of the subject should match those of the targets.

@usysrc
Copy link
Contributor

usysrc commented Apr 20, 2021

You could use the after callback of the tween function.

https://hump.readthedocs.io/en/latest/timer.html#Timer.tween

Example:

local Timer = require "hump.timer"

local fade = {
    0
}

love.update = function(dt)
    Timer.update(dt)
end

love.draw = function()
    love.graphics.setColor(255,255,255,fade[1])
    love.graphics.rectangle("fill", 200, 200, 200, 200)
end

local fading = false

love.keypressed = function(key)
    if not fading and key == "space" then
        fading = true
        if fade[1] == 0 then
            Timer.tween(5, fade, {1}, "linear", function()
                fading = false
            end)
        else
            Timer.tween(5, fade, {0}, "linear", function()
                fading = false
            end)
        end
    end
end

@htv04
Copy link
Author

htv04 commented Apr 20, 2021

@usysrc This is a good workaround, but the precision issue still exists.

@usysrc
Copy link
Contributor

usysrc commented Apr 20, 2021

there is no precision issue, the value is correct inside the after function

@htv04
Copy link
Author

htv04 commented Apr 20, 2021

There is, because of the way floating point values work. Otherwise, the example I showed in my initial post would always work, without the need for math.floor.

One way to solve this issue might be to set the subject to the target value after the timer is complete. While this would be a hacky way of fixing the issue, it would work.

Actually, now that I think about it, this could cause unexpected results in some cases (such as if the subject were changed at all during the timer), so never mind.

@usysrc
Copy link
Contributor

usysrc commented Apr 20, 2021

These are floating point numbers. It is just the way it is. But if you need the target value you can get it inside the after function.

Timer.tween(5, fade, {1}, "linear", function() print(fade[1], fade[1] == 1) end)

@htv04
Copy link
Author

htv04 commented Apr 30, 2021

I see. I guess this can't be worked around, so I'll close this issue.

@htv04 htv04 closed this as completed Apr 30, 2021
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

No branches or pull requests

2 participants