我在一个函数中有一个数据库查询,该函数返回该查询的结果,而在另一个函数中,我想检索该结果,但我不知道如何异步执行。我正在对数据库查询使用“ mysqloo”库https://github.com/FredyH/MySQLOO。
第一个函数,带有查询:
function meta:getMoney()
local query2 = databaseObject:query("SELECT economy FROM luvinsastroi_player WHERE steamid = '" .. self:SteamID64() .. "' ")
query2.onData = function( q, d)
return tonumber(d['economy'])
end
query2:start()
end
然后:
hook.Add( "PlayerSay", "MoneyCommand", function( ply, text, team )
if(text == "/money") then
local money = ply:getMoney()
ply:PrintMessage( HUD_PRINTTALK, "Vous avez " .. money .. "€." )
end
end )
在第二个函数中,金钱为零,因此Error on ply:PrintMessage ( HUD_PRINTTALK, "Vous avez " .. money .. "€." ) attempt to concatenate a nil value (money)
如何从return tonumber(d['economy'])
功能等待meta:getMoney()
?
这可能有用,但是我不确定:
function meta:getMoney(cb)
local query2 = databaseObject:query("SELECT economy FROM luvinsastroi_player WHERE steamid = '" .. self:SteamID64() .. "' ")
if cb then
query2.onData = function(q, d)
cb(tonumber(d['economy']))
end
else
query2.onData = function(q, d)
return tonumber(d['economy'])
end
end
query2:start()
end
hook.Add("PlayerSay", "MoneyCommand", function(ply, text, team)
if(text == "/money") then
local function callback_money(money)
ply:PrintMessage(HUD_PRINTTALK, "Vous avez " .. money .. "€." )
end
ply:getMoney(callback_money)
end
end)