• There is NO official Otland's Discord server and NO official Otland's server list. The Otland's Staff does not manage any Discord server or server list. Moderators or administrator of any Discord server or server lists have NO connection to the Otland's Staff. Do not get scammed!
  • 2026 staff recruitment is open! Check it out and consider applying!

Lua Registering GlobalEvent onTime dynamically tfs 1.3

zuber966

Member
Joined
Apr 11, 2020
Messages
38
Solutions
2
Reaction score
14
Location
Rzeszów, Poland
Hi!
I'm trying to register global event dynamically inside function. My code looks like this:
LUA:
function registerEvent(plusMinutes)

    local someEventCreatedDynamically = GlobalEvent("event_name")

    function someEventCreatedDynamically.onTime(interval)
        do some stuff on time
    end

    local clocknum = os.date("!%X",number):split(":") -- h:m:s
    local startTime = tostring(clocknum[1]) .. ":" .. tostring(clocknum[2] + plusMinutes):split(".")[1]

    someEventCreatedDynamically:time(startTime)
    someEventCreatedDynamically:register()
end

Even hardcoding time does nothing. Console is clear. Is it possible what I'm trying to accomplish?
 
Last edited:
Solution
Code:
local g = GlobalEvent("yourEventName")
g:type("time")
g:time("21:00:00")
g:onTime(yourFunctionName)
g:register()

This is the syntax I believe, though I'm not sure if you can split the creation to two parts. It's better to just pass necessary parameters to addEvent and create the event there.

edit: for clarification: if you have function executeSomething() some code end, your onTime will look like this: g:onTime(executeSomething)
did you call the function?

registerEvent(plusMinutes) (below last end)
Yes. I just found about addEvent(callback, delay, ...) function in luascript.cpp. It should be fine if I'm gonna be able to pass arguments to callback. Will let know here

EDIT

@zbizu I wanted to call registerEvent(plusMinutes) from another lua function.

Alright, addEvent works just fine. Problem is solved, but I'm still curious if it is possible what I've done above. 🤨
 
Last edited:
Code:
local g = GlobalEvent("yourEventName")
g:type("time")
g:time("21:00:00")
g:onTime(yourFunctionName)
g:register()

This is the syntax I believe, though I'm not sure if you can split the creation to two parts. It's better to just pass necessary parameters to addEvent and create the event there.

edit: for clarification: if you have function executeSomething() some code end, your onTime will look like this: g:onTime(executeSomething)
 
Last edited:
Solution
Back
Top