我一直尝试使用this module尝试从回调函数返回一个值。
我正在覆盖从本地sql数据库检索数据的默认GetPData
函数,但我想从MySQL数据库服务器中检索它。
这是原始功能:
--[[---------------------------------------------------------
GetPData
- Saves persist data for this player
-----------------------------------------------------------]]
function meta:GetPData( name, default )
name = Format( "%s[%s]", self:UniqueID(), name )
local val = sql.QueryValue( "SELECT value FROM playerpdata WHERE infoid = " .. SQLStr(name) .. " LIMIT 1" )
if ( val == nil ) then return default end
return val
end
这是我尝试过的代码:
function Player:GetPData(key, default)
testget(self,key,default,function(junk)
//LocalPlayer():ChatPrint("You have $" .. junk .. ".");
print(junk)
return junk
end)
end
function testget(self,key, default, callback)
local retVal = default
insertQ = PDataDB:query("SELECT `value` from `playerdata` WHERE `uniqueid`='" .. self:UniqueID() .. "' AND key` = '" .. key .. "';");
insertQ.onError = DBError
insertQ.onData = function(self, data)
retVal = data.value or default
print(retVal)
print("--------------------------------------------------------------------------------------------------------------")
insertQ.onSuccess = function(self)
print(retVal)
callback(retVal);
end
insertQ:start()
end
end
这是一款名为garrys mod的游戏。我需要在我的服务器上跟踪xp。
答案 0 :(得分:1)
将您的功能修改为:
function Player:GetPData( key, default )
local retVal = 0 -- or any other default value you want
testget(self, key, default, function(junk)
//LocalPlayer():ChatPrint("You have $" .. junk .. ".");
print(junk)
retVal = junk
return junk
end
)
return retVal
end