Lua 中函数只返回一个值而非两个
2021-5-14 19:52:37
收藏:0
阅读:164
评论:2
function GetCharacterName(source)
local xPlayer = QBCore.Functions.GetPlayer(source)
local name = xPlayer.PlayerData.charinfo.lastname
local name2 = xPlayer.PlayerData.charinfo.firstname
if xPlayer then
return name, name2
end
end
我想返回 name 和 name2 两个值,但是实际上只返回了第一个值。我是 lua 和编程方面的新手,想要学习。
函数被调用的其它地方:
AddEventHandler("mdt:attachToCall", function(index)
local usource = source
local charname = GetCharacterName(usource)
local xPlayers = QBCore.Functions.GetPlayers()
for i= 1, #xPlayers do
local source = xPlayers[i]
local xPlayer = QBCore.Functions.GetPlayer(source)
if xPlayer.PlayerData.job.name == 'police' then
TriggerClientEvent("mdt:newCallAttach", source, index, charname)
end
end
TriggerClientEvent("mdt:sendNotification", usource, "You have attached to this call.")
end)
RegisterServerEvent("mdt:performVehicleSearchInFront")
AddEventHandler("mdt:performVehicleSearchInFront", function(query)
local usource = source
local xPlayer = QBCore.Functions.GetPlayer(usource)
if xPlayer.job.name == 'police' then
exports['ghmattimysql']:execute("SELECT * FROM (SELECT * FROM `mdt_reports` ORDER BY `id` DESC LIMIT 3) sub ORDER BY `id` DESC", {}, function(reports)
for r = 1, #reports do
reports[r].charges = json.decode(reports[r].charges)
end
exports['ghmattimysql']:execute("SELECT * FROM (SELECT * FROM `mdt_warrants` ORDER BY `id` DESC LIMIT 3) sub ORDER BY `id` DESC", {}, function(warrants)
for w = 1, #warrants do
warrants[w].charges = json.decode(warrants[w].charges)
end
exports['ghmattimysql']:execute("SELECT * FROM `player_vehicles` WHERE `plate` = @query", {
['@query'] = query
}, function(result)
local officer = GetCharacterName(usource)
TriggerClientEvent('mdt:toggleVisibilty', usource, reports, warrants, officer, xPlayer.job.name)
TriggerClientEvent("mdt:returnVehicleSearchInFront", usource, result, query)
end)
end)
end)
end
end)
点赞
用户2858170
有两种可能的原因。
- name2 是nil
- 您正在以调整返回值数为1的方式使用函数调用。当
GetCharacterName(source)不是表达式列表中的最后一个或将该函数调用放入括号中或将其分配给单个变量时,就会发生这种情况。
编辑:
既然你已经添加了关于如何使用该函数的示例:
local charname = GetCharacterName(usource)
在这种情况下,GetCharacterName的结果列表被调整为1个值(名称),因为你只分配给一个变量。如果你想要两个返回值,你需要两个变量。
要么像这样做:
local lastName, firstName = GetCharactername(usource)
local charName = lastName .. ", " .. firstname
然后charName是例如“特朗普,唐纳德”。
要么你将该名称作为单个字符串返回:
function GetCharacterName(source)
local xPlayer = QBCore.Functions.GetPlayer(source)
local name = xPlayer.PlayerData.charinfo.lastname
local name2 = xPlayer.PlayerData.charinfo.firstname
if xPlayer then
return name .. ", " .. name2
end
end
然后local charname = GetCharacterName(usource)将工作。
2021-05-14 18:52:24
评论区的留言会收到邮件通知哦~
推荐文章
- Lua 虚拟机加密load(string.dump(function)) 后执行失败问题如何解决
- 我想创建一个 Nginx 规则,禁止访问
- 如何将两个不同的lua文件合成一个 东西有点长 大佬请耐心看完 我是小白研究几天了都没搞定
- 如何在roblox studio中1:1导入真实世界的地形?
- 求解,lua_resume的第二次调用继续执行协程问题。
- 【上海普陀区】内向猫网络招募【Skynet游戏框架Lua后端程序员】
- SF爱好求教:如何用lua实现游戏内调用数据库函数实现账号密码注册?
- Lua实现网站后台开发
- LUA错误显式返回,社区常见的规约是怎么样的
- lua5.3下载库失败
- 请问如何实现文本框内容和某个网页搜索框内容连接,并把网页输出来的结果反馈到另外一个文本框上
- lua lanes多线程使用
- 一个kv数据库
- openresty 有没有比较轻量的 docker 镜像
- 想问一下,有大佬用过luacurl吗
- 在Lua执行过程中使用Load函数出现问题
- 为什么 neovim 里没有显示一些特殊字符?
- Lua比较两个表的值(不考虑键的顺序)
- 有个lua简单的项目,外包,有意者加微信 liuheng600456详谈,最好在成都
- 如何在 Visual Studio 2022 中运行 Lua 代码?

local name1, name2 = GetCharactersName()将以上代码翻译为中文并保留原本的markdown格式:
`local name1, name2 = GetCharactersName()` `local name1` 和 `name2` 是两个变量名,它们将从 `GetCharactersName()` 函数中获取到的角色名称赋给它们。