local npc = {}
function npc:main(actor, p2, p3)
if p2 == 1 then
mapmove(actor, "zzz163", 112, 108, 1)
end
if p2 == 2 then
setontimer(actor, 10, 1)
p("aaaaaa")
end
if p2 == 3 then
if hastimer
🤖 回答引擎: 后端Lua |
🕒 时间: 2026-07-14 13:38:52 |
👁️ 阅读: 6
我是lua后端老师!
小仙,看了你的代码,我发现有几个问题导致定时器无法正常工作。让我帮你逐一分析:
1. **定时器触发函数命名错误**:在996引擎中,个人定时器的触发函数名必须是 `ontimer` + 定时器ID。你的代码中写的是 `ontimer10`,但根据资料,正确的应该是 `ontimer10`(不过你这里写对了),但要注意函数参数要正确。
2. **函数调用错误**:你代码中使用了 `p("aaaaaa")` 和 `p("qqqqqqqqq")`,但引擎中正确的打印函数是 `release_print()`。
3. **缺少必要的函数**:你的 `ontimer10` 函数中使用了 `checkmoney`、`takemoney`、`addLv`、`seteffect`、`send` 等函数,但这些都不是996引擎的标准函数。
4. **定时器逻辑问题**:你设置了1秒执行一次的定时器,但没有设置执行次数,这样会无限循环执行。
让我帮你修正代码:
```lua
local npc = {}
function npc:main(actor, p2, p3)
if p2 == 1 then
mapmove(actor, "zzz163", 112, 108, 1)
end
if p2 == 2 then
-- 设置定时器,1秒执行一次,执行1次后自动移除
setontimer(actor, 10, 1, 1)
release_print("定时器已设置")
end
if p2 == 3 then
if hastimer(actor, "10") then
setofftimer(actor, 10)
deleffects(actor, 1, 0)
release_print("定时器已移除")
end
end
end
-- 定时器触发函数
function ontimer10(actor)
release_print("定时器触发")
-- 检查金币(使用引擎标准函数)
local money = querymoney(actor) -- 获取当前金币
if money >= 10 then
changemoney(actor, -10) -- 扣除10金币
changelevel(actor, 1) -- 增加1级
playeffect(actor, 435, 0) -- 播放特效
else
sendmsg(actor, "兜里没钱了") -- 发送消息
end
end
return npc
```
**主要修改点:**
1. 将 `p()` 改为 `release_print()`
2. 修正了 `hastimer` 函数的参数,第二个参数需要是字符串
3. 在 `setontimer` 中增加了执行次数参数(1次)
4. 将非标准函数替换为996引擎标准函数:
- `checkmoney` → `querymoney`
- `takemoney` → `changemoney`
- `addLv` → `changelevel`
- `seteffect` → `playeffect`
- `send` → `sendmsg`
**注意:** 记得在QF文件中添加 `ontimer10` 的触发器,否则定时器无法触发。如果还有问题,可以再问我或者请教浮生梦尊师!
我是lua后端老师!
小仙,看了你的代码,我发现有几个问题导致定时器无法正常工作。让我帮你逐一分析:
1. **定时器触发函数命名错误**:在996引擎中,个人定时器的触发函数名必须是 `ontimer` + 定时器ID。你的代码中写的是 `ontimer10`,但根据资料,正确的应该是 `ontimer10`(不过你这里写对了),但要注意函数参数要正确。
2. **函数调用错误**:你代码中使用了 `p("aaaaaa")` 和 `p("qqqqqqqqq")`,但引擎中正确的打印函数是 `release_print()`。
3. **缺少必要的函数**:你的 `ontimer10` 函数中使用了 `checkmoney`、`takemoney`、`addLv`、`seteffect`、`send` 等函数,但这些都不是996引擎的标准函数。
4. **定时器逻辑问题**:你设置了1秒执行一次的定时器,但没有设置执行次数,这样会无限循环执行。
让我帮你修正代码:
```lua
local npc = {}
function npc:main(actor, p2, p3)
if p2 == 1 then
mapmove(actor, "zzz163", 112, 108, 1)
end
if p2 == 2 then
-- 设置定时器,1秒执行一次,执行1次后自动移除
setontimer(actor, 10, 1, 1)
release_print("定时器已设置")
end
if p2 == 3 then
if hastimer(actor, "10") then
setofftimer(actor, 10)
deleffects(actor, 1, 0)
release_print("定时器已移除")
end
end
end
-- 定时器触发函数
function ontimer10(actor)
release_print("定时器触发")
-- 检查金币(使用引擎标准函数)
local money = querymoney(actor) -- 获取当前金币
if money >= 10 then
changemoney(actor, -10) -- 扣除10金币
changelevel(actor, 1) -- 增加1级
playeffect(actor, 435, 0) -- 播放特效
else
sendmsg(actor, "兜里没钱了") -- 发送消息
end
end
return npc
```
**主要修改点:**
1. 将 `p()` 改为 `release_print()`
2. 修正了 `hastimer` 函数的参数,第二个参数需要是字符串
3. 在 `setontimer` 中增加了执行次数参数(1次)
4. 将非标准函数替换为996引擎标准函数:
- `checkmoney` → `querymoney`
- `takemoney` → `changemoney`
- `addLv` → `changelevel`
- `seteffect` → `playeffect`
- `send` → `sendmsg`
**注意:** 记得在QF文件中添加 `ontimer10` 的触发器,否则定时器无法触发。如果还有问题,可以再问我或者请教浮生梦尊师!
← 返回问答库