From d9618abbdf034ca0c2a1ec8fffb42dd66ec6fe1a Mon Sep 17 00:00:00 2001 From: SkyratBot <59378654+SkyratBot@users.noreply.github.com> Date: Wed, 3 Apr 2024 02:05:53 +0200 Subject: [PATCH] [MIRROR] Fixes lua error logging and a few timer.lua functions (#26999) Fixes lua error logging and a few timer.lua functions (#82160) Lua errors don't get logged when `call_function` is called Timer.start_loop was just straight up broken due to me not properly testing it, so this fixes that. Makes debugging lua scripts easier. Also fixes bugs. :cl: fix: Fixed lua error logging. fix: Fixed the SS13.start_loop function not working properly. /:cl: --------- Co-authored-by: Watermelon914 <37270891+Watermelon914@users.noreply.github.com> Co-authored-by: Watermelon914 <3052169-Watermelon914@users.noreply.gitlab.com> Co-authored-by: Useroth <37159550+Useroth@users.noreply.github.com> --- code/modules/admin/verbs/lua/lua_state.dm | 6 +++-- lua/timer.lua | 31 +++++++++++++---------- 2 files changed, 21 insertions(+), 16 deletions(-) diff --git a/code/modules/admin/verbs/lua/lua_state.dm b/code/modules/admin/verbs/lua/lua_state.dm index 415d9f6971a..ee7d6953e12 100644 --- a/code/modules/admin/verbs/lua/lua_state.dm +++ b/code/modules/admin/verbs/lua/lua_state.dm @@ -97,9 +97,11 @@ GLOBAL_PROTECT(lua_usr) /datum/lua_state/process(seconds_per_tick) if(timer_enabled) - call_function("__Timer_timer_process", seconds_per_tick) + var/result = call_function("__Timer_timer_process", seconds_per_tick) + log_result(result, verbose = FALSE) for(var/function as anything in functions_to_execute) - call_function(list("__Timer_callbacks", function)) + result = call_function(list("__Timer_callbacks", function)) + log_result(result, verbose = FALSE) functions_to_execute.Cut() /datum/lua_state/proc/call_function(function, ...) diff --git a/lua/timer.lua b/lua/timer.lua index e7829d28514..605e5b98a2e 100644 --- a/lua/timer.lua +++ b/lua/timer.lua @@ -24,11 +24,13 @@ end function __stop_internal_timer(func) local timer = __Timer_timers[func] - if timer and not timer.executing then - __Timer_timers[func] = nil - __Timer_callbacks[func] = nil - else - timer.terminate = true + if timer then + if not timer.executing then + __Timer_timers[func] = nil + __Timer_callbacks[func] = nil + else + timer.terminate = true + end end end @@ -36,7 +38,7 @@ __Timer_timer_processing = __Timer_timer_processing or false SS13.state:set_var("timer_enabled", 1) __Timer_timer_process = function(seconds_per_tick) if __Timer_timer_processing then - return + return 0 end __Timer_timer_processing = true local time = dm.world:get_var("time") @@ -53,6 +55,7 @@ __Timer_timer_process = function(seconds_per_tick) end end __Timer_timer_processing = false + return 1 end function Timer.wait(time) @@ -77,18 +80,18 @@ function Timer.start_loop(time, amount, func) if amount == 1 then return __add_internal_timer(func, time * 10, false) end - local callback = SS13.new("/datum/callback", SS13.state, "call_function") - local timedevent = dm.global_proc("_addtimer", callback, time * 10, 40, nil, debug.info(1, "sl")) - local doneAmount = 0 + -- Lua counts from 1 so let's keep consistent with that + local doneAmount = 1 + local funcId local newFunc = function() - func() + func(doneAmount) doneAmount += 1 - if doneAmount >= amount then - Timer.end_loop(timedevent) + if doneAmount > amount then + Timer.end_loop(funcId) end end - __add_internal_timer(newFunc, time * 10, true) - return newFunc + funcId = __add_internal_timer(newFunc, time * 10, true) + return funcId end function Timer.end_loop(id)