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

Calling function

Started by LuffyNL, 21 March 2014 - 05:49 PM
LuffyNL #1
Posted 21 March 2014 - 06:49 PM

m = peripheral.wrap("top")
keyItem = "Lever"

function emptyPlayer()
    for i = 28,36 do
    m.pushItem("west",i,64)
  end
end

function checkKey()
local slotInfo = m.getStackInSlot(10)
if slotInfo ~= nil then
  if slotInfo.name == keyItem then
	   print("Key Found")
	  end
   end
  end

I wrote this little piece of code with a Player Interface from the Random Things mod so that whenever I have a lever in the top left of my inventory it will dump all the items from the row above my hotbar into a chest.
Now I want to call these two functions in a loop where it constanly checks if I have the key in that slot, and if I do it calls the emptyPlayer function. I don't however know how to put the checkKey in an if statement (If that is even the way to do it)
Agoldfish #2
Posted 21 March 2014 - 06:57 PM
I think it will be something like:

while true do
checkKey()
end
TheOddByte #3
Posted 21 March 2014 - 07:05 PM
You could also remove the uneccessary variable at the top and use it like an argument in your function

local function checkKey( item )
    local slotInfo = m.getStackInSlot( 10 )
    if slotInfo and slotInfo == item then -- Checks that it isn't nil and if it's equal to the keyitem
        print( "Key Found!" )
        return true
    end
end
And use it like this

while true do
    local bool = checkKey( "Lever" )
    if bool then
        emptyPlayer()
    end
end