global variables

Questions about deployment and use of Air Manager Instruments

Moderators: russ, Ralph

Post Reply
Message
Author
MeTom
Posts: 7
Joined: Mon May 31, 2021 3:31 pm

global variables

#1 Post by MeTom »

I have the code below but the print() statements all come back as 'nil'.
If I put a print() inside the function I get what I expect to get back.
can someone please explain Global vs local. I thought my 'power = {}' at the top would make it global but it seams that when I assign power['IsPowerOn'] it is not assigning it to the global power array.

Code: Select all

power = {}

function set_power_xpl(bus_volts, avionics_on, avionics_EQ)
    power['isPowerOn'] = fif((bus_volts[1] > 3 or bus_volts[2] > 3 or bus_volts[3] > 3), 1, 0)
    power['isAvionicsAvl'] = avionics_EQ
    power['isAvionicsOn'] = avionics_on
end

xpl_dataref_subscribe("sim/cockpit2/electrical/bus_volts", "FLOAT[6]",
                      "sim/cockpit/electrical/avionics_on", "INT",
                      "sim/cockpit/electrical/avionics_EQ", "BOOL",
                      set_power_xpl)

print('isPowerOn:', power['isPowerOn'])
print('isAvionicsAvl:', power['isAvionicsAvl'])
print(power)

JackZ
Posts: 2262
Joined: Mon Feb 22, 2016 1:02 pm

Re: global variables

#2 Post by JackZ »

Hi
To me, It is not a problem of scope (local/global) of variables.

The function set_power_xpl() is not called before the dataref has changed in the sim.

The Power variable is set to nil at the very beginning and at runtime, Lua executes the three print() statements BEFORE the function has ever been executed, hence the result is nil.

Never assume that a Dataref_subscribe() is called at startup.
It is kinda asynchronous. The callback function itself is not called at initialization, ie when the subscribe line is executed
The fact that the line of the subscribe is placed before the print() statements doesn’t necessarily mean that it will be called before hand.

My advice is to put all the dataref_subscribe at the end of your code to « physically » reflect that fact.

And in your case, I would probably use a request_callback() BEFORE the print() statements, to force the execution of the function at least once. That way, power will be populated, and the print() statement will hopefully reflect the proper values.

But there is probably another method, I’m curious to know it.
One may think of adding to the subscribe() a value that is more than likely susceptible of change rapidly, such as time.

Jacques

As a side note, « hello » and «  thank you » are also powerful words when crying for help in the Internet, you know?
My YouTube Chanel on the A320 (Real SOPs by an Airline Pilot IRL):
https://www.youtube.com/playlist?list=P ... 0Q6SBASRqJ

User avatar
Sling
Posts: 5237
Joined: Mon Sep 11, 2017 2:37 pm
Contact:

Re: global variables

#3 Post by Sling »

Hi,

Jacques is absolutely right. In this case it’s not the scope that’s the issue but the timing of the code execution. Rather than use request_callback() you should wait until the subscribe runs once before trying to use the variable. You can either use a timer for this or most likely you can just use the variable in some later code.

What you have posted doesn’t really do anything so it’s difficult to judge how you wish to use the variable but most likely if you are using it in another callback then this subscribe will of run once before then and the need to do any of the above will be negated anyhow.

Tony

MeTom
Posts: 7
Joined: Mon May 31, 2021 3:31 pm

Re: global variables

#4 Post by MeTom »

JackZ wrote: Tue Jun 01, 2021 7:28 am Hi
To me, It is not a problem of scope (local/global) of variables.

The function set_power_xpl() is not called before the dataref has changed in the sim.

The Power variable is set to nil at the very beginning and at runtime, Lua executes the three print() statements BEFORE the function has ever been executed, hence the result is nil.

Never assume that a Dataref_subscribe() is called at startup.
It is kinda asynchronous. The callback function itself is not called at initialization, ie when the subscribe line is executed
The fact that the line of the subscribe is placed before the print() statements doesn’t necessarily mean that it will be called before hand.

My advice is to put all the dataref_subscribe at the end of your code to « physically » reflect that fact.

And in your case, I would probably use a request_callback() BEFORE the print() statements, to force the execution of the function at least once. That way, power will be populated, and the print() statement will hopefully reflect the proper values.

But there is probably another method, I’m curious to know it.
One may think of adding to the subscribe() a value that is more than likely susceptible of change rapidly, such as time.

Jacques

As a side note, « hello » and «  thank you » are also powerful words when crying for help in the Internet, you know?
Sorry Jackz, you are right. I forgot the "Thank You". I normally do but it slipped my mind. I was aggravated, LOL
I see that mistake now. I was assuming that the dataref had been read. Thanks for the idea of using request_callback()

JackZ
Posts: 2262
Joined: Mon Feb 22, 2016 1:02 pm

Re: global variables

#5 Post by JackZ »

The main issue here is a problem that is frequently encountered:
Get the initial values of the sim at instrument startup.

Jacques
My YouTube Chanel on the A320 (Real SOPs by an Airline Pilot IRL):
https://www.youtube.com/playlist?list=P ... 0Q6SBASRqJ

MeTom
Posts: 7
Joined: Mon May 31, 2021 3:31 pm

Re: global variables

#6 Post by MeTom »

Sling wrote: Tue Jun 01, 2021 8:20 am Hi,

Jacques is absolutely right. In this case it’s not the scope that’s the issue but the timing of the code execution. Rather than use request_callback() you should wait until the subscribe runs once before trying to use the variable. You can either use a timer for this or most likely you can just use the variable in some later code.

What you have posted doesn’t really do anything so it’s difficult to judge how you wish to use the variable but most likely if you are using it in another callback then this subscribe will of run once before then and the need to do any of the above will be negated anyhow.

Tony
Thanks Sling,
The power array is going to be used in the LED segments of an autopilot (heading, speed, VIS, altitude). Each Callback will check the 'power[]' and change the LEDs to "power['bus_volts']=0 then OFF', "power['avionics_on']=0 then -----" or "power['bus_volts'] > 3 & power['avionics_on']=1 then ON" or something like that. I have not got that far yet.
I think I am going with JackZ idea of request_callback() because I want to check the power first and change the LEDs at startup.
Thanks again

User avatar
Sling
Posts: 5237
Joined: Mon Sep 11, 2017 2:37 pm
Contact:

Re: global variables

#7 Post by Sling »

MeTom wrote: Tue Jun 01, 2021 12:04 pm
Sling wrote: Tue Jun 01, 2021 8:20 am Hi,

Jacques is absolutely right. In this case it’s not the scope that’s the issue but the timing of the code execution. Rather than use request_callback() you should wait until the subscribe runs once before trying to use the variable. You can either use a timer for this or most likely you can just use the variable in some later code.

What you have posted doesn’t really do anything so it’s difficult to judge how you wish to use the variable but most likely if you are using it in another callback then this subscribe will of run once before then and the need to do any of the above will be negated anyhow.

Tony
Thanks Sling,
The power array is going to be used in the LED segments of an autopilot (heading, speed, VIS, altitude). Each Callback will check the 'power[]' and change the LEDs to "power['bus_volts']=0 then OFF', "power['avionics_on']=0 then -----" or "power['bus_volts'] > 3 & power['avionics_on']=1 then ON" or something like that. I have not got that far yet.
I think I am going with JackZ idea of request_callback() because I want to check the power first and change the LEDs at startup.
Thanks again
Take whichever approach you wish. Just saying that request_callback() is not needed for the use you describe. How are you going to set the LED’s without first knowing their state. Answer you get their states from one or more different callbacks and there in lies the solution.

Post Reply