Lua接口/时间与计时

来自天龙知识库
雪舞留言 | 贡献2026年9月12日 (六) 13:37的版本 (维护服务端 Lua 接口:核对参数、返回值、边界条件与调用示例)
(差异) ←上一版本 | 最后版本 (差异) | 下一版本→ (差异)
跳到导航跳到搜索

首页 · 服务端接口 · 调用约定

读取服务器时间、日期字段、世界时间,以及为玩家设置计时器。取日期时注意月份为 0 到 11(tm 约定)。

当前时间

接口 参数与返回
LuaFnGetCurrentTime () → 当前时间(秒,无参)。
mytime () → 当前 ANSI 时间戳(无参);失败 -1。
GetHour () → 当前小时 0-23(无参);失败 -1。
GetMinute () → 当前分钟 0-59(无参);失败 -1。
GetDayTime () → 当前天数(如 1140 表示 2001 年第 140 天,无参);失败 -1。

这几个接口都不接受参数。

日期字段

接口 参数与返回
GetTodayYear () → 四位公历年份;失败 -1。
GetTodayMonth () → 月份索引 0-11;失败 -1。
GetTodayDate () → 本月日号;失败 -1。
GetTodayWeek () → 星期索引,星期日为 0;失败 -1。
LuaFnGetThisYear () → 公历年份;失败 -1。
LuaFnGetThisMonth () → 月份 0-11(tm 约定);失败 -1。
LuaFnGetDayOfThisMonth () → 本月日号 1-31;失败 -1。

GetTodayMonthLuaFnGetThisMonth 都返回 **0 到 11**(tm 约定),不要当 1 到 12 用。GetTodayWeek 里**星期日是 0**。

周与时间片

接口 参数与返回
GetWeekTime () → 年份和周序号组合值;失败 -1。
GetTime2Day () → 当前日期 YYYYMMDD 数值;失败 -1。
GetQuarterTime () → 15 分钟时间片编号;失败 -1。

GetTime2Day 返回形如 20260912 的整数,便于直接比较日期。GetQuarterTime 以 15 分钟为一片。

世界时间

接口 参数与返回
LuaFnGetCurrentWorldTime () → 世界服务器同步的十二时辰索引;失败 -1。

LuaFnGetCurrentWorldTime 取的是**世界服同步的十二时辰**,与本地小时不同。

玩家计时器

接口 参数与返回
SetTimer sceneId, selfId, scriptId, funcName, tickTime → 计时器索引;失败 -1。
StopTimer sceneId, sceneTimerIndex;无返回值。
CheckTimer sceneId, TimeIndex → 是否在使用中;失败 -1。

SetTimer 需要 5 个参数,时间到会自动调用指定脚本的 funcName(约定为 OnTimer)。**每个玩家身上最多绑定三个计时器**。StopTimer 无返回值,且只传计时器索引,不传 selfId。

单位与在线时间

接口 参数与返回
SetCharacterTimer sceneId, selfId, nTime;无返回值。nTime 为 0 时停止时钟。
SetCharacterDieTime sceneId, selfId, nTime;无返回值。只对 Monster 和 Pet 有效。
GetGameTime sceneId, selfId → 玩家在线时间(秒);失败 -1。
GetObjCreateTime sceneId, selfId → 对象创建时间;失败 -1。

SetCharacterTimer 设置单位时钟间隔,传 0 停止;SetCharacterDieTime 只对怪物和珍兽有效。

读取当前日期

local year = GetTodayYear()
local month = GetTodayMonth()
local day = GetTodayDate()
if type(year) == "number" and type(month) == "number" and type(day) == "number" then
    -- month 为 0-11,显示时需要加 1
end

GetTodayMonth 返回 0 到 11,展示给玩家时需要加 1。

设置玩家计时器

local idx = SetTimer(sceneId, selfId, scriptId, "OnTimer", 60000)
if type(idx) == "number" and idx ~= -1 then
    -- 计时器创建成功,idx 为索引
end

SetTimer 返回计时器索引,失败 -1;关闭时用 StopTimer 传该索引。

相关主题

服务端调用约定 · 服务端调用示例 · 服务端函数索引

函数不存在或提示 nil · 参数类型与顺序 · 返回值判断 · 对象 ID 与 GUID

本页函数签名与返回约定依据当前服务端注册表整理;sceneId、selfId 由实际回调提供,业务枚举、属性类型编号与运行效果需要实测。