【发布时间】:2019-03-01 06:10:30
【问题描述】:
我正在尝试为我所在的在线社区制作 Lua 脚本,当我尝试搜索我相信的表数组时遇到问题。它没有检测到我想要的结果。
它的工作方式是,当有人键入/gps [streetname] 时,它会搜索顶部的表格,检测匹配的街道名称和坐标,然后将航点设置到该相关位置。
目前它在表中只有一个条目时有效,但是当我输入更多时,它将为任何不匹配的街道提供错误消息,然后为匹配的街道提供路点设置消息。我在 Google 上搜索过,但似乎找不到任何帮助。
任何帮助将不胜感激。
waypoint = {
{404.08, -920.23, 'sinnerstreet', 'Sinner Street'},
{360.85, -956.46, 'atleestreet', 'Atlee Street'},
{500.48, -956.80, 'littlebighornavenue', 'Little Bighorn Avenue'},
}
RegisterCommand('gps', function(source, args, rawCommand)
for k,v in pairs(waypoint) do
x, y, streetname, displayname = table.unpack(v)
results = ""
if args[1] == nil then
if IsWaypointActive() then
SetWaypointOff()
TriggerEvent('chatMessage', '^1^*GPS Navigation: ^r^7Your GPS system has been reset.')
return end
elseif args[2] == nil and args[3] == nil then
results = args[1]
elseif args[2] ~= nil and args[3] == nil then
results = args[1] .. args[2]
else
results = args[1] .. args[2] .. args[3]
end
results = string.lower(results) -- This convertes the args into lower case
end
-- This locates the streetname and sets a waypoint to it for the player
if string.find(streetname, results) then
SetNewWaypoint(x, y)
TriggerEvent('chatMessage', '^1^*GPS Navigation: ^r^7Your waypoint to ^1' .. displayname .. '^r^7 has been set.')
else
TriggerEvent('chatMessage', '^1^*GPS Navigation: ^r^7There has been an error with your street name, please try again.')
end
end)
TriggerEvent('chat:addSuggestion', '/gps', 'This creates a waypoint to your designated street. ^*USE: /gps [streetname]')
【问题讨论】:
-
首先,你好像没有使用
rawCommand参数,所以你还不如去掉。如果调用一个函数时使用的参数超过了它的需要,Lua 不会抱怨,它只是丢弃多余的参数,并且通常认为具有不使用的参数/变量是不好的风格,因为它只会让人们感到困惑。如果要显示函数获取了一个它不使用的参数,通常在参数名称前添加_。 -
for循环相同;如果只需要值,一般写for _,v in ... -
你知道我如何得到想要的效果吗?我已经按照您的建议进行了更改,但我仍然无法让命令正常工作?
-
我什至不完全理解你想要什么,但我可以告诉你你的代码做了什么(见我的回答),至少这看起来不像你想要的。可悲的是,您可能不得不几乎从头开始再试一次。
-
我基本上希望表格存储街道名称的坐标,然后当玩家输入 /GPS [streetname] 时,它将从表格中提取街道名称和坐标,并为他们设置新路点在他们的地图上。
标签: lua