This is a read-only snapshot of the ComputerCraft forums, taken in April 2020.
mlewelt's profile picture

program works when starting manually, but crashes when its opened via startup

Started by mlewelt, 07 July 2013 - 01:45 PM
mlewelt #1
Posted 07 July 2013 - 03:45 PM
I dont know if theres a solution for this already. Hopefully not.
I googled again but it just makes no sense.
so heres my problem: i have a working program ( thanks to lyqyd ;)/> )
It works, but only when i start it manually from the terminal. when i run it as a autostart ( both renaming it to startup or making a startup file and shell.run the main program ) it crashes.
It gives me the error: attempt to call nil
Now i know what that error means. As you will see later, he cant find a function with the key value 2 - 5 inside a tabel. when running the program manually however it works. o.O
heres the program:

---[[
	explanation of the program:
	This program is used for a door. The program has multiple modes ( 4 to be exact ).
	  Mode1: the door is locked, no rs signal will change that
	  Mode2: the door is locked from outside, rs from inside will open the door
	  Mode3: the door is closed but will open for 3 seconds when rs from inside or outside is active
	  Mode4: the door is always open
	When entering the correct password, you get in a menu where you can select which mode you want to run.
	Of course there only can be one mode runnig at a time.
---]]
local password = "administrator"
local input
local access = false
local try = 0
local functions = {		
	[2] = lock,
	[3] = lockOutside,
	[4] = openRedstone,
	[5] = open
	}

function ini()
while access == false do
  if try > 4 then
   term.setCursorPos(1,2)
   print("You "..try.." times typed in the wrong password.")
   term.setCursorPos(1,3)
   print("Please wait 10 seconds.")
   sleep(10)
   term.clearLine()
   term.setCursorPos(1,2)
   term.clearLine()
   try = 0
  end
  term.setCursorPos(1,2)
  print("Please type in a password: ")
  term.setCursorPos(1,3)
  input = read()
  term.clearLine()
  term.setCursorPos(1,2)
  term.clearLine()
  if input == password then
   access = true
   print("password correct!")
   sleep(1.5)
   term.clearLine()
   term.setCursorPos(1,2)
   term.clearLine()
  else
   print("Wrong password!")
   try = try + 1
   sleep(1.5)
   term.clearLine()
   term.setCursorPos(1,2)
   term.clearLine()
  end
end
end
function menu()
shell.run("clear")
print("You have the following options:")
term.setCursorPos(1,2)
print("Close door")
term.setCursorPos(1,3)
print("Close door ( outside only )")
term.setCursorPos(1,4)
print("Open door while redstone is active")
term.setCursorPos(1,5)
print("Open door")
end
function lock()
shell.run("clear")
print("Door is locked.")
ini()
end
function getRsTop()
while true do
  if rs.getInput("back", true) then
   rs.setBundledOutput("bottom", 1)
   sleep(3)
   rs.setBundledOutput("bottom", 0)
  end
  sleep(0.5)
end
end
function getRsBottom()
while true do
  if rs.testBundledInput("bottom", 2) then
   rs.setBundledOutput("bottom", 1)
   sleep(3)
   rs.setBundledOutput("bottom", 0)
  end
  sleep(0.5)
end
end
function lockOutside()
shell.run("clear")
print("Door is locked from outside.")
parallel.waitForAny(ini, getRsTop)
end
function openRedstone()
shell.run("clear")
print("door is free. Open with redstone signal.")
parallel.waitForAny(ini, getRsTop, getRsBottom)
end
function open()
shell.run("clear")
print("Door is always open.")
rs.setBundledOutput("bottom", 1)
ini()
rs.setBundledOutput("bottom", 0)
end
function getFunction()
x, y = nil, nil
event, button, x, y = os.pullEvent("mouse_click")
if y > 1 and y < 6 then
  access = false
  print(y)
  functions[y]()
end
x, y = nil, nil
end
while true do
menu()
getFunction()
end

Lyqyd #2
Posted 07 July 2013 - 03:54 PM
Split into new topic.

Put the functions table below the function declarations. The first time you run the program on a freshly booted computer, it won't be able to find the functions, since they haven't been declared by the time you try to put them into the table (the values will be nil). The second time you run it, you will have already declared the functions (since they are declared globally, your second run of the program is using the declarations from the first run). Moving the table down below the function declarations will fix that issue.
mlewelt #3
Posted 07 July 2013 - 03:56 PM
Thank you again. it worked !! kinda stupid fail from my site :P/>
This is truely the most awesome forum ive ever seen!