lua problems in computercraft/minecraft. possibly global variable related

Soldato
Joined
15 Feb 2011
Posts
10,234
Location
Slough
i've been playing around with the computercraft mod for minecraft, where you can get a moveable computer that can do stuff in minecraft. it uses the lua language with a couple of bits bolted on so i figured someone might know what i'm doing wrong

i am using the fs API (designed for computercraft i believe) to update a file with details about the task it is doing and the position it is in, so that the turtle can resume doing what it was doing from where it left off if the server is restarted.

i am getting an 'attempted to call nil' error on line 23, despite using global variables for everything in the function. (i've tried it with and without global before the variables). there is already a file written in the correct place with the correct name with ten 0's as data to read

code: http://pastebin.com/H1rYTECU

here is the guide i have been following for using the fs API to get data stored to a file. I have used it with local variables in another program and it has worked just fine: http://www.computercraft.info/forums2/index.php?/topic/3923-basic-file-io-learn-how-to-save-data/

and this is the computercraft wiki: http://computercraft.info/wiki/Main_Page
if there is anything else you dont recognise in the code then it is probably in there (the turtle API page is probably worth a look, since i use it a lot)


any help is much apprecaited
 
The handle for the file your opening isn't being allocated to a variable anywhere for later referencing.

Line 22 should be something like "local file = fs.open("quarryData/","w")"

If your initialising file as a global variable it should still look like "file = fs.open("quarryData/","w")".


EDIT: To add to that I don't reccomend declaring file handles globally unless you have a system in place for handling multiple IO streams or opening one file throughout the whole program i.e. a logfile. Keep incidental file writing/reading local to its own function as much as possible.

OMG, i cant believe i missed that. I expect thats the problem so i'll see if it fixes it tomorrow.

thanks very much for the help :)
 
Thanks again for the help, that was exactly what was causing my problems. a few other bugfixes later and i have a working turtle that can recover from being turned off
 
Back
Top Bottom