where to learn

Help creating logic scripts for Air Manager Instruments

Moderators: Ralph, russ

Message
Author
GHILL
Posts: 18
Joined: Wed Apr 13, 2022 9:46 pm

Re: where to learn

#21 Post by GHILL »

led_alt1 = hw_led_add("low amps left", 0.0)



if electrical_genalt_bus_amps[2] < 20 then
hw_led_set(led_alt1, 1)
else
hw_led_set(led_alt, 0)
end



fs2020_variable_subscribe("ELECTRICAL GENALT BUS AMPS", "amps", new_data_fs2020)

so as understand this, ive added a led named alt1, im asking that if the amps are below 20 on engine 1 [2}, then the light is to be lit. if its above 20 amps then the light goes off. Ive subscribed the script to look at the electrical genalt bus amps.


i get an error saying, attempt to index a nil value, (global electrical_genalt_bus_amps.


the function part of the script im not sure what to write as ive tried a few things

i hope this explains a little better,

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

Re: where to learn

#22 Post by Keith Baxter »

GHILL wrote: Thu Apr 28, 2022 2:56 pm led_alt1 = hw_led_add("low amps left", 0.0)



if electrical_genalt_bus_amps[2] < 20 then
hw_led_set(led_alt1, 1)
else
hw_led_set(led_alt, 0)
end



fs2020_variable_subscribe("ELECTRICAL GENALT BUS AMPS", "amps", new_data_fs2020)

so as understand this, ive added a led named alt1, im asking that if the amps are below 20 on engine 1 [2}, then the light is to be lit. if its above 20 amps then the light goes off. Ive subscribed the script to look at the electrical genalt bus amps.


i get an error saying, attempt to index a nil value, (global electrical_genalt_bus_amps.


the function part of the script im not sure what to write as ive tried a few things

i hope this explains a little better,
Hi,

Firstly, please preview your post before posting.

You need the basics. There are many typos and no print statements to debug. You should also be familiar with the debug features in the create/edit console.
I am sure both Jacques and myself would be happy to spend a 1/2 hour on the discord to get you pointed in the right direction.


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 

GHILL
Posts: 18
Joined: Wed Apr 13, 2022 9:46 pm

Re: where to learn

#23 Post by GHILL »

I got this part of the script from the oil pressure example, copied and pasted and tried to tweek it to my needs. All be it to no end. Im not one to give up so i will get it one day,

Im sure its a penny drop kinda moment when you get to grips with this level of the code.

my plan tonight is to watch the video posted at the start and see where that takes me, maybe the wiki pages will be more obvious to me. Ive said before that i don't come here in the forums on a whim. If i'm here it means i've tried my best to understand.

The Disord would be a great help and very much appreciated, thank you.

Alex

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

Re: where to learn

#24 Post by JackZ »

Your code and reply definitely show that you don’t (yet) understand how a variable_subscribe() works.
https://siminnovations.com/wiki/index.p ... _subscribe

And also how Lua works.

Each time a data ref/value in a variable_suscribe() changes in the sim, the AM plugin informs AM and the corresponding function is called, with the value passed as an argument to the fonction called.

Code: Select all

ed_alt1 = hw_led_add("low amps left", 0.0)

if electrical_genalt_bus_amps[2] < 20 then
hw_led_set(led_alt1, 1)
else
hw_led_set(led_alt, 0)
end

fs2020_variable_subscribe("ELECTRICAL GENALT BUS AMPS", "amps", new_data_fs2020)
In the code you posted the function to be called should be
new_data_fs2020. It is not defined anywhere.
And in Lua, any function should be defined BEFORE any call.

Moreover you are using electrical_genalt_bus_amps[2] which is not defined anywhere and hence Lua consider it as a nil value and throws an error.

So your code should be instead:

Code: Select all

led_alt1 = hw_led_add("low amps left", 0.0)

function new_data_fs2020(electrical_genalt2_bus_amps)
  if electrical_genalt2_bus_amps < 20 then
          hw_led_set(led_alt1, 1)
          print("Led ON")
   else
          hw_led_set(led_alt1, 0)
          print("Led OFF")
    end
end

fs2020_variable_subscribe("ELECTRICAL GENALT BUS AMPS:2", "Amperes", new_data_fs2020)
Provided you are watching generator two (hence the :2 index) amps value only.
Note that we used ELECTRICAL GENALT BUS AMPS:2 with the index here, and not in the variable itself. So if you wanted to watch both generator values, you should subscribe to two different values and your code would be like.

Code: Select all

led_alt1 = hw_led_add("low amps left", 0.0)

function new_data_fs2020(electrical_genalt1_bus_amps,electrical_genalt2_bus_amps)
  if (electrical_genalt1_bus_amps < 20) or (electrical_genalt2_bus_amps < 20) then
          hw_led_set(led_alt1, 1)
          print("Led ON")
   else
          hw_led_set(led_alt1, 0)
          print("Led OFF")
    end
end

fs2020_variable_subscribe("ELECTRICAL GENALT BUS AMPS:1", "Amperes","ELECTRICAL GENALT BUS AMPS:2", "Amperes", new_data_fs2020)
And to be more accurate, you should only light the warning light when some voltage is available on the MAIN ELEC BUS (ie the MAster is ON and battery voltage is above 24volts)

Code: Select all

led_alt1 = hw_led_add("low amps left", 0.0)

function new_data_fs2020(electrical_genalt1_bus_amps,electrical_genalt2_bus_amps,electrical_mainbus_volts)
  if ((electrical_genalt1_bus_amps < 20) or (electrical_genalt2_bus_amps < 20))and electrical_mainbus_volts>24 then
          hw_led_set(led_alt1, 1)
          print("Led ON")
   else
          hw_led_set(led_alt1, 0)
          print("Led OFF")
    end
end

fs2020_variable_subscribe("ELECTRICAL GENALT BUS AMPS:1", "Amperes","ELECTRICAL GENALT BUS AMPS:2", "Amperes","ELECTRICAL MAIN BUS VOLTAGE","Volts", new_data_fs2020)
Last edited by JackZ on Thu Apr 28, 2022 6:35 pm, edited 1 time in total.
My YouTube Chanel on the A320 (Real SOPs by an Airline Pilot IRL):
https://www.youtube.com/playlist?list=P ... 0Q6SBASRqJ

GHILL
Posts: 18
Joined: Wed Apr 13, 2022 9:46 pm

Re: where to learn

#25 Post by GHILL »

ok,

I am understanding this as, The function is the link to the sim subscribe at the bottom of the script to gain the information from the sim to allow the arguments to be used? (in a very basic summary).

Ive loaded the script and hey presto no surprise it works lol. Im going to try and use the info gained here to play with it and see if i can get the light only to function when the main bus volt is over 10v like the oil pressure lights. hopefully lol

This has been very valuable and very much appreciated, Im going to go over all the tutorials again this time with a better understanding of what is what.

many thanks Alex

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

Re: where to learn

#26 Post by JackZ »

I am understanding this as, The function is the link to the sim subscribe at the bottom of the script to gain the information from the sim to allow the arguments to be used? (in a very basic summary)
To be exact, the subscribe() calls the function and feeds it with as many parameters as sim variables that are declared in the subscribe(). read carefully the examples provided to understand the difference

Don't forget to check whether "low amps left" is really tied to engine generator #2, as usually engine 1 is the left one.
If there are separate warning lights for right and left generators, of course the later code is not 100% correct, but I hope you got the general idea and will be able to come up with a modified version to suit your needs.
My YouTube Chanel on the A320 (Real SOPs by an Airline Pilot IRL):
https://www.youtube.com/playlist?list=P ... 0Q6SBASRqJ

GHILL
Posts: 18
Joined: Wed Apr 13, 2022 9:46 pm

Re: where to learn

#27 Post by GHILL »

wow many thanks for all the examples, this will be truley helpful going forward.

yes when i did the oil pressure ones i did #2 and#3 for left and right engine as in the info it seemed #1 was a general value, or for the single engine aircraft.

The way you have explained how to instruct that the alt light is only activated once the battery is on is slightly different from the example in the oil pressure one i was following. nice to know a few different way to accomplish the gaol.

Sorry for the delay in replying my night of code learning was interrupted with a call out.

Many thanks again for the information

Alex

Post Reply