I'm just starting out with OpenComputers. I have no problem with the coding, but there are things that don't work where I think they should work:
(1) I'm trying to write a component list to a text file, since the number of components is too large to fit on a screen. I've written this code:
local co=require("component") local fs=require("filesystem") local f=fs.open("textfile.txt","w") --print(f) (commented test line)
for k,v in co.list() do print(k,v) print(f.write(k.." "..v.."\n\r")) end end
f.close()
The file does get opened with the fs.open statement - the commented test line prints a table reference instead of nil, and I find the text file in the OC subfolder of my saved world - but when I try to write with the f.write statement in the loop I get a nil result and the error message "file is closed". For the same reason, the f.close() gives me a an error message. The same happens if I add the test line "print(f.write("test"))" directly after the open statement. What am I doing wrong?
(2) Certain ReactorCraft/RotaryCraft blocks have a method "readtank(<tank index)". This method returns an array with the name/designated fluid type of the tank, its capacity and the amount of liquid in it as its elements, so something like "water 8000 8000" for a full tank of 8B capacity full of water. I can print the complete array with print(readtank(0)), for instance, but I can't access the elements of the array, for instance with
c = readtank(0)
print(c[0])
How do I access the elements of an array returned by a function correctly? print((readtank(0))[0]) doesn't work either.
(3) While I'm at it: Is there a way to route the output of a program to a text file? That way I needn't bother with the filesystem code.
You can post now and register later.
If you have an account, sign in now to post with your account.
Note: Your post will require moderator approval before it will be visible.
Hi everyone,
I'm just starting out with OpenComputers. I have no problem with the coding, but there are things that don't work where I think they should work:
(1) I'm trying to write a component list to a text file, since the number of components is too large to fit on a screen. I've written this code:
local co=require("component")
local fs=require("filesystem")
local f=fs.open("textfile.txt","w")
--print(f) (commented test line)
for k,v in co.list() do
print(k,v)
print(f.write(k.." "..v.."\n\r"))
end
end
f.close()
The file does get opened with the fs.open statement - the commented test line prints a table reference instead of nil, and I find the text file in the OC subfolder of my saved world - but when I try to write with the f.write statement in the loop I get a nil result and the error message "file is closed". For the same reason, the f.close() gives me a an error message. The same happens if I add the test line "print(f.write("test"))" directly after the open statement. What am I doing wrong?
(2) Certain ReactorCraft/RotaryCraft blocks have a method "readtank(<tank index)". This method returns an array with the name/designated fluid type of the tank, its capacity and the amount of liquid in it as its elements, so something like "water 8000 8000" for a full tank of 8B capacity full of water. I can print the complete array with print(readtank(0)), for instance, but I can't access the elements of the array, for instance with
c = readtank(0)
print(c[0])
How do I access the elements of an array returned by a function correctly? print((readtank(0))[0]) doesn't work either.
(3) While I'm at it: Is there a way to route the output of a program to a text file? That way I needn't bother with the filesystem code.
Thanks in advance for any help.
Link to post
Share on other sites