Jump to content
The simFlight Network Forums

A beginner's try with conditional lua script


Recommended Posts

Posted

Hi everyone,

I am coming here to ask for guidance as a newbie in writing a lua script that would accomplish a very simple task.

My intention is to have a lua script that would check whether the engine anti ice in four engine aircraft (FBW A380 ) is ON, and if so then either leave it ON or turn it OFF based on which button I press. My current script doesn't seem to work, and I get the following error in the FSUIPC7.log

18391 *** LUA Error: E:\FSUIPC7\LuaFiles\FBW_TEST.lua:3: '<name>' expected near '"XMLVAR_MOMENTARY_PUSH_OVHD_ANTIICE_ENG1_PRESSED"'

My LUA script is the following:

function engantiice() 

val1 = ipc.readLvar(L:"XMLVAR_MOMENTARY_PUSH_OVHD_ANTIICE_ENG1_PRESSED")
val2 = ipc.readLvar(L:"XMLVAR_MOMENTARY_PUSH_OVHD_ANTIICE_ENG2_PRESSED")
val3 = ipc.readLvar(L:"XMLVAR_MOMENTARY_PUSH_OVHD_ANTIICE_ENG3_PRESSED")
val4 = ipc.readLvar(L:"XMLVAR_MOMENTARY_PUSH_OVHD_ANTIICE_ENG4_PRESSED")

if val1 == 1 then ipc.execPreset("A380X_OH_ANTIICE_ENG1_TOG", 0) end
if val2 == 1 then ipc.execPreset("A380X_OH_ANTIICE_ENG2_TOG", 0) end
ipc.sleep(700)
if val3 == 1 then ipc.execPreset("A380X_OH_ANTIICE_ENG3_TOG", 0) end
if val4 == 1 then ipc.execPreset("A380X_OH_ANTIICE_ENG4_TOG", 0) end

end

Could anyone explain to me in a very simplified language on what am I doing wrong here? Please note I am far away from being a programmer of any kind. I am just trying to learn this.
I have watched some videos on how to write a conditional LUA script but those are never ever dealing with flight sim and variables etc, so it's difficult for me to put in perspective when it comes flight sim and its related differences.

I would appreciate if someone could chime in and say what's wrong above?

Thank you so much,

Balint Szarka

Posted
1 hour ago, Bálint Szarka said:
val1 = ipc.readLvar(L:"XMLVAR_MOMENTARY_PUSH_OVHD_ANTIICE_ENG1_PRESSED")
val2 = ipc.readLvar(L:"XMLVAR_MOMENTARY_PUSH_OVHD_ANTIICE_ENG2_PRESSED")
val3 = ipc.readLvar(L:"XMLVAR_MOMENTARY_PUSH_OVHD_ANTIICE_ENG3_PRESSED")
val4 = ipc.readLvar(L:"XMLVAR_MOMENTARY_PUSH_OVHD_ANTIICE_ENG4_PRESSED")

The L: needs to be in the quotes, i.e.

Quote

val1 = ipc.readLvar("L:XMLVAR_MOMENTARY_PUSH_OVHD_ANTIICE_ENG1_PRESSED")
val2 = ipc.readLvar("L:XMLVAR_MOMENTARY_PUSH_OVHD_ANTIICE_ENG2_PRESSED")
val3 = ipc.readLvar("L:XMLVAR_MOMENTARY_PUSH_OVHD_ANTIICE_ENG3_PRESSED")
val4 = ipc.readLvar("L:XMLVAR_MOMENTARY_PUSH_OVHD_ANTIICE_ENG4_PRESSED")

John

Posted
1 hour ago, Bálint Szarka said:

do you think that you would be able to pin point me towards a direction where I can figure out how can I continuously keep checking until a condition is met?

Use the while construct - from https://www.tutorialspoint.com/lua/lua_while_loop.htm:

Quote

Syntax

The syntax of a while loop in Lua programming language is as follows −

 
while(condition) do statement(s) end

Here, statement(s) may be a single statement or a block of statements. The condition may be any expression, and true is any non-zero value. The loop iterates while the condition is true.

The do statement can be a sleep (e.g. ipc.sleep(25)) to wait a short time to not consume so many resources and to give time to other threads so that the condition can become true. The loop will exit and the lua code continue when the statement is true.

Such a loop is useful in many circumstances, for example to wait for an lvar to become available (not all lvars are initially available!), use:

-- Check Lvar exists
id = ipc.getLvarId("L:FS2CEW_RAASPRO_MASTER_SWITCH")
while ( id == nil or id > 65535 or id < 0) -- Note that just a nil check should be sufficient from 7.5.2 onwards 
do
  ipc.reloadWASM() -- request to scan for new lvars
  ipc.sleep(200) -- wait before checking again
  id = ipc.getLvarId("L:FS2CEW_RAASPRO_MASTER_SWITCH")
end
-- lvar now available

By the way, no need to tag me - I read all posts anyway!

Cheers,

John

Posted
13 minutes ago, John Dowson said:

Use the while construct - from https://www.tutorialspoint.com/lua/lua_while_loop.htm:

The do statement can be a sleep (e.g. ipc.sleep(25)) to wait a short time to not consume so many resources and to give time to other threads so that the condition can become true. The loop will exit and the lua code continue when the statement is true.

Such a loop is useful in many circumstances, for example to wait for an lvar to become available (not all lvars are initially available!), use:

-- Check Lvar exists
id = ipc.getLvarId("L:FS2CEW_RAASPRO_MASTER_SWITCH")
while ( id == nil or id > 65535 or id < 0) -- Note that just a nil check should be sufficient from 7.5.2 onwards 
do
  ipc.reloadWASM() -- request to scan for new lvars
  ipc.sleep(200) -- wait before checking again
  id = ipc.getLvarId("L:FS2CEW_RAASPRO_MASTER_SWITCH")
end
-- lvar now available

By the way, no need to tag me - I read all posts anyway!

Cheers,

John

Thanks for the tip. Sorry for the tag, I learn on the go.

Posted
14 hours ago, Bálint Szarka said:

Sorry for the tag, I learn on the go.

No need to be sorry - you can still tag me, its just that its generally not necessary as I read all posts on these forums anyway!

John

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
×
×
  • Create New...

Important Information

By using this site, you agree to our Terms of Use. Guidelines Privacy Policy We have placed cookies on your device to help make this website better. You can adjust your cookie settings, otherwise we'll assume you're okay to continue.