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

How to let your program continue after a function error

Started by Djosjowa, 09 June 2015 - 03:22 PM
Djosjowa #1
Posted 09 June 2015 - 05:22 PM
I'm trying to let a door open if the player is inside a certain area using the openperipheral addons sensor, but it the program often crashes because of a function error.
I can't really think of a way to prevent the error, so I want the program to just ignore the error if it happens and continue. Is that possible?
Creator #2
Posted 09 June 2015 - 05:40 PM
If you are not sure whether a functiion will error or not use pcall

Example:

func = function(wow)
print.wow()
end -- this code will error
pcall(func) -- don't add brackets after func
print("hi")
Edited on 09 June 2015 - 03:42 PM
flaghacker #3
Posted 09 June 2015 - 06:12 PM
A better approach would be to try to prevent the error in the first place, could you post the code with a crashing function? Maybe we can help you find a way to prevent it.
Edited on 09 June 2015 - 04:12 PM
Creator #4
Posted 09 June 2015 - 06:35 PM
What if there is some sort of loadstring involved?
KingofGamesYami #5
Posted 09 June 2015 - 07:59 PM
Since you are using the OpenPeripherals sensor, you should check the result the function returns. If no players are in the vicinity, it won't return a table (which you would normally index to check player names). Simply add a check to the program to make sure the function returned the data you need before accessing it.

Without your code, I cannot help you more than this.
Djosjowa #6
Posted 09 June 2015 - 09:07 PM
If you are not sure whether a functiion will error or not use pcall

Example:

func = function(wow)
print.wow()
end -- this code will error
pcall(func) -- don't add brackets after func
print("hi")
This worked, thanks

A better approach would be to try to prevent the error in the first place, could you post the code with a crashing function? Maybe we can help you find a way to prevent it.

The problem is in this part of the code:

if #s.getPlayers() > 0 then
  pos = s.getPlayerByName("Djosjowa").position
  etc
end
So the if-statement checks if there are players in range and then I get the position of that player (me).
Sometimes, when i pass quickly through the sensor area I get an error "entity not found" or something along those lines for the getPlayerByName function. I think this is because the if-statement is then true, but when the code moves on, I'm already out of range of the sensor. I cannot think of a way to prevent this error, but it would be interesting to see if some of you know a solution (except for the ignoring-the-error one).
Edited on 09 June 2015 - 09:02 PM
Creator #7
Posted 09 June 2015 - 10:17 PM
Try this:

  pos =   #s.getPlayers() > 0 and s.getPlayerByName("Djosjowa").position or {}

Or something among the lines
KingofGamesYami #8
Posted 09 June 2015 - 11:33 PM
I'd think the error would be caused by someone other than you entering the field, when you aren't there.

s.getPlayers would detect them, but s.getPlayerByName("Djosjowa") wouldn't find "Djosjowa", because you aren't there.
Bomb Bloke #9
Posted 10 June 2015 - 01:21 AM
Try this:

  pos =   #s.getPlayers() > 0 and s.getPlayerByName("Djosjowa").position or {}

Or something among the lines

That's the same logic as he had, and will fall over if it were a player other than Djosjowa in range, of if he happens to move out of range mid-call.

I'd go with:

local playerData = s.getPlayerByName("Djosjowa")

if playerData then            -- If the sensor actually returned something...
  pos = playerData.position   -- ... THEN try to index into it.
  etc
end