Jump to content




Getting nil trying to use the peripheral api


  • You cannot reply to this topic
10 replies to this topic

#1 Yurij

  • Members
  • 18 posts

Posted 13 June 2013 - 10: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

#2 Symmetryc

  • Members
  • 434 posts

Posted 13 June 2013 - 12:39 PM

I don't think this is the case, but do you have "side" defined as a global variable somewhere else?

#3 Engineer

  • Members
  • 1,378 posts
  • LocationThe Netherlands

Posted 13 June 2013 - 01:16 PM

View PostSymmetryc, on 13 June 2013 - 12:39 PM, said:

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.

#4 Symmetryc

  • Members
  • 434 posts

Posted 13 June 2013 - 01:22 PM

View PostEngineer, on 13 June 2013 - 01:16 PM, said:

View PostSymmetryc, on 13 June 2013 - 12:39 PM, said:

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.

#5 Yurij

  • Members
  • 18 posts

Posted 13 June 2013 - 06:01 PM

View PostEngineer, on 13 June 2013 - 01:16 PM, said:

View PostSymmetryc, on 13 June 2013 - 12:39 PM, said:

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.

#6 Engineer

  • Members
  • 1,378 posts
  • LocationThe Netherlands

Posted 13 June 2013 - 06: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


#7 theoriginalbit

    Semi-Professional ComputerCrafter

  • Moderators
  • 7,332 posts
  • LocationAustralia

Posted 13 June 2013 - 11:05 PM

View PostEngineer, on 13 June 2013 - 06:11 PM, said:

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.

#8 Bomb Bloke

    Hobbyist Coder

  • Moderators
  • 7,099 posts
  • LocationTasmania (AU)

Posted 14 June 2013 - 03: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.

#9 theoriginalbit

    Semi-Professional ComputerCrafter

  • Moderators
  • 7,332 posts
  • LocationAustralia

Posted 14 June 2013 - 05:59 AM

View PostBomb Bloke, on 14 June 2013 - 03:30 AM, said:

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.

#10 Bomb Bloke

    Hobbyist Coder

  • Moderators
  • 7,099 posts
  • LocationTasmania (AU)

Posted 14 June 2013 - 07:44 AM

Ah, understanding hashmaps explains an awful lot about a lot of things... thanks!

#11 Yurij

  • Members
  • 18 posts

Posted 14 June 2013 - 05:26 PM

View PostEngineer, on 13 June 2013 - 06:11 PM, said:

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





2 user(s) are reading this topic

0 members, 2 guests, 0 anonymous users