• 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!

Lua Question about Os.Date()

  • Thread starter Deleted member 141899
  • Start date
D

Deleted member 141899

Guest
Hello,

I Have some globalevents that i use Os.Date for execute, like:
Code:
local days = {
['Monday'] = {},
['Wednesday'] = {},
['Friday'] = {},
['Saturday'] = {},
['Sunday'] = {}
}

if days[os.date("%A")] then

In tag of globalevent.xml have exact time it will run, example: 16:00 and the script is onTime().

But all days execute in the same time, and i want to change this, for each day execute different time.

Its possible to change in globalevents tag to "interval" and put the exact time for each day in the script? like:
Code:
local days = {
['Monday'] = {16:00}

how can i do this? i don't know much lua can someone help me? thanks
 
Hey,

You need not to know LUA in order to do this, but have a proper reference of how the time/date functions work in said language.
Here is a brief tutorial about time/date functions in LUA
http://www.lua.org/pil/22.1.html
A TL;DR
%a abbreviated weekday name (e.g., Wed)
%A full weekday name (e.g., Wednesday)
%b abbreviated month name (e.g., Sep)
%B full month name (e.g., September)
%c date and time (e.g., 09/16/98 23:48:10)
%d day of the month (16) [01-31]
%H hour, using a 24-hour clock (23) [00-23]
%I hour, using a 12-hour clock (11) [01-12]
%M minute (48) [00-59]
%m month (09) [01-12]
%p either "am" or "pm" (pm)
%S second (10) [00-61]
%w weekday (3) [0-6 = Sunday-Saturday]
%x date (e.g., 09/16/98)
%X time (e.g., 23:48:10)
%Y full year (1998)
%y two-digit year (98) [00-99]
%% the character `%´

Your options as I see it, though I may be missing something are two:
- Use an onTime to check every hour, and check if that date coincides with an event date.
- Use an onThink and when the exact hour is reach then start the event.
- Use one onTime and check the time difference for the next event in the given day, call the event with an addEvent(?) (I wouldn't do this).

This is kind of how it should look like:
Code:
local days = {
['Monday'] = "16:00:00",
['Wednesday'] = "23:00:00"
}

if(days[os.date("%A")] and (os.date("%X") == days[os.date("%A")])) then
-- Start event
else
-- Exit, no event scheduled for now

I think onTime has a parameter to help you here though.. I don't remember and have little experience in this particular area.
Hope it helps.
 
If you wanted to randomly execute something at specific time, although not sure where you would put this.
Code:
local days = { -- of course you may want to change the times per day
    ['Monday'] = {"01", "05", "12", "17"},
    ['Wednesday'] = {"01", "05", "12", "17"},
    ['Friday'] = {"01", "05", "12", "17"},
    ['Saturday'] = {"01", "05", "12", "17"},
    ['Sunday'] = {"01", "05", "12", "17"}
}
local dayOfTheWeek = days[os.date("%A")]
if dayOfTheWeek[math.random(#dayOfTheWeek)] == os.date("%H") then
    -- code here to execute
else
    -- do something else or nothing at all
end
 
Back
Top