Jump to content


manu3d's Content

There have been 7 items by manu3d (Search limited from 29-March 23)


By content type

See this member's

Sort by                Order  

#167443 Multiple if then statements

Posted by manu3d on 08 March 2014 - 10:40 AM in Ask a Pro

Good point. With the original example an input value of 1 would trigger both the first if/then AND the else in the last if/then/else. Doh! My bad. Edited my first post to reflect this.



#167349 [SOLVED] Help with pcall

Posted by manu3d on 07 March 2014 - 01:34 PM in Ask a Pro

Of course! This way pcall still gets to do its job of catching errors while data about the error becomes available through the errorCode variable. Neat!

Also, to avoid potential conflicts or even store a history of errors, I guess I could do this:


local errorTable = {}

local function foo()
  if (anErrorArises) then
    errorTable[moreOrLessUniqueErrorKey] = {code=12; level="PANIC", message="Ouchy!!!"}
    error(moreOrLessUniqueErrorKey)
  else
    return aValue
  end
end

local ok, theReturnedValueOrAnErrorKey pcall(foo)
if not ok then
  local errorObject = errorTable[theReturnedValueOrAnErrorKey ]
  print(errorObject.code)
  print(errorObject.level)
  print(errorObject.message)
end


Thank you! Much appreciated!



#167348 Ask A Pro Renewal Project (and How You Can Help!)

Posted by manu3d on 07 March 2014 - 01:13 PM in Ask a Pro

Thanks Bubba and Lyqyd for your replies. I can only reply: fair enough! :)



#167275 Multiple if then statements

Posted by manu3d on 06 March 2014 - 08:59 PM in Ask a Pro

Lua syntax for the a simple if/then control structure is this:

if(condition)then
    doSomething()
end

You code lacks the "end" keyword to close each if/then structure. This is why you get the errors.

[EDITED]
However, it is correct to do as lyqyd suggests, so that you have if-then / elseif-then / elseif-then / else. Otherwise your else block at the end will also catch the cases where input=1 and input=2, which is probably not what you want.



#167274 [SOLVED] Help with pcall

Posted by manu3d on 06 March 2014 - 08:40 PM in Ask a Pro

Thank you theoriginalbit for your answer!

Amazing how, having just started with lua, I managed to attempt to take advantage of one of the very latest features!

Concerning your workaround: my understanding is that the point of a pcall is that it catches errors and trigger special handling. I guess what you are saying is that if I want to do some error handling that includes passing back structured error information instead of a simple string I must bypass the error() function altogether. I.e. write functions that return an expected value or return something that can be interpreted and handled like an error by the calling function.

if(thereIsAnError) then
   return {errorCode=301, errorLevel="PANIC!", message="You attempted to reboot the universe without saving. Don't do that again."}
else
   return theExpectedNonTableValue
end

Am I understanding you correctly?



#167272 Ask A Pro Renewal Project (and How You Can Help!)

Posted by manu3d on 06 March 2014 - 08:15 PM in Ask a Pro

I don't mean to break the wonderful flow of tutorials that is originating from this thread, but going back to the original issue of signal-to-noise ratio, I noticed that some posters on the forum make very poor use of the quoting facility. Specifically, they quote whole messages when a snippet would be enough or they nest quotes within quotes within quotes when it is maybe unnecessary. Perhaps it'd be worth to mention -somewhere- and enforce through moderation a couple of guidelines: 1) quote the least you can - a good rule of thumb is: quoted text should be no more than 30% of your post 2) avoid nesting quotes when possible.

On a separate subject, I'm noticing the link to tutorials posted in this thread often (always?) link to another post here in the forum. As somebody who have relied heavily on MediaWiki in the past I find this strange. Wouldn't the wiki the most appropriate place for tutorials, their associated discussion pages the best place where to suggest improvements and refinements to each tutorial?



#167155 [SOLVED] Help with pcall

Posted by manu3d on 06 March 2014 - 12:55 AM in Ask a Pro

Hi everybody, lua -and- computercraft newbie here.

I'm dealing with the following simple code, executed in a turtle, which is derived from the last example in this page: http://www.lua.org/pil/8.4.html

local status, err = pcall(function () error({code=121}) end)
print(status)
print(err)
print(err.code)

I'm confused by the result. Status is false, which make sense. But err is a string when I thought it'd be a table, and print(err.code) results in an empty line, consistent with err -not- being a table.

Am I doing something wrong or pcall does not return a table in ComputerCraft?

Manu