Inline functions .. Lua tips & tricks

Help creating logic scripts for Air Manager Instruments

Moderators: russ, Ralph

Post Reply
Message
Author
auditdata
Posts: 63
Joined: Sun Jun 21, 2020 2:06 pm

Inline functions .. Lua tips & tricks

#1 Post by auditdata »

I have a need for inline functions.
So far I have used them successfully.
I am stuck on the button_add one when trying to use both pressed and released
The example given enables the pressed state to be sensed as per the example in "Lua tips & tricks"

Code: Select all

 button_add("pressed.png", "released.png", 0, 0, 256, 256, function()
    print("Button has been pressed")
 end)
but the standard version enables the button released to be acted upon ie there are two function calls "click_press_callback" and 'click_release_callback"

Code: Select all

button_id = button_add(img_normal,img_pressed,x,y,width,height,click_press_callback,click_release_callback)
How is this achieved in an inline version?
Or is it not possible?

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

Re: Inline functions .. Lua tips & tricks

#2 Post by Sling »

Just the same method but with the extra inline function call after the first one. Like this.

Code: Select all

 button_add("pressed.png", "released.png", 0, 0, 256, 256, function()
    print("Button has been pressed")
 end,
function()
    print("Button has been released")
 end)

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

Re: Inline functions .. Lua tips & tricks

#3 Post by JackZ »

I'm personnally not a big fan of inline() functions if I can avoid it, i.e. 90% of the time.
The code is shorter but definitely less readable IMHO.

And except for use in a timer() function where it could make sense, since in that case the corresponding function would be called only once and after the delay set by the timer, I don't really see the advantage of using such a feature over the classical outside function declaration, but accept to be proven wrong.
Not even sure it saves in compilation or execution time overall...


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

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

Re: Inline functions .. Lua tips & tricks

#4 Post by Keith Baxter »

JackZ wrote: Mon Jun 29, 2020 9:05 pm I'm personnally not a big fan of inline() functions if I can avoid it, i.e. 90% of the time.
The code is shorter but definitely less readable IMHO.

And except for use in a timer() function where it could make sense, since in that case the corresponding function would be called only once and after the delay set by the timer, I don't really see the advantage of using such a feature over the classical outside function declaration, but accept to be proven wrong.
Not even sure it saves in compilation or execution time overall...


Jacques
Jaques,

Inline function is primarily used in canvas_draw.
That being said. Not easy to understand.

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
Sling
Posts: 5239
Joined: Mon Sep 11, 2017 2:37 pm
Contact:

Re: Inline functions .. Lua tips & tricks

#5 Post by Sling »

I know you are not a big fan Jacques and I understand the reasons. In this case a way to shorten the code and not have lots of similar functions was requested. It is a means to achieve that. My intention was for it to be used like this.

Code: Select all

button_add(nil,nil,0,0,100,100, function() common_func(var) end)

User avatar
Corjan
Posts: 2939
Joined: Thu Nov 19, 2015 9:04 am

Re: Inline functions .. Lua tips & tricks

#6 Post by Corjan »

Hi,

As with many things in programming, it is all up to personal taste.
Personally I like the inline functions since it keeps all code together. Callback functions might end up far from the place they are subscribed, making the code a bit cluttered.

Corjan

SimPassion
Posts: 5339
Joined: Thu Jul 27, 2017 12:22 am

Re: Inline functions .. Lua tips & tricks

#7 Post by SimPassion »

There's pros and cons, writing similar statements in one place make the coding rather ordered and provide easy search like grouping all subscribe statements at the end of the scripts or even in another manner
for me, I also like other simmers doing differently, as anyone bring here their best practices that greatly open and enhance each one habits through the sharing

Gilles

auditdata
Posts: 63
Joined: Sun Jun 21, 2020 2:06 pm

Re: Inline functions .. Lua tips & tricks

#8 Post by auditdata »

Thanks Guys, That's awesome. :D
For the situation where it is toggling a dataref if the user presses the button quickly the toggle does not get to happen so needs a short delay.
So from this

Code: Select all

        button_ref[b_n]=button_add(on_png[b_n], off_png[b_n], x_pos[b_n], y_pos[b_n], 100, 100, function()
            xpl_dataref_write(dref_wt[b_n], var_type[b_n], var_on[b_n]) 
            print("Button Pressed")
        end,
        function()
            print("Button has been released")
            xpl_dataref_write(dref_wt[b_n], var_type[b_n], var_off[b_n]) 
        end)
How would you put in a short delay just before the second xpl_dataref_write(dref_wt[b_n], var_type[b_n], var_off[b_n])
I guess I could use a long for next loop but that would be machine speed dependant. I can't find a sleep or wait function in the documentation.
Any ideas.?

SimPassion
Posts: 5339
Joined: Thu Jul 27, 2017 12:22 am

Re: Inline functions .. Lua tips & tricks

#9 Post by SimPassion »

I can't remember any sleep or wait feature in LUA
perhaps by adding a timer_start and manage the state by reading a variable value stating if the timer has ended together with checking https://siminnovations.com/wiki/index.p ... er_running

other may bring some nicer idea

Gilles

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

Re: Inline functions .. Lua tips & tricks

#10 Post by Sling »

You can create your own wait function using Lua but you have to be careful using it as no code runs while it’s waiting and if not managed correctly in can hang up your instrument. I think it’s best to use a one shot AM timer for this.

Out of interest what specifically are you trying to control that requires a quick pulse from 1 to 0 via a dataref.

Tony

Post Reply