Warning: file_get_contents(/data/phpspider/zhask/data//catemap/8/variables/2.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
Variables 控制Lua中的变量范围;它';他正在换两个变量_Variables_Lua_Scope - Fatal编程技术网

Variables 控制Lua中的变量范围;它';他正在换两个变量

Variables 控制Lua中的变量范围;它';他正在换两个变量,variables,lua,scope,Variables,Lua,Scope,我不明白为什么lua会同时改变这两个变量,尽管我的理解是函数的外侧不应该被触及 发生了什么?如何保持“攻击者”变量不变 谢谢 local attacker = { 0,-1 } local function test() local hitPattern = attacker print( "----------->> attacker", # attacker ) --Set Loop Method if hitPattern[ # hitP

我不明白为什么lua会同时改变这两个变量,尽管我的理解是函数的外侧不应该被触及

发生了什么?如何保持“攻击者”变量不变

谢谢

local attacker = { 0,-1 }

local function test()

    local hitPattern = attacker

    print( "----------->> attacker", # attacker )

    --Set Loop Method
    if hitPattern[ # hitPattern ] == -1 then
        hitPattern[ # hitPattern ] = nil
    end

    print( "----->> attacker", # attacker )

end
test()
----------->> attacker 2
----->> attacker 1
发件人:

表、函数、线程和(完整的)userdata值都是对象:变量实际上并不包含这些值,只包含对它们的引用。赋值、参数传递和函数返回总是操纵对这些值的引用;这些操作并不意味着任何类型的复制

因此,当您分配:

local hitPattern = attacker

变量
hitPattern
attacker
都引用同一个表,当您修改其中一个时,另一个也会更改。

那么,我如何解决这个问题呢?我想要一份表格的副本,而不是对它的引用。是否需要创建for循环并“重建”另一个表?@user3660167是,请参阅