Jump to content




Error catching without ending


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

#1 ShadowDisruptor

  • Members
  • 74 posts

Posted 17 September 2014 - 09:04 PM

My code (found in the spoiler below) is meant to basically do what find does (Sort peripherals) in a version of computercraft that dosen't have peripheral.find. I simply need to run these functions in ok, err = pcall(). When I do so, they still return the error and end the program. The goal is to have it NOT do that. If someone could work out this error or explain it to me, I'd be very grateful (Also, if you use the latest version of Tekkit main, getting it to work would be even better :) ) The error I get is 4: attempt to call nil
Spoiler

Edited by ShadowDisruptor, 17 September 2014 - 09:05 PM.


#2 TheOddByte

    Lazy Coder

  • Members
  • 1,607 posts
  • LocationSweden

Posted 17 September 2014 - 09:12 PM

I think you're using pcall wrong
For example, you have to put the function first, then the arguments
local ok, err = pcall( term.setTextColor, colors.lime ) -- separate them
So you would have todo this
local ok, err = pcall( isReactor, foundPeripherals[i] )


#3 ShadowDisruptor

  • Members
  • 74 posts

Posted 17 September 2014 - 09:25 PM

View PostTheOddByte, on 17 September 2014 - 09:12 PM, said:

I think you're using pcall wrong
For example, you have to put the function first, then the arguments
local ok, err = pcall( term.setTextColor, colors.lime ) -- separate them
So you would have todo this
local ok, err = pcall( isReactor, foundPeripherals[i] )

isReactor(foundPeripherals[i]) is the function I'm calling. foundPeripherals[i] is the input. When I tried doing what you did with the first example and the line that wasn't working, nothing happened. EDIT: I've got it now, thanks for the help!

Edited by ShadowDisruptor, 17 September 2014 - 09:28 PM.


#4 TheOddByte

    Lazy Coder

  • Members
  • 1,607 posts
  • LocationSweden

Posted 17 September 2014 - 09:35 PM

View PostShadowDisruptor, on 17 September 2014 - 09:25 PM, said:

- Snip -
No problem, I have a suggestion for you, you should use locals
local foo = "bar"
if 1 == 1 then
    local foo = "blah" -- This will be local to the statement, same goes for loops and functions
end
print( foo ) -- will output bar
If your variables isn't local they'll be accessible outside of your program, which would be very bad if you'd use variables to store credentials.

#5 Bomb Bloke

    Hobbyist Coder

  • Moderators
  • 7,099 posts
  • LocationTasmania (AU)

Posted 18 September 2014 - 01:18 AM

pcall isn't really the way to go about this... It's more for those circumstances where there will potentially be errors you can't avoid. Here, you can avoid them, by not trying to call functions which don't exist.

Currently, you're trying to call "getConnected()" once for every peripheral attached to your system, regardless as to whether they have that function. There are a couple of ways you can check to see if it's available:

One is to outright ask. In Lua, if you stick a variable on its own into a "for" statement, it'll count as true if it exists, and false if it doesn't. Eg:

if tempReactor.getConnected then    -- If there's an entry in "tempReactor" called "getConnected", then...
  return tempReactor.getConnected() -- ... run it and return the result...
else return false end               -- ... or if there isn't, just return false.

Another way, at least as far as peripherals goes, is to check the type of peripheral you've got. If you use peripheral.getType() on a BigReactor control port it'll return "BigReactors-Reactor", so we can do:

if peripheral.getType(input) == "BigReactors-Reactor" then
  return tempReactor.getConnected()
else return false end






1 user(s) are reading this topic

0 members, 1 guests, 0 anonymous users