# 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 ()

ipc.writeLvar("name", 0) else

ipc.writeLvar ("name" +6)

end

end

thanks for looking

Cheers

Stinger

##### Share on other sites
 Help AVSIM continue to serve you!Please donate today!

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()

if var >= 354 then

var = 0

else

var = var + 6

end

ipc.writeLvar("alt_value", val)

end

##### Share on other sites

Hi,

Many thanks for the prompt reply. I'll give it a go and let you know.

regards

Stinger

##### 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 ()

if var >= 354 then

var = 0

else

var = var + 6

ipc.writeLvar("CUSTOM_AP_ALT_KNOB", var)

end

end

function AltSelMinus ()

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 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.

##### 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 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 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.

##### 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 ()

if var >= 354 then

var = 0

else

var = var + 6

ipc.writeLvar("CUSTOM_AP_ALT_KNOB", var)

end

end

function AltSelMinus ()

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.

## Create an account

Register a new account

• Tom Allensworth,
Founder of AVSIM Online

• ### Hot Spots

• 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!