Is this a lua Error?

Discuss suspected bugs with other users and Sim Innovations Staff

Moderators: russ, Ralph

Message
Author
The Artful Dodger
Posts: 204
Joined: Sat Jul 09, 2022 3:20 pm

Is this a lua Error?

#1 Post by The Artful Dodger »

Hello:
I keep getting the following error:
Lua Error.jpg
It is the first statement in my instrument and it reads:

Code: Select all

		SpeedIndex = AirSpeed //  TextKnotsPerDivision
In my lib function, I define TextKnotsPerDivision thus:

Code: Select all

TextKnotsPerDivision = 20
In the attachment, you can also see that it is defined because I have printed out both values.

If this is because of something I'm doing wrong, please let me know because I am completely confused!

Sparky

User avatar
Keith Baxter
Posts: 4674
Joined: Wed Dec 20, 2017 11:00 am
Location: Botswana

Re: Is this a lua Error?

#2 Post by Keith Baxter »

Sparky,

That might be because your var is not declared before the function.

You can get around that by putting in a condition.

Code: Select all

If TextKnotsPerDivision ~= nil then
bla bla bla
end
But should your var calculation not be

Code: Select all

SpeedIndex = AirSpeed / TextKnotsPerDivision
http://www.lua.org/manual/5.4/manual.html#2.1

Keith
Last edited by Keith Baxter on Tue Aug 16, 2022 10:02 pm, edited 1 time in total.
AMD RYZEN 9 5950X CPU, Corsair H80I cooler, ASUS TUF GAMING B550-PLUS AMD Ryzen Mother Board,  32Gb ram Corsair Vengeance 3000Mh, MSI GTX960 4G graphics card 

User avatar
Keith Baxter
Posts: 4674
Joined: Wed Dec 20, 2017 11:00 am
Location: Botswana

Re: Is this a lua Error?

#3 Post by Keith Baxter »

Sparky,

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.

Code: Select all

AirSpeed = 300
--TextKnotsPerDivision = 10

SpeedIndex = AirSpeed //  TextKnotsPerDivision

print(SpeedIndex)
Keith
AMD RYZEN 9 5950X CPU, Corsair H80I cooler, ASUS TUF GAMING B550-PLUS AMD Ryzen Mother Board,  32Gb ram Corsair Vengeance 3000Mh, MSI GTX960 4G graphics card 

User avatar
Keith Baxter
Posts: 4674
Joined: Wed Dec 20, 2017 11:00 am
Location: Botswana

Re: Is this a lua Error?

#4 Post by Keith Baxter »

@The Artful Dodger

Just for interest sake. This code will give you a divide by zero error.

Code: Select all

AirSpeed = 300
TextKnotsPerDivision = 0
SpeedIndex = AirSpeed // TextKnotsPerDivision
print("SpeedIndex    "..SpeedIndex)
This code will return a value for Speedindex of <inf>

Code: Select all

AirSpeed = 300
TextKnotsPerDivision = 0
SpeedIndex = AirSpeed / TextKnotsPerDivision
print("SpeedIndex    "..SpeedIndex)
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.
AMD RYZEN 9 5950X CPU, Corsair H80I cooler, ASUS TUF GAMING B550-PLUS AMD Ryzen Mother Board,  32Gb ram Corsair Vengeance 3000Mh, MSI GTX960 4G graphics card 

The Artful Dodger
Posts: 204
Joined: Sat Jul 09, 2022 3:20 pm

Re: Is this a lua Error?

#5 Post by The Artful Dodger »

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 :

Code: Select all

 
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?

As always, thanks for your help.

Sparky

User avatar
Keith Baxter
Posts: 4674
Joined: Wed Dec 20, 2017 11:00 am
Location: Botswana

Re: Is this a lua Error?

#6 Post by Keith Baxter »

Sparky,

OK so you are using library's.

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.

Keith
AMD RYZEN 9 5950X CPU, Corsair H80I cooler, ASUS TUF GAMING B550-PLUS AMD Ryzen Mother Board,  32Gb ram Corsair Vengeance 3000Mh, MSI GTX960 4G graphics card 

User avatar
Keith Baxter
Posts: 4674
Joined: Wed Dec 20, 2017 11:00 am
Location: Botswana

Re: Is this a lua Error?

#7 Post by Keith Baxter »

Sparky,

As I mentioned previously if you are having issues with z ordering the var then put in a check.

Code: Select all

AirSpeed = 300
--TextKnotsPerDivision = 10
if TextKnotsPerDivision ~= nil then 
   SpeedIndex = AirSpeed // TextKnotsPerDivision
 print("SpeedIndex    "..SpeedIndex)  
else
   print("Speed index = nil")
end
Keith
AMD RYZEN 9 5950X CPU, Corsair H80I cooler, ASUS TUF GAMING B550-PLUS AMD Ryzen Mother Board,  32Gb ram Corsair Vengeance 3000Mh, MSI GTX960 4G graphics card 

The Artful Dodger
Posts: 204
Joined: Sat Jul 09, 2022 3:20 pm

Re: Is this a lua Error?

#8 Post by The Artful Dodger »

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!

Sparky

User avatar
jph
Posts: 2846
Joined: Fri Apr 10, 2020 12:50 pm
Location: Somewhere over the rainbow..

Re: Is this a lua Error?

#9 Post by jph »

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.
Joe. CISSP, MSc.

The Artful Dodger
Posts: 204
Joined: Sat Jul 09, 2022 3:20 pm

Re: Is this a lua Error?

#10 Post by The Artful Dodger »

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.

Sparky

Post Reply