precision timer

User avatar
Echo
Member
 
Posts: 121
Joined: Tue Jul 31, 2012 08:11

precision timer

by Echo » Sun Sep 09, 2012 11:38

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.
 

User avatar
PilzAdam
Member
 
Posts: 4026
Joined: Fri Jul 20, 2012 16:19
GitHub: PilzAdam
IRC: PilzAdam

by PilzAdam » Sun Sep 09, 2012 12:12

https://github.com/celeron55/minetest/blob/master/doc/lua_api.txt#L935
The time param can be also 0.1 or something else.
 

User avatar
Echo
Member
 
Posts: 121
Joined: Tue Jul 31, 2012 08:11

by Echo » Sun Sep 09, 2012 13:27

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.
 

User avatar
PilzAdam
Member
 
Posts: 4026
Joined: Fri Jul 20, 2012 16:19
GitHub: PilzAdam
IRC: PilzAdam

by PilzAdam » Sun Sep 09, 2012 13:54

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()
 

User avatar
Topywo
Member
 
Posts: 1718
Joined: Fri May 18, 2012 20:27

by Topywo » Sun Sep 09, 2012 14:09

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

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

User avatar
Echo
Member
 
Posts: 121
Joined: Tue Jul 31, 2012 08:11

by Echo » Sun Sep 09, 2012 20:12

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?
 


Return to Minetest Features

Who is online

Users browsing this forum: No registered users and 4 guests

cron