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

pcall() with arguments

Started by flaghacker, 23 May 2014 - 09:19 AM
flaghacker #1
Posted 23 May 2014 - 11:19 AM
I have made a program that uses the PIM from openperipheraladdons to clear all snowballs from a player's inventory for a spleef area. When the player stands on top of the PIM it loops trough his inventory and clears all the snowballs from his inventory. This is a decently fast process, but occasionally the player walks of the PIM before the program is done. The program crashes because the function "pim.deleteStack(i)" with "i" being the current inventory, can't find the player. I tried to fix this with pcall() but I can't figure out how to give the called function the parameneter i.
Edited on 23 May 2014 - 09:20 AM
Wojbie #2
Posted 23 May 2014 - 11:42 AM
Fast ansfer from a phone sorry for no formating.
You warp it with anonymus function like this:
pcall(function() pim.deleteStack(i) end)


EDIT: Or you do it correct way as told in post below mine.
Edited on 23 May 2014 - 11:39 AM
MKlegoman357 #3
Posted 23 May 2014 - 11:46 AM
Fast ansfer from a phone sorry for no formating.
You warp it with anonymus function like this:
pcall(function() pim.deleteStack(i) end)

Ah, yet another anonymous-function-to-pass-parameters-to-your-function-using-pcall. pcall has this functionality, you don't need anonymous functions:


--// Usage: pcall( function_pointer, parameter1, parameter2, ...)

pcall(term.setCursorPos, 1, 1) --// Resets the cursor position.
Wojbie #4
Posted 23 May 2014 - 01:38 PM
Fast ansfer from a phone sorry for no formating.
You warp it with anonymus function like this:
pcall(function() pim.deleteStack(i) end)

Ah, yet another anonymous-function-to-pass-parameters-to-your-function-using-pcall. pcall has this functionality, you don't need anonymous functions:


--// Usage: pcall( function_pointer, parameter1, parameter2, ...)

pcall(term.setCursorPos, 1, 1) --// Resets the cursor position.

:o/> The pill has told me wrong way? http://www.lua.org/pil/8.4.html :(/> Guess you learn something new/correct every day.
Thank you for correcting me :D/>
flaghacker #5
Posted 23 May 2014 - 04:26 PM
Thanks!
theoriginalbit #6
Posted 23 May 2014 - 04:29 PM
:o/> The pill has told me wrong way? http://www.lua.org/pil/8.4.html :(/> Guess you learn something new/correct every day.
Thank you for correcting me :D/>
the PIL says nothing about not being able to work with arguments, it states that you can use anonymous functions.
Wojbie #7
Posted 23 May 2014 - 04:40 PM
-snip-
the PIL says nothing about not being able to work with arguments, it states that you can use anonymous functions.
True. I guess i need to reread whole documentation again instead of relaying on stuff i read in pill. Wierd thing to do for language you only use for fun not for work :P/>