Wrench Posted December 14, 2021 Posted December 14, 2021 Hey all, Can anybody help me understand error handling with things like pcall? I'd like to setup some scripts that are prone to failure with pcall, so that my dedicated server doesn't stop when/if they fail. I have a script that writes files, and that can fail for various reasons, and when that happens, the error message comes up on the dedicated server machine, and the server freezes until I remote in and close the error window. What I'd like to do is use pcall, and have that trigger a trigger.action.outText rather than soft crash the server. I've looked at some documentation on pcall, but haven't gotten it to work. As a second option, IIRC there is a configuration file one can edit to stop those error windows showing as well, but I'm not certain if that's true, and what I'd have to do if it is. Carrier Script.
toutenglisse Posted December 14, 2021 Posted December 14, 2021 1 hour ago, Wrench said: ...As a second option, IIRC there is a configuration file one can edit to stop those error windows showing as well, but I'm not certain if that's true, and what I'd have to do if it is. You maybe refer to this : "Here's the good news: It's really easy to prevent DCS from displaying a message box when it encounters an error in a Lua script: Simply set env.setErrorMessageBoxEnabled(boolean on) to false: env.setErrorMessageBoxEnabled(false)" From : 1
Wrench Posted December 14, 2021 Author Posted December 14, 2021 (edited) Huh, I'd have thought that would have to go in a global config. Thanks, toutenglisse I'd still like to understand pcall better, though. Edited December 14, 2021 by Wrench Carrier Script.
Wrench Posted December 14, 2021 Author Posted December 14, 2021 I think as far as disabling the error message box, I'll have to take a look at using a script hook. I should be able to have the server inject that into each mission with net.dostring_in("server") at missionLoadDone. Carrier Script.
Wrench Posted December 16, 2021 Author Posted December 16, 2021 (edited) So I tried my idea above and still get error messages, probably a scope issue. The odd thing is it works on my client but not my server. Edited December 16, 2021 by Wrench Carrier Script.
TEMPEST.114 Posted December 22, 2022 Posted December 22, 2022 On 12/14/2021 at 3:06 PM, Wrench said: Huh, I'd have thought that would have to go in a global config. Thanks, toutenglisse I'd still like to understand pcall better, though. I don't know if you know any other programming languages or scripting languages but PCALL is kinda like a try catch, kinda. Here is the documentation: Quote pcall (f, arg1, ···) Calls function f with the given arguments in protected mode. This means that any error inside f is not propagated; instead, pcall catches the error and returns a status code. Its first result is the status code (a boolean), which is true if the call succeeds without errors. In such case, pcall also returns all results from the call, after this first result. In case of any error, pcall returns false plus the error message. You can use it like this: function safelyCall(func, args) local success, result = pcall(func, args) if not success then --log, try to determine why it failed with the 'result' variable or just fail gracefully return 0 else return result end end function divide(a, b) return a / b end function catchDivideByZero() local result = self:safelyCall(self:divide, { 4, 0 }) -- print / use result end I've just done this from memory without testing so I can't promise it's perfect, but it should give you an idea on how you can best use it. Hope this helps.
Recommended Posts