20 posts
Posted 13 June 2013 - 03:48 PM
I am working with misc peripherals and more specifically the advanced nuclear reader that can hold 9 sensors. I know I can use the getslot() to pull information from the sensor card but I can't find if it possible to write a code that will check if new senors have been added. My goal is to write a program that will detect which slot has a sensor and pull the information off it. This will allow me to add sensor cards as I go and not have to update the program.
In short is there a function that will check all slots and return true or false if a slot as a card in it.
8543 posts
Posted 13 June 2013 - 05:14 PM
Split into new topic.
130 posts
Posted 13 June 2013 - 06:48 PM
You can loop through and check the slot against a known slot or just check the number of items in the slot.
Item count example:
for slot = 1, 16 do
if turtle.getItemCount(slot) > 0 then
-- Item(s) in slot
else
-- Empty slot
end
end
20 posts
Posted 14 June 2013 - 05:57 PM
dumb question but I am guessing I need a turtle for this function to work or can an advance comp do this too?
7083 posts
Location
Tasmania (AU)
Posted 14 June 2013 - 07:11 PM
That code is indeed intended to check the contents of a turtle's inventory. I get the impression the sensors are stored in the reader, so I'm not sure checking the turtle's inventory has any point to it here.
According to the
MiscPeripherals thread,
get(slot) returns nil if the slot is empty. Hence, if I wanted to check all nine slots from a tier 2 reader, I'd use something like:
(wrap the sensor to "nreader" or somesuch here)
for i=1,9 do
UUID,state,title,cardinfotable = nreader.get(i)
if UUID ~= nil then -- There was something in the current slot, so...
... -- .. do something with that information here.
end -- Otherwise do nothing; check the next slot.
end
There are quite a few other ways this can be done depending on what you want to do with the sensor info and when.
20 posts
Posted 15 June 2013 - 06:52 PM
Thanks bomb, thats what i as looking for!
20 posts
Posted 15 June 2013 - 07:17 PM
works perfectly but now is there any way to count how many came back having something in its slot? I tried this:
(wrap the sensor to "nreader" or somesuch here)
u = 0
for i=1,9 do
UUID,state,title,cardinfotable = nreader.get(i)
if UUID ~= nil then
print(something)
u = u + 1
else u = u
end
end
print(u.."slot in use"
never mind had a typo somewhere
Edited on 15 June 2013 - 05:20 PM
7083 posts
Location
Tasmania (AU)
Posted 15 June 2013 - 07:23 PM
The "else u = u end" bit is redundant and should just be replaced with "end", but otherwise that looks fine.