I'm trying to make my first "real" program, but I'm stuck.
I want to take the energy of my MFSU and send it to an other computer, but I can't take the information of energy in MFSU.
I look on lot of other program but don't know … help me please.
My program :
local modem = peripheral.wrap("right")
local mfsu = peripheral.wrap("left")
while true do
if not mfsu then
error("Cannot find mfsu attached to this computer", 0)
end
if not modem then
error("Cannot find modem attached to this computer", 0)
end
while true do
local stored = 0
stored = mfsu.getEUStored()
modem.transmit(7, 1, stored)
sleep(1)
end
end
My error:
mfsu:16: attempt to call nil
and my inspiration:
local function padLeft(str, w)
return string.rep(" ", w - #str) .. str
end
local function findPeripheral(_type)
for _,name in pairs(peripheral.getNames()) do
if peripheral.getType(name) == _type then
return peripheral.wrap(name)
end
end
end
local m = findPeripheral("monitor")
local mfsu = findPeripheral("mfsu")
local batbox = findPeripheral("batbox")
if not m then
error("Cannot find monitor attached to this computer", 0)
end
if not mfsu and not batbox then
error("Cannot find mfsu attached to this computer", 0)
end
m.setTextScale(0.5)
local w, h = m.getSize()
local total = 0
if mfsu then
total = mfsu.getEUCapacity()
elseif batbox then
total = batbox.getCapacity()
end
os.startTimer(1)
while true do
local stored = 0
if mfsu then
stored = mfsu.getEUStored()
elseif batbox then
stored = batbox.getStored()
end
m.clear()
m.setCursorPos(1, 2)
m.setTextColour(colours.orange)
m.write("Energy:")
m.setTextColour(colours.white)
m.write(padLeft(tostring(stored), w - 7))
m.setCursorPos(1, 4)
m.setTextColour(colours.orange)
m.write("Total:")
m.setTextColour(colours.white)
m.write(padLeft(tostring(total), w - 6))
local p = (w-2) * stored / total
m.setBackgroundColour(colours.lightGrey)
m.setCursorPos(2, 8)
m.write(string.rep(" ", w-2))
m.setBackgroundColour(colours.grey)
m.setCursorPos(2, 8)
m.write(string.rep(" ", p))
m.setBackgroundColour(colours.black)
os.pullEvent("timer")
os.startTimer(1)
end
Please, where is the problem ? Thank you a lot !
PS : Sorry for my really bad english –'