Jump to content




pcall() with arguments


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

#1 flaghacker

  • Members
  • 655 posts

Posted 23 May 2014 - 10: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 by flaghacker, 23 May 2014 - 10:20 AM.


#2 Wojbie

  • Members
  • 631 posts
  • LocationKinda lost

Posted 23 May 2014 - 10: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 by wojbie, 23 May 2014 - 12:39 PM.


#3 MKlegoman357

  • Members
  • 1,170 posts
  • LocationKaunas, Lithuania

Posted 23 May 2014 - 10:46 AM

View Postwojbie, on 23 May 2014 - 10:42 AM, said:

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.


#4 Wojbie

  • Members
  • 631 posts
  • LocationKinda lost

Posted 23 May 2014 - 12:38 PM

View PostMKlegoman357, on 23 May 2014 - 10:46 AM, said:

View Postwojbie, on 23 May 2014 - 10:42 AM, said:

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

#5 flaghacker

  • Members
  • 655 posts

Posted 23 May 2014 - 03:26 PM

Thanks!

#6 theoriginalbit

    Semi-Professional ComputerCrafter

  • Moderators
  • 7,332 posts
  • LocationAustralia

Posted 23 May 2014 - 03:29 PM

View Postwojbie, on 23 May 2014 - 12:38 PM, said:

: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.

#7 Wojbie

  • Members
  • 631 posts
  • LocationKinda lost

Posted 23 May 2014 - 03:40 PM

View Posttheoriginalbit, on 23 May 2014 - 03:29 PM, said:

View Postwojbie, on 23 May 2014 - 12:38 PM, said:

-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





1 user(s) are reading this topic

0 members, 1 guests, 0 anonymous users