我正在尝试为我所属的在线社区制作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]')
答案 0 :(得分:2)
说实话,您的代码几乎没有意义,这可能是因为您没有使用Lua提供的所有好东西。
{404.08, -920.23, 'sinnerstreet', 'Sinner Street'},
您正在那里存储冗余数据。第三个值实际上只是第四个值,其中删除了空格,并且全部小写。
'sinnerstreet' == ('Sinner Street'):gsub("[^%l]", ""):lower()
英语:在“ Sinner Street”上,以 g (在整个字符串中表示) sub 来表示不是小写的所有内容(%l
不带任何字母({{1)}的字母,然后将结果转换为小写。您得到的是“罪人街”。
""
在那儿使用全局变量,那不好。全球人是魔鬼。不要使用它们。
然后,再向下几行:
x, y, streetname, displayname = table.unpack(v)
考虑一下。您可以在for循环的每次迭代中设置SetNewWaypoint(x, y)
和x
。循环完成后,它们始终包含您迭代的最后一个航点的坐标。我怀疑那是你想要的。使用y
;它会迫使您思考变量的范围是什么,这将帮助您发现此类问题。
local
除非您特别希望将其限制为3个参数(我对此表示怀疑),否则还可以使用elseif args[2] ~= nil and args[3] == nil then
results = args[1] .. args[2]
来连接序列中的所有值(读取:数组)
table.concat
令我感到困惑的是为什么您要循环执行此操作。对于每个航路点,您都将results = string.lower( table.concat(args) )
设置为相同的值,该值将所有参数串联并转换为小写。
现在怎么办?您检查result
(用户搜索的内容)是否包含result
,正如我们先前所发现的那样,该列表中包含列表中最后一个航点的名称。
Lua具有表,它是编程中最强大的通用数据结构之一。
streetname
这将为您提供如下外观:
local map = {}
for _,waypoint in ipairs(waypoints) do
map[waypoint[3]:lower()] = waypoint
end
,如果您想知道街道是否存在,可以执行以下操作:
local map = {
sinnerstreet = {404.08, -920.23, 'sinnerstreet', 'Sinner Street'},
atleestreet = {360.85, -956.46, 'atleestreet', 'Atlee Street'},
littlebighornavenue ={500.48, -956.80, 'littlebighornavenue', 'Little Bighorn Avenue'},
}
if map['atleestreet'] then
print(map.atleestreet[4])
end
将不属于if
或false
的所有内容都视为真实内容,因此您只需在条件中写入“ map ['atleestreet”] nil
可以写为my_table['text']
尝试仔细考虑您的代码。如有必要,逐行进行检查,写下变量在每个时刻保持的值。如果您已经这样做了一段时间,请先休息一下或做其他事情。
然后尽可能将变量设置为my_table.text
(阅读:无处不在),弄清楚循环内外都需要什么,然后重试。
您可以只写local
而不是if something == nil
,而if not something
只写if something ~= nil
很抱歉,文本较长且在方括号内使用空格,但我希望这些内容特别易于理解。