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

edit:50: attempt to call nil

Started by Dartania Thorne, 18 September 2013 - 12:23 AM
Dartania Thorne #1
Posted 18 September 2013 - 02:23 AM
Hello. This is the first issue I've run into where I can't find a work-around or an answer already posted. Hence, this is the first time I'm posting, even though I use this all the time.

My error is an attempt to call nil; however, it's not usual. I get this error when I type in the root(?) directory "edit boot"
edit:50: attempt to call nil
This error goes away temporarily when I restart the turtle. But so do all my functions and variables.

boot is the temporary name of a program I'm making to install a significantly larger set of programs on all my construction turtles, from a disk drive. I will later change the program's name to something talking about installation. And boot will be reused as the temporary name of the initialization file, which will be moved to startup. This program currently named boot will be used for other program-set installations as well. Currently, I only have it set up to confirm the disk drive and floppy disk name.

The edit:50: error occurs when I try to edit my program before resetting and after it is successfully run with the variable 'peri' set to whatever side the drive is on. I can still copy the program to a new name. And… the exact circumstances of the error keep changing. Now it's triggering when I tell it that it's not the right disk(hit a key that's not 'y').

I'm pretty sure that the error lies somewhere with one of my variables. I'm just not sure which one, and how to fix it. The code for the boot program follows.

 1.----Confirm Disk to Install----
 2.
 3.i=1
 4.peri = "blah"
 5.io = nil
 6.while io ~= "y" and peri ~= nil do
 7.  peri = peripheral.getNames()[i]
 8.  if peripheral.getType(peri) == "drive" then
 9.    print("Is \""..disk.getLabel(peri).."\" what you wish to download?")
10.    print("Any character but \"y\" will return a negative response.")
11.    temp, io = os.pullEvent("char")
12.  end
13.  if peri == "right" then
14.    print("No (more) Valid Drives")
15.  end
16.  i = i+1
17.end
18.
19.----Download Programs----
20.
21.
22.----Install----
Bubba #2
Posted 18 September 2013 - 06:55 AM
Line 5: You set the global variable io to nil, which is required for edit to function. Change the io variable to something else (such as input) and you should be good to go.
MKlegoman357 #3
Posted 18 September 2013 - 02:05 PM
Change "io" to something else or start using local variables (just add "local" before a variable name):


----Confirm Disk to Install----

local i=1
local peri = "blah"
local io = nil

while io ~= "y" and peri ~= nil do
  peri = peripheral.getNames()[i]

  if peripheral.getType(peri) == "drive" then
    print("Is \""..disk.getLabel(peri).."\" what you wish to download?")
    print("Any character but \"y\" will return a negative response.")
    temp, io = os.pullEvent("char")
  end

  if peri == "right" then
    print("No (more) Valid Drives")
  end

  i = i + 1
end

----Download Programs----

----Install----
Dartania Thorne #4
Posted 18 September 2013 - 04:47 PM
Ok, so by 'global variable' you mean a variable that's used by other programs. All my variables are global, so that I can run a program and debug in the interactive lua prompt. I did not realize that io was used by the default programs. Originally, I thought you meant for me to write io = "input" Instead, you meant for me to write input = nil

In the end, I changed the variable I used, and my program no longer breaks the computer. Thanks a bunch, to both of you. I won't use local variables, but I will look up a list of default globals and start a list of important personal globals. I always reset any variables I must use, at the beginning of the program or section. I thought the default systems did the same.

This problem is solved.

~Dartania Thorne
immibis #5
Posted 18 September 2013 - 09:28 PM
io is an API, so they can't "reset it at the beginning."
If someone else did "peripheral = nil" and then ran your program, it would crash, and there's nothing your program can do about it.
Lyqyd #6
Posted 19 September 2013 - 12:44 AM
You should really use locals and correctly scope your variables.