温馨提示:本文翻译自stackoverflow.com,查看原文请点击:lua - Not returning correct value
lua

lua - 没有返回正确的值

发布于 2020-04-10 17:55:29

您好,我有2个函数返回一个值

function AddPlayer(steamID, source)
    for i,p in ipairs(players) do
        if steamID == p[1] then
            players[i] = {p[1], p[2], source}
            return
        end
    end

    local initialPoints = GetInitialPoints(steamID)
    print(initialPoints)
    table.insert(players, {steamID, initialPoints, source})
end

function GetInitialPoints(steamID)
    local points = 0
    print(steamID)
    MySQL.Async.fetchAll("Select priority FROM queue where `identifier`= @identifier",
    {
        ['@identifier'] = steamID,
    },
    function(resp)
        points = resp[1].priority
        print(points)
    end)
    return points
end

因此打印件实际上会打印正确的值(10,000)

但是在返回函数AddPlayer上,我在其中打印初始点,它正在打印0,这是我在声明时设置的变量。当需要打印时,我要设置的指向。

查看更多

提问者
jake pavek
被浏览
68
Egor Skriptunoff 2020-02-02 05:41
function AddPlayer(steamID, source)
    for i,p in ipairs(players) do
        if steamID == p[1] then
            players[i] = {p[1], p[2], source}
            return
        end
    end
    print(steamID)
    MySQL.Async.fetchAll(
        "Select priority FROM queue where `identifier`= @identifier",
        {
            ['@identifier'] = steamID,
        },
        function(resp)
            local initialPoints = resp[1].priority
            print(initialPoints)
            table.insert(players, {steamID, initialPoints, source})
        end
    )
end