Jump to content




An unusual end error


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

#1 ShadowDisruptor

  • Members
  • 74 posts

Posted 17 September 2014 - 08:07 PM

Now, before you judge me for not being able to figure out an end error, I already checked my ends. I did by hand as well as with notepad++. From what I can see, all my ends are there. The exact error: 8: 'end' expected (to close 'function' at line 2)

Before you open spoiler: the code is 46 lines, couldn't shorten it due to me not knowing where the error is. Also, using the tekkit modpack, which really dosen't matter because the issue isn't with the mods codes.
Spoiler

Edited by ShadowDisruptor, 17 September 2014 - 08:08 PM.


#2 Dragon53535

  • Members
  • 973 posts
  • LocationIn the Matrix

Posted 17 September 2014 - 08:12 PM

Found your error
   return checkReactor
   checkReactor = nil
Using return gets out of the function entirely, so setting checkReactor to being nil after returning messes with the lua interpreter

Also when you set checkReactor as a local variable, when you leave the function it's automatically set to nil, so you don't have to reset it back to nil.

Edited by Dragon53535, 17 September 2014 - 08:14 PM.


#3 Dog

  • Members
  • 1,179 posts
  • LocationEarth orbit

Posted 17 September 2014 - 08:20 PM

I've been over your code several times and I can't see what's causing the 'end' expected error, but I can see a couple of other things you may want to address:

1. You don't need to nil your variables if they are declared local within the scope of a function - when the function exits the variables will be discarded automatically
2. In your first function (isReactor) you use checkReactor both to wrap the peripheral and to store the result of your check on the wrapped device. Since you aren't going back and referencing the wrapped peripheral again it probably isn't technically a problem, but it's good practice not to re-use a variable like that. I'd recommend changing the wrapping variable name to something like tmpReactor or something else that makes sense.

FWIW, I hope that helps :)

EDIT: semi- :ph34r: 'd

View PostDragon53535, on 17 September 2014 - 08:12 PM, said:

...
Using return gets out of the function entirely, so setting checkReactor to being nil after returning messes with the lua interpreter
...
Are you sure about that? I'm no expert (by any stretch of the imagination), but my understanding is that when a return is invoked, everything after the return is ignored. However, looking at it, I can see how that might confuse the interpreter if my understanding is incorrect.

EDIT2: The more I look at it, the more Dragon's answer makes sense - it's the only thing I can see that would seem to be the cause. I learned something new today :D

Edited by Dog, 17 September 2014 - 08:27 PM.


#4 flaghacker

  • Members
  • 655 posts

Posted 17 September 2014 - 08:33 PM

That is correct:
function ()
  if something then
    return
  end
  --other code
end

This is correct, but notice how the return is directly followed by the end that closes the if statement?

function ()
  if something then
    return
    --other code
  end
  --other code
end
This would trow the error, as the end is no longer directly followed by the end.

It expects an end after the return, an end expected

Edited by flaghacker, 17 September 2014 - 08:33 PM.


#5 Dog

  • Members
  • 1,179 posts
  • LocationEarth orbit

Posted 17 September 2014 - 08:33 PM

Thanks for the clarification, flaghacker :)

#6 ShadowDisruptor

  • Members
  • 74 posts

Posted 17 September 2014 - 08:37 PM

View PostDog, on 17 September 2014 - 08:20 PM, said:

2. In your first function (isReactor) you use checkReactor both to wrap the peripheral and to store the result of your check on the wrapped device. Since you aren't going back and referencing the wrapped peripheral again it probably isn't technically a problem, but it's good practice not to re-use a variable like that. I'd recommend changing the wrapping variable name to something like tmpReactor or something else that makes sense.
I did this on purpose to try and make the program more efficient.

View PostDragon53535, on 17 September 2014 - 08:12 PM, said:

Found your error
   return checkReactor
   checkReactor = nil
Using return gets out of the function entirely, so setting checkReactor to being nil after returning messes with the lua interpreter

Also when you set checkReactor as a local variable, when you leave the function it's automatically set to nil, so you don't have to reset it back to nil.
Thanks for the help!

#7 Bomb Bloke

    Hobbyist Coder

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

Posted 18 September 2014 - 12:41 AM

View PostShadowDisruptor, on 17 September 2014 - 08:37 PM, said:

I did this on purpose to try and make the program more efficient.

Since "checkReactor" is local to "isReactor", it's automatically cleared whenever that function ends. There's no need to do it yourself.





2 user(s) are reading this topic

0 members, 2 guests, 0 anonymous users