Timekeeper

Timekeeper acts as a centralized dispatcher for all time-sensitive routines

API / Library

The Timekeeper class provides a simple and efficient means of executing code at regular intervals. The constructor itself is global, so typically it will be used in conjunction with entities.

on_activate(self)
        self.timekeeper = Timekeeper(self)
        :
end

The following methods are available:

Four parameters are provided to the timer callback for each iteration:

In order for the timers to be processed correctly, you must call the on_step() method of the timekeeper object during every server step. For example, in the case of entities:

on_step = function (dtime)
        local timers = self.timekeeper.on_step(dtime)
        :
end,

With a globalstep callback, it is similar albeit the timekeeper object will likely be defined at the head of the script.

local timekeeper = Timekeeper { }
:
minetest.register_globalstep(function(dtime)
        local timers = timekeeper.on_step(dtime)
end)

Notice that the on_step method of the timekeeper object returns a table of timers. This can be useful for processing one or more timer events directly within the globalstep callback.

For convenience, a global timekeeper object is available for use by all mods. This can avoid the need to register additional globalsteps when a simple timer mechanism is all that is required. In this case, it is recommened that timer names be prefixed with the mod name and a colon to avoid collisions.

Here is an example mod that displays the player's orientation at the bottom of the screen.

local player_huds = { }

local dir_names = {
    ["N"] = "north",
    ["NE"] = "northeast",
    ["E"] = "east",
    ["SE"] = "southeast",
    ["S"] = "south",
    ["SW"] = "southwest",
    ["W"] = "west",
    ["NW"] = "northwest",
    ["U"] = "up",
    ["D"] = "down",
}

minetest.register_on_joinplayer( function( player )
        local player_name = player:get_player_name( )

        player_huds[ player_name ] = player:hud_add( {
                hud_elem_type = "text",
                text = "",
                position = { x = 0.5, y = 1 },
                scale = { x = -100, y = -100 },
                number = 0xFFFFFF,
                alignment = { x = 0, y = 0 },
                offset = { x = 0, y = -105 }
        } )
end )

minetest.register_on_leaveplayer( function( player )
    player_huds[ player:get_player_name( ) ] = nil
end )

local function to_facing( dir )
    local dx = math.floor( dir.x + 0.5 )
    local dz = math.floor( dir.z + 0.5 )

    if dx == 0 and dz == 0 then
        return dir.y > 0 and "U" or "D"
    else
        return ( { [1] = "N", [0] = "", [-1] = "S" } )[ dz ] .. ( { [1] = "E", [0] = "", [-1] = "W" } )[ dx ]
    end
end

globaltimer.start( 2.5, "sample_mod:update_hud", function ( this, cycles )
    for _, player in ipairs( minetest.get_connected_players( ) ) do
        local cur_dir = to_facing( player:get_look_dir( ) )
        local player_name = player:get_player_name( )
        player:hud_change( player_huds[ player_name ], "text", string.format( "You are facing %s", dir_names[ cur_dir ] ) )
    end
end )

Ratings and Reviews

Review

Do you recommend this mod?