Avoiding errors with nil values AND disabling timers fast

Posted by Silencher on Fri 10 Apr 2015 01:21 PM — 4 posts, 20,120 views.

#0
Is there a way to avoid error message pop ups for nil values in Hard variables? Example: an if statement that calls GetVariable but the target variable doesn't exist.

Also- short of manually clicking disable and disconnecting from a mud, is there a way to disable all timers? I have a timer on a 1 second loop and it's active when disconnected and it is screwing me up everytime i load that world because it has an error in it and is giving me continuous pop up errors. not sure how to fix it. Is there any quick shortcut to disable all timers?
USA Global Moderator #1
Quote:
Is there a way to avoid error message pop ups for nil values in Hard variables? Example: an if statement that calls GetVariable but the target variable doesn't exist.
The documentation for GetVariable says it just returns nil. So why not check if the result is nil before trying to use it?


I'm pretty sure that timer control is per-world. You could go and edit your world file.
Or use http://www.gammon.com.au/scripts/doc.php?function=EnableTimer
or maybe SetOption("enable_timers", 0)
Amended on Fri 10 Apr 2015 03:29 PM by Fiendish
#2
Sorry, can you show me an example of how to check for a nil value? i realize this is likely a 'newb' question but i have no idea
Australia Forum Administrator #3
You could use two methods. One is:


foo = GetVariable ("foo")
if foo then
  -- do something with it
end -- if


In Lua doing an "if variable" is equivalent to checking if it is nil or not (and also if it is false or not but that wouldn't apply in this case).

More explicitly:


foo = GetVariable ("foo")
if foo ~= nil then
  -- do something with it
end -- if





Another technique which is often used is to force in a default value, eg.



foo = GetVariable ("foo") or ""

-- use foo


This uses "short-circuit" evaluation. Either you get the variable foo, or if that doesn't exist you get a "default" of an empty string (which won't be nil). You could make the default something else if you wanted (eg. zero).