If you run this code you will get your error. If you un-commencement line 2 the error is not there.
So <TextKnotsPerDivision> has no value declared before <SpeedIndex = AirSpeed // TextKnotsPerDivision>. That means that TextKnotsPerDivision is nil.
Hence why I asked about the use of // instead of /
@Corjan Why is there a difference in error reporting?
I know that // is a floor division and / a float division. Just curious as to the different fault reporting.
Keith
Last edited by Keith Baxter on Tue Aug 16, 2022 10:02 pm, edited 3 times in total.
Hi, Keith:
I guess I'm not getting the "not declared before the function". Does "before" mean physically before in the file or executed before? In the file, it goes something like this :
Start of logic.lua
..
lib_AirSpeed()
..
eof
Start of function AirSpeedProcessing(Airspeed)
All code to process the display of the Air Speed including the definition causing the error.
..
end
Start of function AirSpeed.lua
function lib_AirSpeed()
...
Constants including that in question.
..
Call to function AirSpeedProcessing(Airspeed)
..
end
Subscriptions etc.
eof
So, you see that function lib_AirSpeed is executed before function AirSpeedProcessing but it has to appear second. How does one solve that, make them individual files?
The variable <TextKnotsPerDivision> must be declared and have a value before you try to use it for any math.
If <TextKnotsPerDivision> is in a different library to where you do this calculation <SpeedIndex = AirSpeed // TextKnotsPerDivision>, then in the lodjic.lua, the library with the <TextKnotsPerDivision> must be declaired first.
TextKnotsPerDivision_lib()
SpeedIndex_lib()
If they are in the same library then declare <TextKnotsPerDivision> before you do the <SpeedIndex> calculation.
Hi, guys:
After all the discussion, I thought I would provide the reasoning behind and what caused the error in the first place.
When I was organizing the code at the outset, I wanted to move as much of the code as possible into the "lib" function (only to be executed once) and then put the information that changed each time into another function (AirSpeedProcessing) and the latter function called by the callback process. Thus the two functions and the difficulty with when/where something is defined.
As to the use of // instead of /, the // rounds to minus infinity, thus ensuring an integer value but without the possibility of rounding up. Of course, neither of the problems mentioned would have happened, at least in my mind, because I had already defined the denominator!
So, that explains my erroneous reasoning! But, it begs the question: Is all of that really worth it - is Lua execution good enough that a few definitions repeated each time noticible? Or, if it is, how do I place everything so that it works properly?
Sorry for the trouble - I started coding when such improvements were critical and it's a hard habit to break!
It makes absolutely no discernible difference in operation these days.
Make the code the EASIEST for YOU to read, understand and follow and be able to come back to in 6 months time and resume from where you left off.
Your code comments are far far more important.
In the time since I wrote those comments, I think I have figured out how to "have my cake and eat it too"! But I have to try it first. It may also solve the problem I was going to have later on when the time came to insert bugs and all the other things that make up a modern PFD.
I also know to insert lots of comments - what effect they have on an interpretative language I don't know.