18 posts
                
             
            
                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
 
                
             
         
        
        
            
            
                
                    
                
                500 posts
                
             
            
                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?
                
             
         
        
        
            
            
                
                    
                
                1522 posts
                
                    
                        Location
                        The Netherlands
                    
                
             
            
                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.
 
                
             
         
        
        
            
            
                
                    
                
                500 posts
                
             
            
                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.
 
                
             
         
        
        
            
            
                
                    
                
                18 posts
                
             
            
                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.
 
                
             
         
        
        
            
            
                
                    
                
                1522 posts
                
                    
                        Location
                        The Netherlands
                    
                
             
            
                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
 
                
             
         
        
        
            
            
                
                    
                
                7508 posts
                
                    
                        Location
                        Australia
                    
                
             
            
                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.
 
                
             
         
        
        
            
            
                
                    
                
                7083 posts
                
                    
                        Location
                        Tasmania (AU)
                    
                
             
            
                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.
                
             
         
        
        
            
            
                
                    
                
                7508 posts
                
                    
                        Location
                        Australia
                    
                
             
            
                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.
 
                
             
         
        
        
            
            
                
                    
                
                7083 posts
                
                    
                        Location
                        Tasmania (AU)
                    
                
             
            
                Posted 14 June 2013 - 08:44 AM
                Ah, understanding hashmaps explains an awful lot about a lot of things… thanks!
                
             
         
        
        
            
            
                
                    
                
                18 posts
                
             
            
                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/>