This is a read-only snapshot of the ComputerCraft forums, taken in April 2020.
yannickkutil's profile picture

Peripheral error

Started by yannickkutil, 03 September 2015 - 12:35 PM
yannickkutil #1
Posted 03 September 2015 - 02:35 PM
Problem is resolved – thanks
Bomb Bloke


Hey,

the code only works once the method Laden().
then break the Code with error = attempt to index ? (a nil value)
i habe no idea, what the problem is



local kiste = peripheral.wrap("back")

function KistenStatus()
ZahlPlus = 1
ZahlOnline = 0
while true do
if ZahlPlus == getVolumen() then return ZahlOnline end
ZahlOnline = ZahlOnline + kiste.getStackInSlot(ZahlPlus).qty or 0
ZahlPlus = ZahlPlus + 1
end end
function getVolumen()
Volume = kiste.getInventorySize()
return tonumber(Volume)
end
function Clear()
print("Kiste ist bereit EXPORT!")
rs.setOutput("right",true)
while true do
if KistenStatus() == 0 then rs.setOutput("right",false) Laden() end
os.sleep(0.1)
end end
function Laden()
print("Kiste ist bereit IMPORT!")
rs.setOutput("left",true)
while true do
if KistenStatus() == getVolumen()-4 then  rs.setOutput("left",false) Clear() end
os.sleep(0.1)
end end
rs.setOutput("right",false)
rs.setOutput("left",false)
term.setBackgroundColor(colors.black)
term.setTextColor(colors.green)
shell.run("clear")
--Laden()
print(KistenStatus())
--print(textutils.serialize(kiste.getStackInSlot(ZahlPlus).qty))
Edited on 04 September 2015 - 07:40 AM
MindenCucc #2
Posted 03 September 2015 - 03:30 PM
The problem is that you're calling the Laden function before it's even defined. I would suggest moving the Laden function higher, so it exists before you try to call it.

Edit: or after kiste = periperal.wrap("back"), you can do

local Laden
so the reference exists. You get the error because Lua executes the file as it is, so it doesn't pre-load the variables and functions like other programming languages do.
Edited on 03 September 2015 - 01:37 PM
Bomb Bloke #3
Posted 03 September 2015 - 03:34 PM
Er, no he's not? Laden() is defined in the global scope by the time a call is attempted. Granted, it'd ideally be in the local space, but that's not going to trigger an attempt to index nil.

But if kiste.getStackInSlot(ZahlPlus) is nil (because there's nothing in slot ZahlPlus, for example), then you can't index into it with a qty key (because nil is not a table).

Try using "kiste.getStackInSlot(ZahlPlus) and kiste.getStackInSlot(ZahlPlus).qty or 0". Or better yet, saving the result of kiste.getStackInSlot(ZahlPlus) into a variable then checking that (so you don't have to call it twice per slot).
yannickkutil #4
Posted 03 September 2015 - 05:51 PM
First thanks for the help
I now Revision my code
The result is Unfortunately, it is not still.The error is the same.



local Chest = peripheral.wrap("back")


*Chest.getStackInSlot(SchleifeRepeat)*  return


{mod_id=minecraft,
raw_name=tile.dirt.default,
max_dmg=0.0,
qty=2.0,
name=dirt,
id=minecraft:dirt,
display_name=Dirt,
max_size=64.0,
dmg=0.0,
ore_dict={}}



*TheWerterF1.qty*

return === nil


function getChestState() -- Return:Number ..:.. Wie Viele Slots sind Frei in der Kiste ? | How Many slots a free in the Chest ?
local SchleifeRepeat = 1
local HoManyIsFree = 0
while true do
TheWerterF1 = Chest.getStackInSlot(SchleifeRepeat)
TheWerterF2 = TheWerterF1.qty
if SchleifeRepeat == getVolumen() then return HoManyIsFree end
if not TheWerterF2 == nil then
HoManyIsFree = HoManyIsFree + TheWerterF2
end
SchleifeRepeat = SchleifeRepeat + 1
end end


function getVolumen() -- Return:Number ..:.. Wie groß ist die Kiste Formel: 1 Slot = 64 Stack |How big is the box Formula 1 Slot = 64 Stack
return Chest.getInventorySize()/64
end
function Clear() -- Void -- instance of Main Loop
print("Chest ist bereit EXPORT!")
rs.setOutput("right",true)
while true do
if getChestState() == 0 then rs.setOutput("right",false) Laden() end
os.sleep(0.1)
end end


function Laden() -- Void- - instance of Main Loop
print("Chest ist bereit IMPORT!")
rs.setOutput("left",true)
while true do
if getChestState() == getVolumen()-4 then  rs.setOutput("left",false) Clear() end
os.sleep(0.1)
end end

-- Start Code


rs.setOutput("right",false)
rs.setOutput("left",false)
term.setBackgroundColor(colors.black)
term.setTextColor(colors.green)
shell.run("clear")


-- Normaly Start

Laden()

-- Debug Test

--print(KistenStatus())

Edited on 03 September 2015 - 06:51 PM
Bomb Bloke #5
Posted 04 September 2015 - 01:31 AM
TheWerterF1 = Chest.getStackInSlot(SchleifeRepeat)  -- If the slot is empty, then TheWerterF1 becomes nil.
TheWerterF2 = TheWerterF1.qty                       -- You can't index into nil with the qty key.

So:

TheWerterF1 = Chest.getStackInSlot(SchleifeRepeat)

if TheWerterF1 then              -- if TheWerterF1 is not nil,
  TheWerterF2 = TheWerterF1.qty  -- then it must be a table, so you can index into it.
else
  TheWerterF2 = 0                -- otherwise just use 0.
end

Or, to abbreviate the same code:

TheWerterF1 = Chest.getStackInSlot(SchleifeRepeat)
TheWerterF2 = TheWerterF1 and TheWerterF1.qty or 0
Edited on 03 September 2015 - 11:33 PM