Jump to content




Calling function


  • You cannot reply to this topic
2 replies to this topic

#1 LuffyNL

  • Members
  • 16 posts

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)

#2 Agoldfish

  • Members
  • 451 posts
  • LocationSome Fish Bowl in Ohio.

Posted 21 March 2014 - 06:57 PM

I think it will be something like:
while true do
checkKey()
end


#3 TheOddByte

    Lazy Coder

  • Members
  • 1,607 posts
  • LocationSweden

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






2 user(s) are reading this topic

0 members, 2 guests, 0 anonymous users