compare 2 consecutive values of a variable

Help creating logic scripts for Air Manager Instruments

Moderators: russ, Ralph

Post Reply
Message
Author
maak_pk
Posts: 27
Joined: Sun Dec 13, 2020 11:37 am
Location: Pakistan

compare 2 consecutive values of a variable

#1 Post by maak_pk »

hello folks
i hope someone can help me out to compare the value whether if its increasing or decreasing so i can trigger the event accordingly

Code: Select all

function thr_psn(psn)
if psn >33 then

fsx_event("RUDDER_LEFT")

end

fsx_variable_subscribe("GENERAL ENG THROTTLE LEVER POSITION:1", "percent",
thr_psn)

i want to actually trigger rudder with each application of throttle
cant figure out in lua how to compare the new value of variable with last one
looking forward for quick guide plz
regards
maak

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

Re: compare 2 consecutive values of a variable

#2 Post by Keith Baxter »

Hi,

Try this.

Code: Select all

old_val=1
new_val=1
function compare(val)
    new_val=new_val+val
    if old_val >= new_val then bla bla bla end
    old_val=new_val
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 

maak_pk
Posts: 27
Joined: Sun Dec 13, 2020 11:37 am
Location: Pakistan

Re: compare 2 consecutive values of a variable

#3 Post by maak_pk »

hi thanks for a quick reply
i need to understand how can i store my thr_psn in two different variables with every change in it

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

Re: compare 2 consecutive values of a variable

#4 Post by Keith Baxter »

maak_pk wrote: Mon Dec 14, 2020 6:18 am hi thanks for a quick reply
i need to understand how can i store my thr_psn in two different variables with every change in it
Hi,

You are storing to two variables before the old value becomes equal to the new value. To measure change at some point both variables must be equal.

You could have a third variable change_var.

Code: Select all

old_val=1
new_val=1
change_var=""
function compare(dir)
    new_val=new_val+dir
    if  new_val  >= old_val then change_var="Greater Value" else change_var="Lesser Value" end
    old_val=new_val
    print("The Value is a "..change_var)
end			
fsx_variable_subscribe("GENERAL ENG THROTTLE LEVER POSITION:1", "percent", compare)					
end)



If you have two data sources that is something different.

Keith
Last edited by Keith Baxter on Mon Dec 14, 2020 8:35 am, edited 2 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 

maak_pk
Posts: 27
Joined: Sun Dec 13, 2020 11:37 am
Location: Pakistan

Re: compare 2 consecutive values of a variable

#5 Post by maak_pk »

sorry to bother u again
by this i am getting the old and new val same all the time and new val is always increasing

Code: Select all

old_val=1
new_val=1
change_var=""
function compare(val)
    new_val=new_val+val
    if old_val >= new_val then change_var="Greater Value" else change_var="Lesser Value" end
    old_val=new_val
    print("The Value is a "..change_var)
print(new_val)
print("old"..old_val)
print("val"..val)
end			
fsx_variable_subscribe("GENERAL ENG THROTTLE LEVER POSITION:1", "percent", compare)					


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

Re: compare 2 consecutive values of a variable

#6 Post by Keith Baxter »

Hi,

That is because you print statement is after old_val =new_val but before the event is firing the callback
Put the print statement before and you will see different values.

Code: Select all

old_val=1
new_val=1
change_var=""
function compare(val)
    new_val=val
    if new_val >= old_val  then change_var="Greater Value" else change_var="Lesser Value" end
   print(new_val)
   print("old"..old_val)
   print("val"..val)   
    
    old_val=new_val
    print("The NEW Value is a "..change_var)

end			
fsx_variable_subscribe("GENERAL ENG THROTTLE LEVER POSITION:1", "percent", compare)					


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

Re: compare 2 consecutive values of a variable

#7 Post by Sling »

First up. Please only post the question in one thread. I’ve just replied to another thread with the same question. The solution is very simple.

Code: Select all


local old_val = 200

function compare(val)
    
    if val > old_val  and old_val < 200 then
        —do increasing stuff here
    elseif val < old_val and old_val < 200 then
        —do decreasing stuff here
    end
    
    old_val = val

end			
fsx_variable_subscribe("GENERAL ENG THROTTLE LEVER POSITION:1", "percent", compare)					



maak_pk
Posts: 27
Joined: Sun Dec 13, 2020 11:37 am
Location: Pakistan

Re: compare 2 consecutive values of a variable

#8 Post by maak_pk »

Thanks alot

Post Reply