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

Attempt to call a string value. error doesn't happen on first call

Started by vargaszm, 30 May 2014 - 03:30 PM
vargaszm #1
Posted 30 May 2014 - 05:30 PM
Hello World

I'm working on building an intro to programming curriculum with computer craft aimed at kids grades 1-3. I have a system where they don't directly deal with the shell until a later lesson, to keep them focus on coding and avoid confusion. it's also set up(in other code) for them to edit on a computer which sends the programs to the turtle over rednet. Right now i have a file menu system, when they create a new program it copies a template and opens it for editing. Everything works, except the copy function. it works once, and everything else works after you use it once. if you enter copy again i get

101:attempt to call string

i don't see and missing ..'s and it did work the first time.



function edit()
 local input="42"
 while true do
  valid=false
  while not valid do
 term.clear()
 term.setCursorPos(1,1)
 print("Program Editor")
 print("")
   list = fs.list("turtle")
    if #list==0 then
     print("No Programs Created Yet. Hit enter.")
     io.read()
     return
    end
   print([[Enter the name of the program you'd like to edit or x to exit then hit enter.
   ]])
   while not valid do
    list = fs.list("turtle")
    textutils.pagedTabulate(list)
    print("")
    input = tostring(io.read())
    for i=1, #list do
     if list[i] == input then valid = true break end
    end
    if not valid and input~="x" and input~="42" then
      print(input.." does not exist. Please check for typos and try again or type x to exit.")
    elseif input=="x" then
     return  
    end
   end
  end
  shell.run("fg edit turtle/"..input)
 end
 return
end

function copy()
 local input = "42"
 list = fs.list("turtle")
  while true do
   valid=false
   while not valid do
    term.clear()
    term.setCursorPos(1,1)
    print("Program Copier")
    print("")
   while not valid do
    list = fs.list("turtle")
    textutils.pagedTabulate(list)
    print("")
    input = tostring(io.read())
    for i=1, #list do
     if list[i] == input then valid = true break end
    end
    if not valid and input~="x" and input~="42" then
      print(input.." does not exist. Please check for typos and try again or type x to exit.")
    elseif input=="x" then
     return  
    end
   end
  print("What will you name this copy of "..input.."?")
  copy =tostring(io.read())
  fs.copy("turtle/"..input,"turtle/"..copy)
  end
  print("Opening "..copy.." for editing in a new tab.")
  sleep(1.6)
  shell.run("fg edit turtle/"..copy)
  return
 end
end

function new()
 term.clear()
 term.setCursorPos(1,1)
 print("Create New Program From Template.")
 print("")
 print("Enter a name for this new program. Do not use spaces.")
 input = io.read()
 while fs.exists("turtle/"..input) and input~="x" do
  print(input.." alio.ready exists. Enter a different name or enter x to exit")
 local input = io.read()
 end
 fs.copy("template", "turtle/"..input)
 shell.run("fg edit turtle/"..input)
 return
end

while true do
 term.clear()
 term.setCursorPos(1,1)
 print("Main Menu")
 print("")
 print("Enter edit, copy or new to edit, copy or create a new program.")
 local input = io.read()
 while input ~= "edit" and input ~= "copy" and input ~= "new" and input ~= "shell" do
  print(input.." is not a valid command.")
  input = io.read()
 end
 if input == "edit" then edit()
 elseif input == "copy" then copy()
 elseif input == "new" then new() 
 elseif input == "shell" then shell.run("fg shell") 
 end
end

CometWolf #2
Posted 30 May 2014 - 05:46 PM
You overwrite the copy function with a string within itself

  copy =tostring(io.read())
Use local variables.
vargaszm #3
Posted 31 May 2014 - 12:35 AM
i haven't programmed since TI basic in high school and I'm relearning to develop this, would you mind elaborating? i understand local variables but does that mean my check will be


 input = tostring(io.read())
 end
 if input == "edit" then edit()
 elseif input == "copy" then copy()
 elseif input == "new" then new() 

?

i'll have to test that later, thanks for replying.
CometWolf #4
Posted 31 May 2014 - 12:39 AM
errr no, it does not. You store your copy function in the variable "copy", however when you run it you overwrite the function stored in "copy" with a string, in the line i posted. Changing the copy variable defined within the function to local, would prevent it from overwriting the function variable which is defined globally.
vargaszm #5
Posted 31 May 2014 - 03:34 PM
thanks, now i understand. I changed the internal copy variable to a local variable copyName.