3 posts
Posted 07 October 2015 - 05:48 PM
Hi,
I've decided to make my own BigReactors reactor control program(even though there is a lot of better and already prepared programs out there, I like the DIY attitude). Nevertheless, I got stuck right at the beginning, when I was just testing, if my peripherals are connected and wrapped correctly. Probably its because of some banality, but I am quite new to lua and I wasn't able to figure out, where the problem is.
local reactor=peripheral.wrap("BigReactors-Reactor_2")
local monitor=peripheral.wrap("left")
print(reactor.getConnected())
monitor.clear()
monitor.setCursorPos(2,2)
monitor.write(reactor.getConnected())
I am getting "attempt to call nil" error on the last row and I really can't see why.
Advanced monitor 4x3 is connected to the left obviously, BigReactors reactor is connected via wired modems and reactor computer port.
Any advice? Thanks.
Edited on 08 October 2015 - 09:31 AM
7083 posts
Location
Tasmania (AU)
Posted 07 October 2015 - 10:48 PM
If you hadn't wrapped the monitor, then "monitor" would be nil, instead of a table. This'd generate an "attempt to index nil" when you attempted to reference functions inside it.
The monitor is wrapped, so you can index into "monitor", but "serCursorPos" isn't in that table and can't be called (hence "attempt to call nil").
3 posts
Posted 08 October 2015 - 11:38 AM
Thanks for reply. Sorry for misleading code, "serCursorPos" was just typo here :)/> Fixed. Anyways, as I said before, I am getting "attempt to call nil" error on the last row
monitor.write(reactor.getConnected())
Any thoughts?
8543 posts
Posted 08 October 2015 - 05:46 PM
Please copy and paste the code you're actually using, and provide the full text of the error message. We can't find bugs in code we can't see.
212 posts
Location
Somewhere in this dimension... I think.
Posted 08 October 2015 - 08:33 PM
More than likely reactor.getConnected() is not a function, though without the full error code I cannot confirm.
7083 posts
Location
Tasmania (AU)
Posted 08 October 2015 - 10:55 PM
It's called earlier in the snippet (where it's printed), so you'd think it to be a valid function. Likewise, "monitor" appears to be a wrapped monitor.
So I can't see how the provided code could produce the stated error. It'd indeed be worth double checking that it matches the code you're actually trying to run, and that the error points to the line that you think it does.
3 posts
Posted 11 October 2015 - 11:39 AM
ok…doublechecked, the code I posted here is exactly the same as in game…and the result is
true
ReactorControl:7: attempt to call nil
I thought it would be some banality since I didn't see any obvious mistake in the code…but now I am getting a bit frustrated
7083 posts
Location
Tasmania (AU)
Posted 11 October 2015 - 12:33 PM
What's the full output of this?:
local reactor=peripheral.wrap("BigReactors-Reactor_2")
local monitor=peripheral.wrap("left")
print(reactor.getConnected())
print(peripheral.getType("left"))
print(type(monitor.write))
print(type(reactor.getConnected))
monitor.write(
reactor.getConnected())
Edited on 11 October 2015 - 10:34 AM
209 posts
Location
Denmark
Posted 11 October 2015 - 12:46 PM
i just tested your code in a creative world..
it should not throw any errors at all (copied it directly from your post)
is your monitor connected with a modem or is it directly attached to the computer??
1852 posts
Location
Sweden
Posted 11 October 2015 - 01:55 PM
I'm just wondering, why don't you do this?
Example
These following examples automatically finds and wraps a monitor, if no monitor is found it throws an error
CC 1.6+
local monitor = peripheral.find( "monitor" )
if not monitor then
error( "No monitor attached", 0 )
end
Older Versions of CC
local monitor
for _, name in ipairs( peripheral.getNames() ) do --# Loop through all peripherals attached
if peripheral.getType( name ) == "monitor" then --# Check if the peripheral was a monitor
monitor = peripheral.wrap( name ) --# Wrap it
break
end
end
if not monitor then
error( "No monitor attached", 0 )
end
And I'd suggest you try this to debug, it will show if you can find the index "getConnected"
if reactor then
print( reactor.getConnected and reactor.getConnected() or "Failed to find index 'getConnected'" )
else
print( "No reactor attached", 0 )
end
Edited on 11 October 2015 - 11:55 AM
196 posts
Location
Hiding in Online Storage
Posted 14 October 2015 - 07:12 PM
If you are using the code you supplied, it is because you are calling
local reactor=peripheral.wrap("BigReactors-Reactor_2")
rather than
local reactor=peripheral.find("BigReactors-Reactor_2")
.
212 posts
Location
Somewhere in this dimension... I think.
Posted 14 October 2015 - 09:28 PM
If you are using the code you supplied, it is because you are calling
local reactor=peripheral.wrap("BigReactors-Reactor_2")
rather than
local reactor=peripheral.find("BigReactors-Reactor_2")
.
The reactor is connected via a wired modem. The peripheral name is BigReactors-Reactor and it is the 3rd so BigReactors-Reactor_2
Edited on 14 October 2015 - 07:28 PM
7083 posts
Location
Tasmania (AU)
Posted 14 October 2015 - 10:33 PM
Or rather, the peripheral
type would be along the lines of "BigReactors-Reactor".
peripheral.find() requires types, not names/sides, Selim, so your example won't "find" a peripheral at all.
Besides which, we've already established that the reactor is being wrapped correctly.