Posted 01 February 2013 - 10:12 AM
Title: [Lua] API help: Doesn't work when loaded, but works when run.
I'm trying to write a peripheral detector & wrapper. When I run this code (typing `hardware` at the CraftOS prompt), it works as expected.
However, if I open up the interactive Lua interpreter(`lua` at the CraftOS prompt) and type `os.loadAPI("hardware")`, all of the tables are populated correctly, except for the singular `modem` table. All other singular peripheral tables are generated properly.
Does anyone know the cause of this?
Note: I'm running the latest Recommended builds of Forge and CC, with no other mods. MC 1.4.7
sides
hardware
I'm trying to write a peripheral detector & wrapper. When I run this code (typing `hardware` at the CraftOS prompt), it works as expected.
However, if I open up the interactive Lua interpreter(`lua` at the CraftOS prompt) and type `os.loadAPI("hardware")`, all of the tables are populated correctly, except for the singular `modem` table. All other singular peripheral tables are generated properly.
Does anyone know the cause of this?
Note: I'm running the latest Recommended builds of Forge and CC, with no other mods. MC 1.4.7
sides
left = "left"
right = "right"
front = "front"
back = "back"
bottom = "bottom"
top = "top"
hardware
os.loadAPI("sides") -- My convenience API, because I hate quotation marks (also allows iteration through sides). Exported as "sides" table.
-- Found out that I could just use redstone.getSides(), but I already wrote this, so I'll keep it.
modems = {number = 0}
computers = {number = 0}
turtles = {number = 0}
drives = {number = 0}
monitors = {number = 0}
printers = {number = 0}
local env = getfenv() -- Grab global environment. Everything else will edit this.
-- Populate the hardware tables.
for k, v in pairs(sides) do -- Iterate through all sides of the computer.
if peripheral.isPresent(v) == true then -- Check if a peripheral is attached. (either k or v could have been used here).
local peripheralType = peripheral.getType(v) -- Find out what type it is.
local longName = peripheralType.."s" -- Add an "s" to the end of its type, resulting in the name for the table.
env[longName][v] = peripheral.wrap(v) -- Wrap the peripheral and stick it in the right table.
env[longName].number = env[longName].number + 1 -- Update the number of peripherals attached to that table. (To detect whether there
end -- are multiple attached.
end
-- Populate the singular hardware tables. (convenience)
for k,v in pairs(env) do -- Iterate through all the global hardware tables.
if type(v) == "table" then -- Keeps Lua from throwing an error when other environment junk interferes.
if v.number == 1 then -- Check if there is only one peripheral attached of the current type.
for k2,v2 in pairs(v) do -- If there is, iterate through the global table to get the peripheral
-- instance.
if type(v2) == "table" then -- Filter out the "number" key (did it this way to prevent future breakage).
local shortName = string.sub(k, 1, string.len(k) - 1) -- Strip the trailing "s" off the end of the global table name.
env[shortName] = v2 -- Create a new global table without the "s" on the end and bind it to the
-- intsance.
env[shortName].side = k2 -- Add a "side" key for convenience.
end
end
end
end
end