Page 1 of 1

precision timer

PostPosted: Sun Sep 09, 2012 11:38
by Echo
I wondered if there is any precision timer useable in LUA.
os.time() gives only seconds
minetest.env:get_timeofday() gives always the same value within a function

Why?
I'm working on a complex algorithm but I don't want the game to lag. With a precision timer I could modify my algorithm to abort when a certain time is over, else it will increase its range. So the range of the function will be limited by the speed of the computer.
Other mods should be able to auto-adjust, too. I think about the MOB-framework (AKA animals-modpack). Depending on the elapsed time, it could decide which or how many mobs are calculated. So the warning "WARNING: you need a reasonable fast machine with enough memory for animals." could be removed.

PostPosted: Sun Sep 09, 2012 12:12
by PilzAdam
https://github.com/celeron55/minetest/blob/master/doc/lua_api.txt#L935
The time param can be also 0.1 or something else.

PostPosted: Sun Sep 09, 2012 13:27
by Echo
Not what I want.
minetest.after just calls a function after a period of time (in seconds, not milliseconds). But within the function you have no control how long it takes to compute.
I'd like to have a function like "microtime" in PHP, which gives you a precise time when you call it.
Or the function minetest.env:get_timeofday() should give the actual time, not a buffered one.

PostPosted: Sun Sep 09, 2012 13:54
by PilzAdam
You can use milliseconds in minetest.after(). In the function you can set a boolean variable to false and check it in the main code.
Another method would be to split up the main code into small functions and execute them in minetest.register_globalstep()

PostPosted: Sun Sep 09, 2012 14:09
by Topywo
There is a world time mod. Maybe some use for your idea?

http://minetest.net/forum/viewtopic.php?id=2205

PostPosted: Sun Sep 09, 2012 20:12
by Echo
PilzAdam wrote:You can use milliseconds in minetest.after(). In the function you can set a boolean variable to false and check it in the main code.

Nope, not working. Neither with globalstep:
Your phone or window isn't wide enough to display the code box. If it's a phone, try rotating it to landscape mode.
Code: Select all
local global_timestep = false
minetest.register_chatcommand("infinite_loop", {
    params = "",
    description = "Wait for a global variable",
    func = function(name, param)
        global_timestep = false
        local counter = 0
        repeat
            counter = counter + 1
            print("Waiting for external bool..."..counter)
        until global_timestep or counter == 1000
    end,
})

minetest.register_globalstep(function(dtime)
    if not global_timestep then
        global_timestep=true
        print("Set boolean true")
    end
end)

Nor with after:
Your phone or window isn't wide enough to display the code box. If it's a phone, try rotating it to landscape mode.
Code: Select all
local global_timestep = false
minetest.register_chatcommand("infinite_loop", {
    params = "",
    description = "Wait for a global variable",
    func = function(name, param)
        global_timestep = false
        local set_true = function()
            global_timestep = true
            print("Set boolean true")
        end
        minetest.after(0.01, set_true)
        local counter = 0
        repeat
            counter = counter + 1
            print("Waiting for external bool..."..counter)
        until global_timestep or counter == 1000
    end,
})

Once in a loop, no chance that any timer gets a turn. I added an additional counter, that when somebody wants to try this code doesn't has to kill the minetest-process.

PilzAdam wrote:Another method would be to split up the main code into small functions and execute them in minetest.register_globalstep()

Wow, make an iteration, save every value so the next iteration is able to load every value and proceed where exited before. That's a tough job just because there is no precise time-function.

Topywo wrote:There is a world time mod. Maybe some use for your idea?

http://minetest.net/forum/viewtopic.php?id=2205

Sorry, same issue here. No solution.

Is my wish so absurd? Am I the only one caring about mods getting so complex, that you have to warn the users to either buy high performance machines or better have no mobs/ambient sound/whatever?