Skip to content

YaTimeAPI

YaTimeAPI provides time related APIs for users. Note: Time APIs would only provide time resolution of no larger than 0.1 second. That means, invoking APIs with time less than 0.1 seconds would lead to truncated results (e.g., WaitFor 0.01 seconds may cause the callback be invoked immediately).

Functions

number YaTimeAPI.GetTimeElapsedSinceStart()

Returnsseconds since the application started
Get the time elapsed since the application started. Note: This API is not networked. That means when invoked on a client, the returned value would be different from the server. After all the API only returns the time elapsed since the endpoint (i.e., the server or Yahaha application) starts.

CancelToken YaTimeAPI.WaitFor(number delaySeconds, function callback)

Returnsthe handle of the callback registration
Schedule a time event that happens in delaySeconds and register the event callback.
  • delaySeconds number of seconds after which time event is triggered
  • callback the procedure to invoke when the time event is triggered

When you need to trigger logic at a specific time, you can use the YaTime.WaitFor method.

1
2
3
4
5
-- Print a log in five minutes.
YaTimeAPI.WaitFor(5, function()
    -- Execution logic
    print("Processing the delayed task")
end)

You can achieve the effect of executing a series of delayed logic by using nested WaitFor calls.

 1
 2
 3
 4
 5
 6
 7
 8
 9
10
11
12
13
14
15
function SayGoodbye()
    print("Goodbye")
end

function Chat()
    print("Chatting")
    YaTimeAPI.WaitFor(1, SayGoodbye)
end

function SayHello()
    print("Hello")
    YaTimeAPI.WaitFor(1, Chat)
end

YaTimeAPI.WaitFor(1, SayHello)

CancelToken YaTimeAPI.ScheduleAtInterval(number startSeconds, number intervalSeconds, function callback)

Returnsthe handle of the callback registration
Schedule a periodical time event that starts in startSeconds and triggered every intervalSeconds. Then register the event callback.
  • startSeconds number of seconds after which periodical time event starts
  • intervalSeconds number of seconds between consequence triggers of the time event (this value must not be smaller than 0.1)
  • callback the procedure to invoke when the time event is triggered

When you need to trigger logic periodically, you can use the YaTimeAPI.ScheduleAtInterval method.

1
2
3
4
5
6
local metCounter = 0
-- Print the log in 2 seconds. Then print the log every second.
YaTimeAPI.ScheduleAtInterval(2, 1, function()
    metCounter = metCounter + 1
    print("Hey! We have met " .. metCounter .. " times")
end)

You can control some procedural logic by using the YaTimeAPI.GetSecondsSinceStart method.

 1
 2
 3
 4
 5
 6
 7
 8
 9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
-- This example implements a round manager where each round lasts for 5 seconds with a 1-second gap between rounds.
local gameRound = 0
local nextStatusSeconds = 10
local roundActive = false
YaTimeAPI.ScheduleAtInterval(0, 1, function()
    -- The update logic is triggered only when the update condition is met.
    if YaTimeAPI.GetTimeElapsedSinceServerStart() > nextStatusSeconds then
        -- Stop the round if the round is activated.
        if roundActive then
            print("Round " .. gameRound .. " ends...")
            -- Update the round status after waiting for 1 second.
            nextStatusSeconds = nextStatusSeconds + 1
            roundActive = false
        -- Start the round otherwise.
        else
            gameRound = gameRound + 1
            -- Update the round status after waiting for 5 seconds.
            nextStatusSeconds = nextStatusSeconds + 5
            print("Round " .. gameRound .. " begins...")
            roundActive = true
        end
    end
end)