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

[solved]help! peripherals listMethods not working correctly, works in lua but not in actual code

Started by CreeperGoBoom, 23 November 2016 - 07:39 AM
CreeperGoBoom #1
Posted 23 November 2016 - 08:39 AM
ok so im trying to create a program that will document anything i want using a turtle

for each peripheral i am having problems with, listMethods shows up in getMethods
in lua listmethods works fine, but in my code it returns the error: attempt to index ? a nil value

why would it work in lua but not in code for the same code? [confused]

the error randomly gets thrown for both line 36 and 39

here is the code

http://pastebin.com/uNZDxbNJ

this is driving me absolutely nuts, any help is appreciated, cheers!!
Edited on 23 November 2016 - 08:13 AM
Bomb Bloke #2
Posted 23 November 2016 - 09:06 AM
You're defining "device" at a time when "side" is "" - so wrapping fails, and "device" ends up as nil. You hence can't index into it to look for a "listMethods" function, as the error tells you.

Best to forget about listMethods anyway, mind you. There's no guarantee all peripherals will offer it, and peripheral.getMethods() already does the job.
CreeperGoBoom #3
Posted 23 November 2016 - 09:09 AM
thanks for the quick answer Bomb Bloke, but im looking for the additional info that listMethods provides such as the parameters for each method, getMethods only lists the methods without parameters, any ideas on how to accomplish this?

edit: nvm i just removed the local side = ""
and moved the corresponding local peripheral wraps to just below side = read()

for anyone who was having my problem here is the fixed code

Spoilerlocal function setstr(file,input) –writes to file from a variable
local f=fs.open(file,"a")
f.writeLine(input)
f.close()
end

local function mkfile(file) –creates a new file, deletes original first
shell.run("delete "..file)
local f=fs.open(file,"w")
f.close()
end

term.clear()
term.setCursorPos(1,1)
print("What side am I getting methods?")
local side = read()
local p = peripheral
local device = p.wrap(side)

for i,k in ipairs(p.getMethods(side)) do

print(i..":"..k)

end

local name = p.getType(side)
local fname = name..".txt"

if not fs.exists(fname) then
mkfile(fname)
for i,k in ipairs(p.getMethods(side)) do
setstr(fname,k)
end
setstr(fname,device.listMethods())
end

print(device.listMethods())

Cheers @Bomb Bloke [thumbsup]
you my friend are seriously one of the best coders I know
Edited on 23 November 2016 - 08:15 AM