Function 在计算机辅助Lua中使用并行函数时的变量重置
我是一个在ComputerCraft(Minecraft)中设计Lua程序的初学者,该程序在玩家第一次使用它时会询问他们的名字,并记录下来。现在,我需要一个程序来检测变量Function 在计算机辅助Lua中使用并行函数时的变量重置,function,lua,minecraft,computercraft,Function,Lua,Minecraft,Computercraft,我是一个在ComputerCraft(Minecraft)中设计Lua程序的初学者,该程序在玩家第一次使用它时会询问他们的名字,并记录下来。现在,我需要一个程序来检测变量firstname是否等于nil,并询问名称是否等于nil。如果变量不等于nil,则表示不需要注册。我当前只有在从主菜单中按5后才调用register() 问题是每次我在收到提示时将字符串指定给firstname,当主菜单出现时,firstname返回到nil。我甚至把print(firstname)放在菜单的末尾来测试这一点
firstname
是否等于nil,并询问名称是否等于nil。如果变量不等于nil,则表示不需要注册。我当前只有在从主菜单中按5
后才调用register()
问题是每次我在收到提示时将字符串指定给firstname
,当主菜单出现时,firstname
返回到nil。我甚至把print(firstname)
放在菜单的末尾来测试这一点
我假设这是由于这一切都运行在并行函数中。我并行运行main menu()
和Controls()
,以便可以同时收听键盘输入和红石输入
在保留变量的同时,如何保持函数侦听和菜单工作
以下是完整的代码:
rednet.open("back") --Opens rednet on the back side of computer
local innerdooropen = false --Stuff to do with the airlock
local outerdooropen = false
function reset() --A handy function for screen reset
term.clear()
term.setCursorPos(1,1)
end
local function register() --This is the registration menu system
if firstname == nil then
print "Welcome to Obsidian Station!"
print "You must register before entering"
print "Please type your first name"
local firstname = read()
if firstname ~= "" then
print("Enter your last name")
local lastname = read()
print("You are now registered "..firstname.." "..lastname)
sleep(3)
rednet.broadcast(firstname.." "..lastname)
elseif firstname == "" then
print "You must register to enter"
shell.run("startup")
end
end
if firstname ~= nil then
print("Registration Not Needed")
sleep(2)
end
end
--Beginning of Section You Don't Have to Read
local function MainMenu()
while true do
term.clear()
term.setCursorPos(1, 1)
if innerdooropen == true then
rs.setOutput("left", false)
end
if outerdooropen == true then
rs.setOutput("right", false)
end
if innerdooropen == false then
rs.setOutput("left", true)
end
if outerdooropen == false then
rs.setOutput("right", true)
end
print "Safety Airlock Control"
print "~~~~~~~~~~~~~~~~~~~~~~"
print "[1] Open Outer Door"
print "[2] Open Inner Door"
print "[3] Close Both Doors"
print ""
print "[4] Open Both Doors - WARNING! DANGEROUS!"
print ""
print "[5] Register"
print(firstname)
input = read()
if input == "2" then
print "Inner Door Open"
outerdooropen = false
sleep "1"
innerdooropen = true
end
if input == "1" then
print "Outer Door Open"
innerdooropen = false
sleep "1"
outerdooropen = true
end
if input == "3" then
print "Both Doors Closed"
innerdooropen = false
outerdooropen = false
end
if input == "5" then
reset()
register()
end
if input == "6" then
print("firstname: "..firstname)
sleep(3)
end
if input == "4" then
term.clear()
term.setCursorPos(1, 1)
print "CONFIRM BOTH DOORS OPEN? [y] [n]"
input = read()
if input == "y" then
print "OPENING AIRLOCK DOORS IN"
sleep "1"
print "10"
sleep "1"
print "9"
sleep "1"
print "8"
sleep "1"
print "7"
sleep "1"
print "6"
sleep "1"
print "5"
sleep "1"
print "4"
sleep "1"
print "3"
sleep "1"
print "2"
sleep "1"
print "1"
sleep "1"
innerdooropen = true
outerdooropen = true
print "DOORS OPEN"
sleep "1"
end
elseif input == "n" then
term.clear()
term.setCursorPos(1, 1)
shell.run("startup")
end
end
end
--end of section you don't have to read
local function Controls()
while true do
local e = os.pullEvent()
if e == "redstone" and rs.getInput("bottom") then
redstone.setOutput ("left", true)
sleep "1"
redstone.setOutput ("right", false)
innerdooropen = true
outerdooropen = false
end
end
end
while true do
parallel.waitForAll(MainMenu,Controls)
end
在并行外部初始化firstname。将
local firstname
放在代码顶部,将local firstname=read()
更改为justfirstname=read()
,并对姓氏执行相同的操作
在检查变量是否为nil后,您将创建该变量,这就是它总是返回nil的原因。类似地,当函数结束时,firstname不再存在,因为它是在函数内部调用和创建的。所以它总是返回零。
代码的顶部应该是这样的
rednet.open("back") --Opens rednet on the back side of computer
local innerdooropen = false --Stuff to do with the airlock
local outerdooropen = false
local firstname = ""
local lastname = ""
另一部分应如下所示:
if firstname == nil then
print "Welcome to Obsidian Station!"
print "You must register before entering"
print "Please type your first name"
firstname = read()
if firstname ~= "" then
print("Enter your last name")
lastname = read()
print("You are now registered "..firstname.." "..lastname)
sleep(3)
在并行外部初始化firstname。将
local firstname
放在代码顶部,将local firstname=read()
更改为justfirstname=read()
,并对姓氏执行相同的操作
在检查变量是否为nil后,您将创建该变量,这就是它总是返回nil的原因。类似地,当函数结束时,firstname不再存在,因为它是在函数内部调用和创建的。所以它总是返回零。
代码的顶部应该是这样的
rednet.open("back") --Opens rednet on the back side of computer
local innerdooropen = false --Stuff to do with the airlock
local outerdooropen = false
local firstname = ""
local lastname = ""
另一部分应如下所示:
if firstname == nil then
print "Welcome to Obsidian Station!"
print "You must register before entering"
print "Please type your first name"
firstname = read()
if firstname ~= "" then
print("Enter your last name")
lastname = read()
print("You are now registered "..firstname.." "..lastname)
sleep(3)
在检查firstname是否等于nil之前,应该初始化firstname
local firstname = nil; --Move this up here so that you can access it elsewhere.
local lastname = nil; --This is also needed out here.
local function register() --This is the registration menu system
if firstname == nil then
print "Welcome to Obsidian Station!"
print "You must register before entering"
print "Please type your first name"
firstname = read() --Change this to just setting firstname to the read name.
if firstname ~= "" then
print("Enter your last name")
lastname = read()
print("You are now registered "..firstname.." "..lastname)
sleep(3)
rednet.broadcast(firstname.." "..lastname)
elseif firstname == "" then
print "You must register to enter"
shell.run("startup")
end
end
if firstname ~= nil then --Now this will not error as well.
print("Registration Not Needed")
sleep(2)
end
end
这允许在其他地方访问firstname,而不仅仅是在firstif语句中。您正在检查未初始化的变量是否等于nil
编辑:看到了purxiz的答案,意识到您可能也希望lastname位于循环之外。我现在在上面的代码中修复了它。在检查firstname是否等于nil之前,应该初始化firstname
local firstname = nil; --Move this up here so that you can access it elsewhere.
local lastname = nil; --This is also needed out here.
local function register() --This is the registration menu system
if firstname == nil then
print "Welcome to Obsidian Station!"
print "You must register before entering"
print "Please type your first name"
firstname = read() --Change this to just setting firstname to the read name.
if firstname ~= "" then
print("Enter your last name")
lastname = read()
print("You are now registered "..firstname.." "..lastname)
sleep(3)
rednet.broadcast(firstname.." "..lastname)
elseif firstname == "" then
print "You must register to enter"
shell.run("startup")
end
end
if firstname ~= nil then --Now this will not error as well.
print("Registration Not Needed")
sleep(2)
end
end
这允许在其他地方访问firstname,而不仅仅是在firstif语句中。您正在检查未初始化的变量是否等于nil
编辑:看到了purxiz的答案,意识到您可能也希望lastname位于循环之外。我现在在上面的代码中修复了它。当然。这很有道理。谢谢你尽可能清楚地解释这一点。当然。这很有道理。感谢您尽可能清楚地解释这一点。