Playtime

by thuejk

Adds a clock to the top left corner, saying how long the current game has been running.

Utilities
4 days ago
0.15 - 2.1
26.3K

b Desync on 2nd connect

3 days ago

When hosting a dedicated multiplayer server, the 2nd time a client attempts to connect results in a desync. Tracked down to this mod causing it. Desync tagged tick heuristics shows the first byte in the <custom-gui> tag differs between server and client (FE on server, FF on client).

3 days ago

Hmm, I can't reproduce. But perhaps that is just normal in the wonderful world of desyncs.

I am not sure what the problem is, but I am no expert (it is a very simple mod). But ChatGPT suggested the code below, that the problem was iterating over connecting players - if you feel like making the effort, could you replace the contents of control.lua, and see if it helps?


local mod_gui = require("mod-gui")

local playtime = {
play_time_seconds = -1
}

local function ensure_gui(player)
local flow = mod_gui.get_button_flow(player)

local button = flow.clockGUI
if button and button.valid then
    return button
end

return flow.add{
    type = "button",
    name = "clockGUI",
    style = mod_gui.button_style,
    caption = ""
}

end

local function update_button(player)
local button = ensure_gui(player)

local seconds = playtime.play_time_seconds % 60
local minutes = math.floor(playtime.play_time_seconds / 60) % 60
local hours   = math.floor(playtime.play_time_seconds / 3600)

if hours > 0 then
    button.caption = string.format(
        "Total time: %d:%02d:%02d",
        hours,
        minutes,
        seconds
    )
else
    button.caption = string.format(
        "Total time: %02d:%02d",
        minutes,
        seconds
    )
end

end

script.on_init(function()
for _, player in pairs(game.players) do
ensure_gui(player)
end
end)

script.on_configuration_changed(function()
for _, player in pairs(game.players) do
ensure_gui(player)
end
end)

script.on_event(defines.events.on_player_created, function(event)
local player = game.get_player(event.player_index)
if player then
ensure_gui(player)
end
end)

script.on_event(defines.events.on_player_joined_game, function(event)
local player = game.get_player(event.player_index)
if player then
ensure_gui(player)
update_button(player)
end
end)

script.on_event(defines.events.on_tick, function(event)
local previous = playtime.play_time_seconds
playtime.play_time_seconds = math.floor(event.tick / 60)

if previous == playtime.play_time_seconds then
    return
end

for _, player in pairs(game.connected_players) do
    update_button(player)
end

end)

New response