Jump to content
Sign in to follow this  
stinger2k2

Arithmetic in lua ?

Recommended Posts

Hi,

 

I am trying to increase the value of my Lvar in increments of 6 from 0-354

 

Whats the LUA code for this as my existing code results in an error saying

that I am trying to perform arithmetic on a string.

 

Here's what I wrote

 

function AltPlusSlow ()

if ipc.readLvar("name") == 354 then

ipc.writeLvar("name", 0) else

ipc.writeLvar ("name" +6)

end

end

 

thanks for looking

Cheers

Stinger

Share this post


Link to post
Share on other sites

Hi Stinger

 

It is good practice to carry out any computation (calculations) within the function rather than read and write data directly to Lvars. Variables should also be declared local so they do not risk impacting other parts of your code. This function assumes that the 'alt_value' is numerical.

 

function AltPlusSlow()

local var = ipc.readLvar("alt_value")

 

if var >= 354 then

var = 0

else

var = var + 6

end

ipc.writeLvar("alt_value", val)

end


Andrew Gransden

Scotland, UK

LINDA Support/Developer - VATSIM and BAVirtual - Airbus Flyer

i7 1TB SSD GTX980 - FSX/P3D - Aerosoft Airbus A318/A319/A320/A321 - FS2Crew

Share this post


Link to post
Share on other sites

Okay, getting closer....

 

the Lvar is now increasing and decreasing in increments of 6 but it stops at 354 on the increase side

and stops at 0 on the decrease side?

 

Here's what I wrote........

*********************************************************************

function AltSelPlus ()

local var = ipc.readLvar("CUSTOM_AP_ALT_KNOB")

if var >= 354 then

var = 0

else

var = var + 6

ipc.writeLvar("CUSTOM_AP_ALT_KNOB", var)

end

end

function AltSelMinus ()

local var = ipc.readLvar("CUSTOM_AP_ALT_KNOB")

if var <= 0 then

var = 354

else

var = var - 6

ipc.writeLvar("CUSTOM_AP_ALT_KNOB", var)

end

end

************************************************************************

As an additional problem, although the Lvar value is changing

the altitude indicator is not ???

 

Does this mean there is some ipc.control going on too? If so, how do I check for this?

 

REALLY appreciate your help on this, I have been struggling since yestersay morning. :smile:

 

 

cheers

Stinger

Okay, I have now cured the stopping at 354 or 0 .
 
I just need to sort out why the altitude is not changing on the main display.
 
cheers
Stinger

Share this post


Link to post
Share on other sites

Remember what you are trying to do. A simple pencil and paper exercise is good for working it out.

 

In your minus function you are trying to move anti-/counter-clockwise. Therefore the step before zeeo/360 degrees is +6 not 354.

 

The test should be:

 

If var <= 6 then

var = 360

else

var = var -6

end

 

In some simulators there are 2 Lvars - one for indication and one for the knob. I suggest you study the code module for another aircraft and see how it does want you want to do.

 

Remember this is a support Forum for LINDA not for learning programming.


Andrew Gransden

Scotland, UK

LINDA Support/Developer - VATSIM and BAVirtual - Airbus Flyer

i7 1TB SSD GTX980 - FSX/P3D - Aerosoft Airbus A318/A319/A320/A321 - FS2Crew

Share this post


Link to post
Share on other sites

Hi again, I was actually close, it just needed a writeLvar in the middle of the function to set it to 0 when increasing from 354 and 354 when decreasing from zero.

 

I take your point about this being a Linda support forum. I shall ask elsewhere in future. However I appreciate your reply.

 

I had actually studied the other available modules before starting out but none of them seemed to have what I needed and after two days if head scratching I was getting desperate. !

 

Cheers

Stinger

Share this post


Link to post
Share on other sites

Sorry. I didn't spot where you placed the ipc.writeLvar statement. I have been looking at the screen all day and I am getting code blind.


Andrew Gransden

Scotland, UK

LINDA Support/Developer - VATSIM and BAVirtual - Airbus Flyer

i7 1TB SSD GTX980 - FSX/P3D - Aerosoft Airbus A318/A319/A320/A321 - FS2Crew

Share this post


Link to post
Share on other sites

Okay, getting closer....

 

the Lvar is now increasing and decreasing in increments of 6 but it stops at 354 on the increase side

and stops at 0 on the decrease side?

 

Here's what I wrote........

*********************************************************************

function AltSelPlus ()

local var = ipc.readLvar("CUSTOM_AP_ALT_KNOB")

if var >= 354 then

var = 0

else

var = var + 6

ipc.writeLvar("CUSTOM_AP_ALT_KNOB", var)

end

end

function AltSelMinus ()

local var = ipc.readLvar("CUSTOM_AP_ALT_KNOB")

if var <= 0 then

var = 354

else

var = var - 6

ipc.writeLvar("CUSTOM_AP_ALT_KNOB", var)

end

end

************************************************************************

As an additional problem, although the Lvar value is changing

the altitude indicator is not ???

 

Does this mean there is some ipc.control going on too? If so, how do I check for this?

 

REALLY appreciate your help on this, I have been struggling since yestersay morning. :smile:

 

 

cheers

Stinger

Okay, I have now cured the stopping at 354 or 0 .
 
I just need to sort out why the altitude is not changing on the main display.
 
cheers
Stinger

 

 

Stinger, could you post your final version here?

 

Thanks,

Dirk.

Share this post


Link to post
Share on other sites

Create an account or sign in to comment

You need to be a member in order to leave a comment

Create an account

Sign up for a new account in our community. It's easy!

Register a new account

Sign in

Already have an account? Sign in here.

Sign In Now
Sign in to follow this  

  • Tom Allensworth,
    Founder of AVSIM Online


  • Flight Simulation's Premier Resource!

    AVSIM is a free service to the flight simulation community. AVSIM is staffed completely by volunteers and all funds donated to AVSIM go directly back to supporting the community. Your donation here helps to pay our bandwidth costs, emergency funding, and other general costs that crop up from time to time. Thank you for your support!

    Click here for more information and to see all donations year to date.
×
×
  • Create New...