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

Getting nil trying to use the peripheral api

Started by Yurij, 13 June 2013 - 09:06 AM
Yurij #1
Posted 13 June 2013 - 11:06 AM
Title: Getting nil trying to use the peripheral api


write("Choose side: ")
side = read()
if peripheral.isPresent(side) then
end

While trying this code I am getting some wonky behaviour.
Most of the time I get the "attempt to call nil" error on the isPresent call, but sometimes it works :huh:/>

Writing it in the lua promt works fine
Symmetryc #2
Posted 13 June 2013 - 01:39 PM
I don't think this is the case, but do you have "side" defined as a global variable somewhere else?
Engineer #3
Posted 13 June 2013 - 02:16 PM
I don't think this is the case, but do you have "side" defined as a global variable somewhere else?
That should not matter, because he is redefining it.

Try this right here:

local side = nil
local sides = rs.getSides()
repeat
    write("Choose side: ")
    side = read():lower() -- To make sure its all lowered case, wich matters
until sides[side] -- See what I did here? This is a reference to TheOriginalBIT :P/>
if peripheral.isPresent( side ) then
end

This code keeps asking until you have entered a valid side.
Symmetryc #4
Posted 13 June 2013 - 02:22 PM
I don't think this is the case, but do you have "side" defined as a global variable somewhere else?
That should not matter, because he is redefining it.
Yeah, I didn't think that it was the case :/.

IIRC, peripheral.isPresent() doesn't error with attempt to call nil, even if the side isn't valid, though.
Yurij #5
Posted 13 June 2013 - 07:01 PM
I don't think this is the case, but do you have "side" defined as a global variable somewhere else?
That should not matter, because he is redefining it.

Try this right here:

local side = nil
local sides = rs.getSides()
repeat
	write("Choose side: ")
	side = read():lower() -- To make sure its all lowered case, wich matters
until sides[side] -- See what I did here? This is a reference to TheOriginalBIT :P/>/>
if peripheral.isPresent( side ) then
end

This code keeps asking until you have entered a valid side.

That code isn't working for me. It just loops.
Engineer #6
Posted 13 June 2013 - 07:11 PM
What are you putting in? The sides are:

left
right
top
bottom
front
back

Edit: oops, just realised.. Try this instead:

local sides = {}
for k, v in pairs(rs.getSides()) do
  sides[v] = true
end
local side = nil
repeat
   side = read():lower()
until sides[side]
if peripheral.isPresent( side ) then
   print('hooray!')
end
theoriginalbit #7
Posted 14 June 2013 - 12:05 AM
Edit: oops, just realised.. Try this instead:

local sides = {}
for k, v in pairs(rs.getSides()) do
  sides[v] = true
end
local side = nil
repeat
   side = read():lower()
until sides[side]
if peripheral.isPresent( side ) then
   print('hooray!')
end
Much better, I was just about to point that out :P/>
BTW you have to admit that a lookup is much better than a looping.
Bomb Bloke #8
Posted 14 June 2013 - 04:30 AM
Is that really the case? Doesn't Lua have to perform a loop in order to perform the lookup anyway?

Though I sorta suspect it does that regardless. Single loop versus a double loop? Dunno.
theoriginalbit #9
Posted 14 June 2013 - 06:59 AM
Is that really the case? Doesn't Lua have to perform a loop in order to perform the lookup anyway?
Though I sorta suspect it does that regardless. Single loop versus a double loop? Dunno.
Tables, in LuaJ, are HashMaps, so no Lua does not have to perform a loop to perform a lookup.
Bomb Bloke #10
Posted 14 June 2013 - 08:44 AM
Ah, understanding hashmaps explains an awful lot about a lot of things… thanks!
Yurij #11
Posted 14 June 2013 - 06:26 PM
What are you putting in? The sides are:

left
right
top
bottom
front
back

Edit: oops, just realised.. Try this instead:

local sides = {}
for k, v in pairs(rs.getSides()) do
  sides[v] = true
end
local side = nil
repeat
   side = read():lower()
until sides[side]
if peripheral.isPresent( side ) then
   print('hooray!')
end

Thanks.
Looks like it's working now :D/>