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

[Lua][Error]/[Question] How do i unload a File?

Started by kelevra.1337, 04 January 2013 - 09:36 AM
kelevra.1337 #1
Posted 04 January 2013 - 10:36 AM
Hello guys, kelevra here again

I just finished a restart-resistant goto skript and apart from the fact that it leaves a little file with serialized data im pretty happy with it.
Everytime i try to delete it within/while the skript is running with fs.delete() it gives out the error message "Access denied". My question is now: how can i unload this file and delete it without leaving the skript?

The second problem i got is this little piece of code from my api:



statedata = loadData("state")								loadData unserializes a table and returns it

if statedata["state"] == "running" then
  print("FIX THIS ALREADY")

  shell.run(statedata["program"])							 Error: try to index a nil value
end


-----------------
{["state"]="running",["program"]="goto",}			   this is the serialized data
-----------------


I tried many variants of tostringing it or putting it in a variable first but nothing helped so far.
whats especially strange about this is that this part works:


if statedata["state"] == "error" then
  print("OH NOEZ, SOMETHING CRASHED in the "..statedata["program"].." skript")
  statedata = {state = "idle", program = "none"}
end

I hope i dont spam the forum too much with my problems and i know im a little late but: HAPPY NEWYEAR! (and thanks :)/> )
Bubba #2
Posted 04 January 2013 - 10:59 AM
Are you closing the file before trying to delete it? If not then it will give you that error message. As for the other issue, could you put the entire code up so that we can see what's going on with the loadData function as well? I don't see any errors in the code you posted. Are you sure that the error is not in the "goto" program?
MysticT #3
Posted 04 January 2013 - 11:04 AM
The problem with the api is that you can't use the shell api there. Apis are loaded in an environment where the shell api table is not defined, so it doesn't exist (it's nil, that's why it says attempt to index a nil value).
Bubba #4
Posted 04 January 2013 - 11:06 AM
The problem with the api is that you can't use the shell api there. Apis are loaded in an environment where the shell api table is not defined, so it doesn't exist (it's nil, that's why it says attempt to index a nil value).

Ah, didn't even notice that it was an API. Is it possible to access api's through _G even during the load process? I'm unable to test right now but I doubt it is.
kelevra.1337 #5
Posted 04 January 2013 - 11:13 AM
Well, this got very confusing right now because i can access the fs. api within my api but not the shell. api(?)

Here is the code in the api for saving tables

function saveData(table,name)
local file = fs.open(tostring(name),"w")
file.write(textutils.serialize(table))
file.close()
end
function loadData(name)
if fs.open(tostring(name),"r") == nil then
  return false
else
  local file = fs.open(tostring(name),"r")
  local data = file.readAll()
  file.close()
  return textutils.unserialize(data)
end
end

And Thanks for the fast help guys
Lyqyd #6
Posted 04 January 2013 - 11:19 AM
The shell API isn't actually an API, unlike the others.
kelevra.1337 #7
Posted 04 January 2013 - 11:22 AM
Ah ok,

Is there any other way to run programs from within an api or do i have to make the statusloading a startup skript?
Bubba #8
Posted 04 January 2013 - 11:25 AM
Ah ok,

Is there any other way to run programs from within an api or do i have to make the statusloading a startup skript?

Does dofile() work for you?
kelevra.1337 #9
Posted 04 January 2013 - 11:29 AM
Yes it does thank you very much.
Do any of the turtle/CC apis work in other apis or only the standard lua ones?
MysticT #10
Posted 04 January 2013 - 11:42 AM
You can use any loaded api, just that the shell api is not a common loaded api, it is loaded in the shell environment, wich is different from the global one used to load apis.
kelevra.1337 #11
Posted 04 January 2013 - 12:11 PM
Ah ok, i hopen then i'll just never need any non loaded api in my api, and once a again thank you for taking the time to answer.
I really appreciate your and anyone elses help in the forum.
Can you just give me one last little hint about the fs.delete() thing i mentioned in my first post?