Skip to content

Conversation

A-lex-Ra
Copy link
Contributor

@A-lex-Ra A-lex-Ra commented Jul 30, 2025

Usage example:

-- world.lua
local tt = require("core:tick_timer")
local should_run_timer_tick = false

function on_world_open()
    if not tt.b_acquired_by_some_script then
        tt.b_acquired_by_some_script = true
        should_run_timer_tick = true
    end
    tt.interval(5, "inf", function() print("5t-callback called!!!") end)
end

function on_world_quit()
    tt.destroy_all()
    tt.b_acquired_by_some_script = false
end

function on_world_tick()
    if should_run_timer_tick then
        tt.tick()
    end
end

It turned out that on_world_tick hardly works in modules (It only works if the module is exported to world.lua that does not yet have on_world_tick.), so users (for now) will have to call Timer.tick() manually as in the example, and also ensure that Timer.tick() is called exactly once per tick (using the global flag Timer.b_acquired_by_some_script).

@Volcraft311
Copy link

Ворованный скрипт, переработанный с помощью нейросети.

@A-lex-Ra
Copy link
Contributor Author

It's not stealing if there's almost nothing left of the original.

print("Timer error (" .. timer.name .. "): " .. err)
-- Optionally log the error to a file or system logger here
-- Optionally re-raise the error if you want to stop execution
-- error("Timer callback failed: " .. err)
Copy link
Owner

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

?

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

i can't decide what to do when callback throws error

@MihailRis
Copy link
Owner

In any case, the complete misunderstanding of the engine events system, metamethods without metatables (is it even tested?), random indentation in different functions (like lazy copy-pasted), useless AI-generated comments completely undermines the credibility of the code work without Lua autotests (dev/tests)

@A-lex-Ra
Copy link
Contributor Author

A-lex-Ra commented Jul 30, 2025

In any case, the complete misunderstanding of the engine events system, metamethods without metatables (is it even tested?), random indentation in different functions (like lazy copy-pasted), useless AI-generated comments completely undermines the credibility of the code work without Lua autotests (dev/tests)

it's mine dumb comments ;( sorry for that. But that's what code review is for, right?
Everything will be fixed.
Metamethods without metatables don't affect anything.

Copy link
Owner

@MihailRis MihailRis left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

It's better to leave only PriorityQueue for now, and I'll create a PR for the correctly integrated scheduler today

@A-lex-Ra
Copy link
Contributor Author

A-lex-Ra commented Aug 2, 2025

Ok. I recommend using update_front_priority to efficiently change the priority of the smallest element in the queue. And it might be worth creating a separate list for timers that are called every tick, as the queue isn't very efficient in this case.

@A-lex-Ra
Copy link
Contributor Author

A-lex-Ra commented Aug 2, 2025

And also item must have an "id" field.

@A-lex-Ra A-lex-Ra changed the title add tick_timer (sheduler) & priority_queue modules add priority_queue module Aug 2, 2025
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

Successfully merging this pull request may close these issues.

3 participants