I'm having trouble with a mod that utilizes minetest.after heavily.
Basically what I want to happen is this. A function is called through the use of minetest.after which does something and then uses minetest.after to call itself again sometime in the future. It will then continue to do something and call itself sometime in the future indefinitely.
Here is a very simple example.
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 timerfunction
timerfunction = function()
minetest.log("action", "timerfunction called, calling again in 5 seconds")
minetest.after(5, timerfunction)
end
minetest.log("action", "Calling timerfunction in 5 seconds")
minetest.after(5, timerfunction)
This code calls timerfunction 5 seconds in the future through the use of minetest.after. timerfunction then logs a message, and uses minetest.after to call itself 5 seconds in the future. This cycle goes on indefinitely.
The problem is, all calls to timerfunction through minetest.after besides the first one seem to be executed after 10 seconds rather than 5 seconds. Here is the output from minetest with this mod running.
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
2015-09-08 17:32:59: ACTION[main]: Calling timerfunction in 5 seconds
2015-09-08 17:32:59: ACTION[main]: .__ __ __
2015-09-08 17:32:59: ACTION[main]: _____ |__| ____ _____/ |_ ____ _______/ |_
2015-09-08 17:32:59: ACTION[main]: / \| |/ \_/ __ \ __\/ __ \ / ___/\ __\
2015-09-08 17:32:59: ACTION[main]: | Y Y \ | | \ ___/| | \ ___/ \___ \ | |
2015-09-08 17:32:59: ACTION[main]: |__|_| /__|___| /\___ >__| \___ >____ > |__|
2015-09-08 17:32:59: ACTION[main]: \/ \/ \/ \/ \/
2015-09-08 17:32:59: ACTION[main]: World at [/home/rhys/Desktop/minetest-0.4.13/bin/../worlds/asdf]
2015-09-08 17:32:59: ACTION[main]: Server for gameid="minetest" listening on 0.0.0.0:54258.
2015-09-08 17:32:59: ACTION[ServerThread]: singleplayer [127.0.0.1] joins game.
2015-09-08 17:32:59: ACTION[ServerThread]: singleplayer joins game. List of players: singleplayer
2015-09-08 17:33:00: ACTION[main]: Irrlicht: Could not open file of texture: character.png
2015-09-08 17:33:00: ACTION[main]: Irrlicht: Could not open file of texture: character.png
2015-09-08 17:33:04: ACTION[ServerThread]: timerfunction called, calling again in 5 seconds
2015-09-08 17:33:14: ACTION[ServerThread]: timerfunction called, calling again in 5 seconds
2015-09-08 17:33:24: ACTION[ServerThread]: timerfunction called, calling again in 5 seconds
2015-09-08 17:33:34: ACTION[ServerThread]: timerfunction called, calling again in 5 seconds
2015-09-08 17:33:44: ACTION[ServerThread]: timerfunction called, calling again in 5 seconds
2015-09-08 17:33:54: ACTION[ServerThread]: timerfunction called, calling again in 5 seconds
Notice how the first log message "Calling timerfunction in 5 seconds" appears at 17:32:59 and then the message "timerfunction called, calling again in 5 seconds" is logged at 17:33:04. These two messages are separated by 5 seconds, which makes sense, given that the call to minetest.after specified that timerfunction was to be called 5 seconds in the future.
What I don't understand however, is why all the remaining log messages are separated by 10 seconds. This presents a large problem in my mod, as the correct timing of actions is very important.
I'm pretty stumped here. Can anybody tell me why this is happening? Many thanks in advance.