Warning: file_get_contents(/data/phpspider/zhask/data//catemap/8/lua/3.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
Lua 如何将局部变量重置为原始值?_Lua_Scope - Fatal编程技术网

Lua 如何将局部变量重置为原始值?

Lua 如何将局部变量重置为原始值?,lua,scope,Lua,Scope,在为IOS编写bot脚本时,我想使用特定坐标检查当前屏幕状态的位置。我将通过使用命令调用坐标的rgb颜色来实现这一点。下面是一个示例,假设print将返回rgb 如果运行此命令,它将返回 100,200 200,300 200,300 300400 是否可以调用example()内部的x和y,而不覆盖函数外部的本地x和本地y 编辑:我的解决方案 local x=100 local y=200 function getxy() print(x .. "," .. y) end fu

在为IOS编写bot脚本时,我想使用特定坐标检查当前屏幕状态的位置。我将通过使用命令调用坐标的rgb颜色来实现这一点。下面是一个示例,假设
print
将返回rgb



如果运行此命令,它将返回 100,200 200,300 200,300 300400

是否可以调用
example()
内部的x和y,而不覆盖函数外部的本地x和本地y


编辑:我的解决方案

local x=100
local y=200

function getxy()
   print(x .. "," .. y)
end

function home()
   x=100
   y=200
end

function example()
   x=200
   y=300
   getxy()
   home()
end

function example2()
   x=300
   y=400
   getxy()
   home()
end

getxy()
example()
getxy()
example2()
现在运行它将返回100200 200300 100200 300400

这正是我想要的。
因此,我添加了另一个名为
home()
的函数。这对我试图完成的任务有效吗?还是应该将
home()
调用放在
getxy()
函数中?

使用以下示例

local x,y = 100,200;
function output(x,y)
    print('x =',x,', y =',y);
end

function home(o)
    local x,y = x+50,y+50; -- this uses the declared x,y but assigns them to a local variables named x,y... so original x,y won't be rewritten..
    o = (o and output(x,y)); -- lazy call to output instead doing if then end..
end
function example1(o)
    local x,y = 15,20;
    o = (o and output(x,y));
end
function example2(o)
    local x,y = x*x/(x+x),y*y/(y+y);
    o = (o and output(x,y))
end
print('original variables value =',x,y); -- original variables value = 100 200
home(true); -- x = 150, y = 250
example1(true); -- x = 15, y = 20
example2(true); -- x = 50, y = 100
print('variables value =',x,y); -- variables value = 100 200

总的来说,我对逻辑和编程还是个新手。你能解释一下函数后的
(o)
是做什么的吗?我不能理解你的局部变量,在
example2
o
是一个局部变量,这是一个调用函数的“hack”。它基本上执行
o=如果o然后返回输出(x,y)end
,但方式更短。或者我们可以做
if(o)然后输出(x,y);结束
。关于示例2,首先它得到x值(100),乘以x(100),即1000,再除以2x(200),因此输出为50,然后它设置一个局部变量(名为
x
,新变量,不会用值(50)覆盖主x,y)。
local x,y = 100,200;
function output(x,y)
    print('x =',x,', y =',y);
end

function home(o)
    local x,y = x+50,y+50; -- this uses the declared x,y but assigns them to a local variables named x,y... so original x,y won't be rewritten..
    o = (o and output(x,y)); -- lazy call to output instead doing if then end..
end
function example1(o)
    local x,y = 15,20;
    o = (o and output(x,y));
end
function example2(o)
    local x,y = x*x/(x+x),y*y/(y+y);
    o = (o and output(x,y))
end
print('original variables value =',x,y); -- original variables value = 100 200
home(true); -- x = 150, y = 250
example1(true); -- x = 15, y = 20
example2(true); -- x = 50, y = 100
print('variables value =',x,y); -- variables value = 100 200