if not npcid then
local npcidx = SL:Get_ACTOR_MAP_X(npcid)
local npcidy = SL:GetMetaValue("ACTOR_MAP_Y", npcid)
local npcx,npcy= SL:Get_ACTOR_MAP_X(npcid),SL:GetMetaValue("ACTOR_MAP_Y", npcid)
end
SL:Print(actorx,actor
🤖 回答引擎: 前端Lua |
🕒 时间: 2026-06-14 22:21:04 |
👁️ 阅读: 1
你的代码存在逻辑错误。当 `npcid` 为 `nil` 时,你进入了 `if not npcid then` 的代码块,然后在里面尝试使用 `npcid` 去获取坐标,这必然会导致 `npcidx` 和 `npcidy` 为 `nil`。
问题出在这里:
```lua
if not npcid then
-- 进入这个代码块,说明 npcid 是 nil
local npcidx = SL:Get_ACTOR_MAP_X(npcid) -- 这里传入了 nil
local npcidy = SL:GetMetaValue("ACTOR_MAP_Y", npcid) -- 这里也传入了 nil
-- 后面的代码同样有问题
end
```
你应该检查 `npcid` 是否存在,如果存在才获取坐标:
```lua
local npcidx, npcidy
if npcid then
-- 只有当 npcid 不是 nil 时才获取坐标
npcidx = SL:Get_ACTOR_MAP_X(npcid)
npcidy = SL:GetMetaValue("ACTOR_MAP_Y", npcid)
else
SL:Print("npcid 是 nil,无法获取坐标")
return
end
-- 现在再打印
SL:Print(actorx, actory, npcidx, npcidy)
```
或者使用更简洁的方式:
```lua
local npcidx = npcid and SL:Get_ACTOR_MAP_X(npcid) or nil
local npcidy = npcid and SL:GetMetaValue("ACTOR_MAP_Y", npcid) or nil
if npcidx and npcidy then
SL:Print(actorx, actory, npcidx, npcidy)
else
SL:Print("无法获取 NPC 坐标,npcid:", npcid)
end
```
**错误原因**:你在 `npcid` 为 `nil` 的情况下,仍然尝试调用需要 `npcid` 作为参数的函数,这自然得不到有效值。下次写条件判断时要注意逻辑顺序,确保变量有效后再使用!
你的代码存在逻辑错误。当 `npcid` 为 `nil` 时,你进入了 `if not npcid then` 的代码块,然后在里面尝试使用 `npcid` 去获取坐标,这必然会导致 `npcidx` 和 `npcidy` 为 `nil`。
问题出在这里:
```lua
if not npcid then
-- 进入这个代码块,说明 npcid 是 nil
local npcidx = SL:Get_ACTOR_MAP_X(npcid) -- 这里传入了 nil
local npcidy = SL:GetMetaValue("ACTOR_MAP_Y", npcid) -- 这里也传入了 nil
-- 后面的代码同样有问题
end
```
你应该检查 `npcid` 是否存在,如果存在才获取坐标:
```lua
local npcidx, npcidy
if npcid then
-- 只有当 npcid 不是 nil 时才获取坐标
npcidx = SL:Get_ACTOR_MAP_X(npcid)
npcidy = SL:GetMetaValue("ACTOR_MAP_Y", npcid)
else
SL:Print("npcid 是 nil,无法获取坐标")
return
end
-- 现在再打印
SL:Print(actorx, actory, npcidx, npcidy)
```
或者使用更简洁的方式:
```lua
local npcidx = npcid and SL:Get_ACTOR_MAP_X(npcid) or nil
local npcidy = npcid and SL:GetMetaValue("ACTOR_MAP_Y", npcid) or nil
if npcidx and npcidy then
SL:Print(actorx, actory, npcidx, npcidy)
else
SL:Print("无法获取 NPC 坐标,npcid:", npcid)
end
```
**错误原因**:你在 `npcid` 为 `nil` 的情况下,仍然尝试调用需要 `npcid` 作为参数的函数,这自然得不到有效值。下次写条件判断时要注意逻辑顺序,确保变量有效后再使用!
← 返回问答库