Do-While button is pressed.

Help creating logic scripts for Air Manager Instruments

Moderators: russ, Ralph

Post Reply
Message
Author
BeRol
Posts: 20
Joined: Sat Apr 20, 2019 8:34 pm

Do-While button is pressed.

#1 Post by BeRol »

Hello Team,

I seem to be having some trouble using the forum search correctly - but I am unable to locate this (assumedly) trivial piece of information.

I am trying to get the LUA-script to send an x-plane command WHILE a button is pressed.

while
function button_pitchtrim_up()
do
xpl_command("sim/flight_controls/pitch_trim_up")
print ("pitch trim up")
end
end

This piece of code is not working - but I am having a hard time figuring out the correct syntax. The LUA reference page does not give any specifics...

Could you please point me in the right direction?

Thanks in advance and cheers.

Benno

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

Re: Do-While button is pressed.

#2 Post by SimPassion »

Hi Benno @BeRol

Perhaps with something like this as a first simple approach ?

Code: Select all

function button_pitchtrim_up()
	xpl_command("sim/flight_controls/pitch_trim_up")
	print ("pitch trim up")
end

btn_hw_pitch_up = hw_button_add("Pitch Trim UP",button_pitchtrim_up)
Gilles

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

Re: Do-While button is pressed.

#3 Post by Sling »

You need to BEGIN and END the command like this.

Code: Select all

function button_pitchtrim_up_press()
    xpl_command("sim/flight_controls/pitch_trim_up", 1)
end

function button_pitchtrim_up_release()
    xpl_command("sim/flight_controls/pitch_trim_up, 0)
end

btn_hw_pitch_up = hw_button_add("Pitch Trim UP",button_pitchtrim_up_press, button_pitchtrim_up_release)

BeRol
Posts: 20
Joined: Sat Apr 20, 2019 8:34 pm

Re: Do-While button is pressed.

#4 Post by BeRol »

That did the trick!
Thank you very much.

I dare not ask why the LUA construct "Do-while-end" does not work in this case. Well - maybe some other time.

BTW - I really like the knobster!

Cheers

Benno

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

Re: Do-While button is pressed.

#5 Post by Corjan »

Hi Benno,


The scripting done in Air Manager is event based.

This means that AM will call a function when something important happens. Things like new data from the simulator, a button being pressed, etc. etc. etc. You can immediately do your thing and let the function finish.

You should never use any for or while loops since that can lock up the script.


Corjan

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

Re: Do-While button is pressed.

#6 Post by Sling »

Corjan wrote: Fri May 15, 2020 7:05 pm You should never use any for or while loops since that can lock up the script.
I don’t agree with this. For and While loops can be used successfully for certain tasks as long as you have a defined way to complete the loop. They are very good for doing multiples of the same thing. Some code would be unnecessarily long and complex without them.

This for instance can be used to add all the tick marks for a compass. It has a definite and pre defined finish. It loops for 72 passes and then completes.

for i=1, 72 do
—add compass ticks
end

User avatar
Ralph
Posts: 7933
Joined: Tue Oct 27, 2015 7:02 pm
Location: De Steeg
Contact:

Re: Do-While button is pressed.

#7 Post by Ralph »

For is okay, but while is a bit risky.

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

Re: Do-While button is pressed.

#8 Post by Corjan »

Sling wrote: Fri May 15, 2020 8:52 pm
Corjan wrote: Fri May 15, 2020 7:05 pm You should never use any for or while loops since that can lock up the script.
I don’t agree with this. For and While loops can be used successfully for certain tasks as long as you have a defined way to complete the loop. They are very good for doing multiples of the same thing. Some code would be unnecessarily long and complex without them.

This for instance can be used to add all the tick marks for a compass. It has a definite and pre defined finish. It loops for 72 passes and then completes.

for i=1, 72 do
—add compass ticks
end
You are right, there are situations where it makes sense and is totally okay.
Trying to say that you shouldn't make any loops that run indefinitely.

Corjan

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

Re: Do-While button is pressed.

#9 Post by Sling »

Corjan wrote: Sun May 17, 2020 7:53 am You are right, there are situations where it makes sense and is totally okay.
Trying to say that you shouldn't make any loops that run indefinitely.
100%. You have to be careful not to leave the completion of the loop open ended. Ralph is right, While can be a bit of a problem if the completion is not handled correctly.

Post Reply