没有返回正确的值

时间:2020-02-01 18:42:24

标签: lua

你好,我有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,这是我在声明时设置变量的值。当需要打印时,我要设置的指向。

1 个答案:

答案 0 :(得分:0)

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